public String newDocument(InputStream in, String json) throws DocumentException { try { GridFS gridFS = new GridFS(dataBase); GridFSInputFile gridFSInputFile = gridFS.createFile(in); ObjectId objectId = (ObjectId) gridFSInputFile.getId(); String GUID = objectId.toStringMongod(); DBObject dbObject = (DBObject) JSON.parse(json); gridFSInputFile.setFilename((String) dbObject.get(NAME)); gridFSInputFile.setContentType((String) dbObject.get(CONTENT_TYPE)); gridFSInputFile.setMetaData(dbObject); gridFSInputFile.save(); CommandResult result = dataBase.getLastError(); if (!result.ok()) { throw new DocumentException(result.getErrorMessage()); } return GUID; } catch (Exception e) { log.error("newDocument error:" + e.getMessage()); e.printStackTrace(); throw new DocumentException(e.getMessage()); } }
public Photo saveImg(FileItem item) throws Exception { Photo photo = new Photo(); String filename = item.getName(); if (filename == null || filename.length() == 0) { log.error("img name illegal"); return null; } int index = filename.lastIndexOf("."); String type = filename.substring(index + 1); if (!ImgTools.checkImgFormatValidata(type)) { log.error("img type illegal"); return null; } ObjectId id = ObjectIdGenerator.generate(); // filename = new ObjectId() + filename.substring(index); photo.setId(id.toString()); photo.setType(type); GridFS mphoto = new GridFS(MongoDBPool.getInstance().getDB(), collection); GridFSInputFile in = null; in = mphoto.createFile(item.getInputStream()); in.setId(id); in.setFilename(id.toString()); in.setContentType(type); in.save(); item.getInputStream().close(); return photo; }
public String newDocument(InputStream in, Map<String, String> properties) throws DocumentException { try { GridFS gridFS = new GridFS(dataBase); GridFSInputFile gridFSInputFile = gridFS.createFile(in); ObjectId id = (ObjectId) gridFSInputFile.getId(); String GUID = id.toStringMongod(); gridFSInputFile.setFilename(properties.get(DocumentConnector.NAME)); gridFSInputFile.setContentType(properties.get(DocumentConnector.CONTENT_TYPE)); gridFSInputFile.put(DocumentConnector.ID, properties.get(DocumentConnector.ID)); gridFSInputFile.put( DocumentConnector.DOCUMENT_TYPE, properties.get(DocumentConnector.DOCUMENT_TYPE)); gridFSInputFile.save(); CommandResult result = dataBase.getLastError(); if (!result.ok()) { throw new DocumentException(result.getErrorMessage()); } return GUID; } catch (Exception e) { log.error("newDocument error:" + e.getMessage()); e.printStackTrace(); throw new DocumentException(e.getMessage()); } }
@Override public void saveAvatar(String userName, InputStream originalIs) throws AvatarUploadException { ByteArrayOutputStream os = null; InputStream is = null; try { BufferedImage originalImage = ImageIO.read(originalIs); BufferedImage thumbnail = Thumbnails.of(originalImage).crop(Positions.CENTER).size(128, 128).asBufferedImage(); os = new ByteArrayOutputStream(); ImageIO.write(thumbnail, "png", os); is = new ByteArrayInputStream(os.toByteArray()); String fileName = String.format(AVATAR_NAME, userName); GridFS avatarFS = new GridFS(m_template.getDb()); avatarFS.remove(fileName); GridFSInputFile gfsFile = avatarFS.createFile(is); gfsFile.setFilename(fileName); gfsFile.save(); } catch (Exception ex) { throw new AvatarUploadException(ex); } finally { IOUtils.closeQuietly(originalIs); IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } }
@Test public void testImportAttachment() throws Exception { logger.debug("*** testImportAttachment ***"); byte[] content = copyToBytesFromClasspath( "/test/elasticsearch/plugin/river/mongodb/gridfs/test-attachment.html"); logger.debug("Content in bytes: {}", content.length); GridFS gridFS = new GridFS(mongoDB); GridFSInputFile in = gridFS.createFile(content); in.setFilename("test-attachment.html"); in.setContentType("text/html"); in.save(); in.validate(); String id = in.getId().toString(); logger.debug("GridFS in: {}", in); logger.debug("Document created with id: {}", id); GridFSDBFile out = gridFS.findOne(in.getFilename()); logger.debug("GridFS from findOne: {}", out); out = gridFS.findOne(new ObjectId(id)); logger.debug("GridFS from findOne: {}", out); Assert.assertEquals(out.getId(), in.getId()); Thread.sleep(wait); refreshIndex(); CountResponse countResponse = getNode().client().count(countRequest(getIndex())).actionGet(); logger.debug("Index total count: {}", countResponse.getCount()); assertThat(countResponse.getCount(), equalTo(1l)); countResponse = getNode().client().count(countRequest(getIndex()).query(fieldQuery("_id", id))).actionGet(); logger.debug("Index count for id {}: {}", id, countResponse.getCount()); assertThat(countResponse.getCount(), equalTo(1l)); SearchResponse response = getNode() .client() .prepareSearch(getIndex()) .setQuery(QueryBuilders.queryString("Aliquam")) .execute() .actionGet(); logger.debug("SearchResponse {}", response.toString()); long totalHits = response.getHits().getTotalHits(); logger.debug("TotalHits: {}", totalHits); assertThat(totalHits, equalTo(1l)); gridFS.remove(new ObjectId(id)); Thread.sleep(wait); refreshIndex(); countResponse = getNode().client().count(countRequest(getIndex()).query(fieldQuery("_id", id))).actionGet(); logger.debug("Count after delete request: {}", countResponse.getCount()); assertThat(countResponse.getCount(), equalTo(0L)); }
/** * @param args * @throws UnknownHostException * @throws IOException */ public static void main(String[] args) throws IOException { Mongo mongo = new Mongo("localhost", 27017); DB db = mongo.getDB("test"); String newFileName = "mkyong-java-image5"; File imageFile = new File("c:\\mongodb.jpg"); GridFS gfsPhoto = new GridFS(db); GridFSInputFile gfsFile = gfsPhoto.createFile(imageFile); gfsFile.setFilename(newFileName); gfsFile.setContentType("image/jpeg"); gfsFile.save(); /// System.out.println(gfsFile.getId()); gfsFile = gfsPhoto.createFile(imageFile); gfsFile.setFilename(newFileName); gfsFile.setContentType("image/jpeg"); gfsFile.save(); System.out.println(gfsFile.getId()); /* RegistryKey rg = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(suffix); object obj=rg.GetValue("Content Type"); result =obj!=null?obj.ToString():string.Empty; rg.Close(); */ }
@Test public void shouldStoreFileInMultipleChunks() throws Exception { final byte[] data = new byte[] {1, 2, 3, 4, 5}; final GridFSInputFile file = fs.createFile(data); file.setChunkSize(3); // chunk size is less than data size in order to get more than one chunk file.save(); final GridFSDBFile result = fs.findOne((ObjectId) file.getId()); final ByteArrayOutputStream out = new ByteArrayOutputStream(); assertEquals(data.length, result.writeTo(out)); assertArrayEquals(data, out.toByteArray()); }
@Override public Operation call() throws Exception { try { String ritePropertiesFilename = Rite.getInstance().getProperty(Rite.PropertyKeys.HOST); Properties hostProps = new Properties(); hostProps.load(new FileInputStream(ritePropertiesFilename)); String hostname = hostProps.getProperty("hostname"); int port = Integer.parseInt(hostProps.getProperty("port")); String dbname = hostProps.getProperty("dbname"); boolean auth = Boolean.parseBoolean(hostProps.getProperty("auth")); Mongo mongo = new Mongo(hostname, port); DB db = mongo.getDB(dbname); if (auth) { String user = hostProps.getProperty("user"); String pass = hostProps.getProperty("pass"); db.authenticate(user, pass.toCharArray()); } GridFS gfs = new GridFS(db); String filename = getFileName(); File f = new File(filename); if (!f.exists()) { throw new Exception("The file " + filename + " does not exist locally!"); } int filesInDb = gfs.find(filename).size(); if (filesInDb > 0) { throw new Exception("The file " + filename + " already exists in the database!"); } GridFSInputFile gsampleFile = gfs.createFile(f); gsampleFile.setFilename(f.getName()); gsampleFile.save(); mongo.close(); } catch (Exception e) { this.setProperty( GenericOperation.PropertyKeys.ERROR, OperationUtilities.getStackTraceAsString(e)); this.fail(); this.complete(); return this; } this.complete(); return this; }
public static void main(String[] args) throws Exception { MongoClient client = new MongoClient(); DB db = client.getDB("course"); FileInputStream inputStream = null; GridFS videos = new GridFS(db, "video"); // does not create new GridFS it just gives object to manipulate it // returns bucket named video try { inputStream = new FileInputStream("BinarySearch.mp4"); } catch (FileNotFoundException e) { e.printStackTrace(); } GridFSInputFile video = videos.createFile(inputStream, "BinarySearch.mp4"); BasicDBObject meta = new BasicDBObject("description", "Binary Search"); ArrayList<String> tags = new ArrayList<String>(); tags.add("search"); tags.add("data structures"); meta.append("tags", tags); video.setMetaData(meta); video.save(); System.out.println(video.get("_id")); System.out.println("file saved"); System.out.println("reading file from mongo"); GridFSDBFile dbFile = videos.findOne(new BasicDBObject("filename", "BinarySearch.mp4")); FileOutputStream outputStream = new FileOutputStream("BinarySearch_copy.mp4"); dbFile.writeTo(outputStream); }
/** * @param file * @param db * @param contentType * @param filename * @param id * @return */ private static GridFSInputFile putSongFileInDB( File file, DB db, String contentType, String filename, Object id) { byte[] songAsBytes = null; GridFS myFS = new GridFS(db); GridFSInputFile gridFSInputFile = null; try { songAsBytes = FileUtils.readFileToByteArray(file); gridFSInputFile = myFS.createFile(songAsBytes); gridFSInputFile.setFilename(filename); gridFSInputFile.setContentType(contentType); if (id != null) { gridFSInputFile.put("_id", id); } gridFSInputFile.save(); // insert file } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return gridFSInputFile; }
@Override public void saveBlob(final MD5 md5, final InputStream data, final boolean sorted) throws BlobStoreCommunicationException { if (data == null || md5 == null) { throw new NullPointerException("Arguments cannot be null"); } if (getFile(md5) != null) { return; // already exists } final GridFSInputFile gif = gfs.createFile(data, true); gif.setId(md5.getMD5()); gif.setFilename(md5.getMD5()); gif.put(Fields.GFS_SORTED, sorted); try { gif.save(); } catch (DuplicateKeyException dk) { // already here, done } catch (MongoException me) { throw new BlobStoreCommunicationException("Could not write to the mongo database", me); } }
@BodyParser.Of(BodyParser.Json.class) public static Result newBeer() { GridFSInputFile gfsImg = null; ObjectMapper mapper = new ObjectMapper(); Beer beer = null; try { beer = mapper.readValue(request().body().asJson(), Beer.class); beers.save(beer); } catch (JsonParseException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (JsonMappingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { BufferedImage img = ImageIO.read(new URL("http://wwwimages.harpoonbrewery.com/SummerBeer-2013-Modal.jpg")); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(img, "jpg", baos); baos.flush(); gfsImg = gfs.createFile(baos.toByteArray()); gfsImg.setFilename("bestbeer.jpg"); gfsImg.save(); beer.imgId = gfsImg.getId().toString(); beers.save(beer); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ok(Json.parse("{\"beerId\":\"" + beer.getId() + "\"}")); }
public FileEntity saveFile( String filePath, String fileName, String contentType, InputStream inputStream, String objectId) { if (filePath == null) filePath = "fs"; GridFS gridFS = new GridFS(db, filePath); GridFSInputFile gfsFile = gridFS.createFile(inputStream); gfsFile.setFilename(fileName); gfsFile.setContentType(contentType); if (objectId != null) gfsFile.setId(new ObjectId(objectId)); // String objectId = gfsFile.getId().toString(); gfsFile.save(); GridFSDBFile dbFile = gridFS.findOne(new ObjectId(gfsFile.getId().toString())); FileEntity entity = new FileEntity(dbFile); return entity; }
@Test public void testImportAttachment() throws Exception { logger.debug("*** testImportAttachment ***"); try { // createDatabase(); byte[] content = copyToBytesFromClasspath(TEST_ATTACHMENT_HTML); logger.debug("Content in bytes: {}", content.length); GridFS gridFS = new GridFS(mongoDB); GridFSInputFile in = gridFS.createFile(content); in.setFilename("test-attachment.html"); in.setContentType("text/html"); in.save(); in.validate(); String id = in.getId().toString(); logger.debug("GridFS in: {}", in); logger.debug("Document created with id: {}", id); GridFSDBFile out = gridFS.findOne(in.getFilename()); logger.debug("GridFS from findOne: {}", out); out = gridFS.findOne(new ObjectId(id)); logger.debug("GridFS from findOne: {}", out); Assert.assertEquals(out.getId(), in.getId()); Thread.sleep(wait); refreshIndex(); CountResponse countResponse = getNode().client().count(countRequest(getIndex())).actionGet(); logger.debug("Index total count: {}", countResponse.getCount()); assertThat(countResponse.getCount(), equalTo(1l)); GetResponse getResponse = getNode().client().get(getRequest(getIndex()).id(id)).get(); logger.debug("Get request for id {}: {}", id, getResponse.isExists()); assertThat(getResponse.isExists(), equalTo(true)); // countResponse = // getNode().client().count(countRequest(getIndex()).query(fieldQuery("_id", // id))).actionGet(); // logger.debug("Index count for id {}: {}", id, countResponse.getCount()); // assertThat(countResponse.getCount(), equalTo(1l)); SearchResponse response = getNode() .client() .prepareSearch(getIndex()) .setQuery(QueryBuilders.queryString("Aliquam")) .execute() .actionGet(); logger.debug("SearchResponse {}", response.toString()); long totalHits = response.getHits().getTotalHits(); logger.debug("TotalHits: {}", totalHits); assertThat(totalHits, equalTo(1l)); gridFS.remove(new ObjectId(id)); Thread.sleep(wait); refreshIndex(); getResponse = getNode().client().get(getRequest(getIndex()).id(id)).get(); logger.debug("Get request for id {}: {}", id, getResponse.isExists()); assertThat(getResponse.isExists(), equalTo(false)); // countResponse = // getNode().client().count(countRequest(getIndex()).query(fieldQuery("_id", // id))).actionGet(); // logger.debug("Count after delete request: {}", countResponse.getCount()); // assertThat(countResponse.getCount(), equalTo(0L)); } catch (Throwable t) { logger.error("testImportAttachment failed.", t); Assert.fail("testImportAttachment failed", t); } finally { // cleanUp(); } }
private void uploadMapToServer(RoomMap newMap) { Mongo mongo = null; DB database; DBCollection sampleData; String databaseAddress = DEFAULT_DATABASE_ADDRESS; try { // read parameters from the configuration file databaseAddress = Utilities.getConfigurationValue(DATABASE_ADDRESS); } catch (NumberFormatException exception) { // reading has failed, use default values logger.info( "Reading parameters from configuration file failed. " + "Using default values for database_address instead."); } try { mongo = new Mongo(databaseAddress); } catch (UnknownHostException e1) { e1.printStackTrace(); } database = mongo.getDB("rssiSystem"); sampleData = database.getCollection("map_records"); // remove all maps sampleData.drop(); SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); String strDate = simpledateformat.format(new Date()); // ------------------- // Load our image byte[] imageBytes = null; try { imageBytes = Utilities.LoadImageAsBytes(newMap.getPath()); } catch (Exception e1) { e1.printStackTrace(); } // --------------------- // Create GridFS object GridFS fs = new GridFS(database); // Save image into database GridFSInputFile in = fs.createFile(imageBytes); in.save(); Object mapIdObject = in.getId(); // System.out.println(mapIdObject); try { DBObject documentDetail = new BasicDBObject(); documentDetail.put("_cls", "mapRecords"); // for mongoEngine ORM users documentDetail.put("image", mapIdObject); documentDetail.put("mapId", newMap.getId()); documentDetail.put("width", newMap.getWidthInMeters()); documentDetail.put("height", newMap.getHeightInMeters()); documentDetail.put("offsetX", newMap.getLowerLeftMarkerOffsetXInPixels()); documentDetail.put("offsetY", newMap.getLowerLeftMarkerOffsetYInPixels()); documentDetail.put("offset2X", newMap.getUpperRightMarkerOffsetXInPixels()); documentDetail.put("offset2Y", newMap.getUpperRightMarkerOffsetYInPixels()); documentDetail.put("scalingX", newMap.getRatioWidth()); documentDetail.put("scalingY", newMap.getRatioHeight()); documentDetail.put("title", newMap.getTitle()); documentDetail.put("updateTime", strDate); sampleData.insert(documentDetail); } catch (Exception e) { e.printStackTrace(); } }