List of usage examples for com.mongodb.gridfs GridFS GridFS
public GridFS(final DB db, final String bucket)
From source file:com.englishtown.vertx.GridFSModule.java
License:Open Source License
public void getFile(Message<JsonObject> message, JsonObject jsonObject) { ObjectId objectId = getObjectId(message, jsonObject, "id"); if (objectId == null) { return;// w w w .ja v a2 s. co m } // Optional bucket, default is "fs" String bucket = jsonObject.getString("bucket", GridFS.DEFAULT_BUCKET); GridFS files = new GridFS(db, bucket); GridFSDBFile file = files.findOne(objectId); if (file == null) { sendError(message, "File does not exist: " + objectId.toString()); return; } JsonObject fileInfo = new JsonObject().putString("filename", file.getFilename()) .putString("contentType", file.getContentType()).putNumber("length", file.getLength()) .putNumber("chunkSize", file.getChunkSize()) .putNumber("uploadDate", file.getUploadDate().getTime()); DBObject metadata = file.getMetaData(); if (metadata != null) { fileInfo.putObject("metadata", new JsonObject(JSON.serialize(metadata))); } // Send file info sendOK(message, fileInfo); }
From source file:com.fileoperations.CopyClass.java
public Boolean forSingleFile() { try {//from w w w. j ava2 s.co m if (name.contains(".")) { BasicDBObject query = new BasicDBObject(); query.put("_id", parentPath + pathMerger + name); DBCursor cursor = collection.find(query); if (cursor.hasNext()) { BasicDBObject checknewquery = new BasicDBObject(); checknewquery.put("_id", newPath + pathMerger + name); DBCursor tempCursor = collection.find(checknewquery); if (tempCursor.hasNext()) { return false; } DBObject copyFile = cursor.next(); GridFS fileDB = new GridFS(mymongo.getDB(), userCollectionName); InputStream data = fileDB.findOne(query).getInputStream(); BasicDBObject document = new BasicDBObject(); document.append("_id", newPath + pathMerger + name); document.append("folder", "0"); document.append("parent", newPath); document.append("name", name); document.append("type", copyFile.get("type").toString()); collection.insert(document); GridFSInputFile inputFile = fileDB.createFile(data); inputFile.setId(newPath + pathMerger + name); inputFile.put("path", newPath); inputFile.setFilename(name); inputFile.save(); return true; } else { return false; } } else { return false; } } finally { mymongo.closeConnection(); } }
From source file:com.fileoperations.FolderDownload.java
public File makeFolder() throws IOException { try {//from ww w .j a v a 2 s. c o m String mongoFolder = parentPath + pathMerger + folderName; BasicDBObject query = new BasicDBObject(); query.put("_id", mongoFolder); DBCursor cursor = collection.find(query); if (cursor.hasNext()) { getPathOfAllChildrenFolder(parentPath, folderName); BasicDBObject toFindAllFilesInFolder = new BasicDBObject(); toFindAllFilesInFolder.put("$or", pathOfChildrenFolders); GridFS fileStore = new GridFS(mymongo.getDB(), userCollectionName); List<GridFSDBFile> AllFiles = fileStore.find(toFindAllFilesInFolder); File zip = new File(folderName + ".zip"); ZipOutputStream folderToZip = new ZipOutputStream(new FileOutputStream(zip)); for (int i = 0; i < AllFiles.size(); i++) { GridFSDBFile indivFile = AllFiles.get(i); ByteArrayOutputStream out = new ByteArrayOutputStream(); InputStream in = indivFile.getInputStream(); int data = in.read(); while (data >= 0) { out.write((char) data); data = in.read(); } out.flush(); String zipPath; zipPath = indivFile.get("path").toString() + pathMerger + indivFile.getFilename(); zipPath = zipPath.replaceFirst(parentPath, ""); zipPath = zipPath.replaceFirst(pathMerger, ""); ZipEntry add = new ZipEntry(zipPath); folderToZip.putNextEntry(add); folderToZip.write(out.toByteArray(), 0, out.toByteArray().length); } BasicDBObject toFindAllEmptyFilesInFolder = new BasicDBObject(); toFindAllEmptyFilesInFolder.put("$or", pathOfChildrenEmptyFolders); DBCursor emptyFolder = collection.find(toFindAllEmptyFilesInFolder); List<String> emptyFolderPathToAdd = new ArrayList<String>(); while (emptyFolder.hasNext()) { DBObject temp = emptyFolder.next(); BasicDBObject isEmpty = new BasicDBObject(); isEmpty.put("parent", temp.get("_id").toString()); if (!collection.find(isEmpty).hasNext()) { if (!temp.get("_id").toString().contains(".")) { emptyFolderPathToAdd.add(temp.get("_id").toString()); } } } for (int i = 0; i < emptyFolderPathToAdd.size(); i++) { String temp = emptyFolderPathToAdd.get(i).replaceFirst(parentPath, ""); temp = temp.replaceFirst(pathMerger, ""); ZipEntry add = new ZipEntry(temp + pathMerger); folderToZip.putNextEntry(add); } folderToZip.closeEntry(); folderToZip.close(); return zip; } else { return null; } } finally { mymongo.closeConnection(); } }
From source file:com.fileoperations.FolderDownload.java
public Boolean copyFolder(String newPath) throws IOException { try {//from w w w . ja v a 2 s . c om String mongoFolder = parentPath + pathMerger + folderName; BasicDBObject query = new BasicDBObject(); query.put("_id", mongoFolder); DBCursor cursor = collection.find(query); if (cursor.hasNext()) { BasicDBObject newquery = new BasicDBObject(); newquery.put("_id", newPath + pathMerger + folderName); if (collection.find(newquery).hasNext()) { return false; } getPathOfAllChildrenFolder(parentPath, folderName); BasicDBObject toFindAllFilesInFolder = new BasicDBObject(); toFindAllFilesInFolder.put("$or", pathOfChildrenFolders); GridFS fileStore = new GridFS(mymongo.getDB(), userCollectionName); List<GridFSDBFile> AllFiles = fileStore.find(toFindAllFilesInFolder); for (int i = 0; i < AllFiles.size(); i++) { GridFSDBFile indivFile = AllFiles.get(i); InputStream data = indivFile.getInputStream(); String zipPath; zipPath = indivFile.get("path").toString(); String tempFileName = indivFile.getFilename(); zipPath = zipPath.replaceFirst(parentPath, newPath); BasicDBObject document = new BasicDBObject(); document.append("_id", zipPath + pathMerger + tempFileName); document.append("folder", "0"); document.append("parent", zipPath); document.append("name", tempFileName); int index = tempFileName.lastIndexOf("."); document.append("type", tempFileName.substring(index)); collection.insert(document); GridFSInputFile inputFile = fileStore.createFile(data); inputFile.setId(zipPath + pathMerger + tempFileName); inputFile.put("path", zipPath); inputFile.setFilename(tempFileName); inputFile.save(); } BasicDBObject toFindAllEmptyFilesInFolder = new BasicDBObject(); toFindAllEmptyFilesInFolder.put("$or", pathOfChildrenEmptyFolders); DBCursor allFolders = collection.find(toFindAllEmptyFilesInFolder); while (allFolders.hasNext()) { DBObject temp = allFolders.next(); if (temp.get("folder").toString().equals("1")) { String tempPath = temp.get("parent").toString().replaceFirst(parentPath, newPath); BasicDBObject document = new BasicDBObject(); document.put("_id", tempPath + pathMerger + temp.get("name")); document.put("folder", "1"); document.put("name", temp.get("name")); document.put("parent", tempPath); document.put("type", "1"); collection.insert(document); } } return true; } else { return false; } } finally { mymongo.closeConnection(); } }
From source file:com.fileoperations.FolderDownload.java
public Boolean renameFolder(String newName) throws IOException { try {// www . j av a 2 s .c o m String mongoFolder = parentPath + pathMerger + folderName; BasicDBObject query = new BasicDBObject(); query.put("_id", mongoFolder); DBCursor cursor = collection.find(query); if (cursor.hasNext()) { BasicDBObject newquery = new BasicDBObject(); newquery.put("_id", parentPath + pathMerger + newName); if (collection.find(newquery).hasNext()) { return false; } BasicDBObject doc = new BasicDBObject(); doc.put("_id", parentPath + pathMerger + newName); doc.put("folder", "1"); doc.put("name", newName); doc.put("parent", parentPath); doc.put("type", "1"); collection.insert(doc); getPathOfAllChildrenFolder(parentPath, folderName); BasicDBObject toFindAllFilesInFolder = new BasicDBObject(); toFindAllFilesInFolder.put("$or", pathOfChildrenFolders); GridFS fileStore = new GridFS(mymongo.getDB(), userCollectionName); List<GridFSDBFile> AllFiles = fileStore.find(toFindAllFilesInFolder); for (int i = 0; i < AllFiles.size(); i++) { GridFSDBFile indivFile = AllFiles.get(i); InputStream data = indivFile.getInputStream(); String zipPath; zipPath = indivFile.get("path").toString(); String tempFileName = indivFile.getFilename(); zipPath = zipPath.replaceFirst(parentPath + pathMerger + folderName, parentPath + pathMerger + newName); BasicDBObject document = new BasicDBObject(); document.append("_id", zipPath + pathMerger + tempFileName); document.append("folder", "0"); document.append("parent", zipPath); document.append("name", tempFileName); int index = tempFileName.lastIndexOf("."); document.append("type", tempFileName.substring(index)); collection.insert(document); GridFSInputFile inputFile = fileStore.createFile(data); inputFile.setId(zipPath + pathMerger + tempFileName); inputFile.put("path", zipPath); inputFile.setFilename(tempFileName); inputFile.save(); } BasicDBObject toFindAllEmptyFilesInFolder = new BasicDBObject(); toFindAllEmptyFilesInFolder.put("$or", pathOfChildrenEmptyFolders); DBCursor allFolders = collection.find(toFindAllEmptyFilesInFolder); while (allFolders.hasNext()) { DBObject temp = allFolders.next(); if (temp.get("folder").toString().equals("1")) { String tempPath = temp.get("parent").toString(); tempPath = tempPath.replaceFirst(parentPath + pathMerger + folderName, parentPath + pathMerger + newName); BasicDBObject updocument = new BasicDBObject(); updocument.put("_id", tempPath + pathMerger + temp.get("name")); updocument.put("folder", "1"); updocument.put("name", temp.get("name")); updocument.put("parent", tempPath); updocument.put("type", "1"); collection.insert(updocument); } } return true; } else { return false; } } finally { mymongo.closeConnection(); } }
From source file:com.fileoperations.RenameFolder.java
public Boolean forSingleFile() { try {/*from w w w. jav a2 s . com*/ if (oldName.contains(".")) { BasicDBObject query = new BasicDBObject(); query.put("_id", parentPath + pathMerger + oldName); DBCursor cursor = collection.find(query); if (cursor.hasNext()) { DBObject renameFile = cursor.next(); BasicDBObject checknewquery = new BasicDBObject(); checknewquery.put("_id", parentPath + pathMerger + newName + renameFile.get("type").toString()); DBCursor tempCursor = collection.find(checknewquery); if (tempCursor.hasNext()) { return false; } GridFS file = new GridFS(mymongo.getDB(), userCollectionName); InputStream data = file.findOne(query).getInputStream(); BasicDBObject document = new BasicDBObject(); document.append("_id", parentPath + pathMerger + newName + renameFile.get("type").toString()); document.append("folder", "0"); document.append("parent", parentPath); document.append("name", newName + renameFile.get("type").toString()); document.append("type", renameFile.get("type").toString()); collection.insert(document); GridFSInputFile inputFile = file.createFile(data); inputFile.setId(parentPath + pathMerger + newName + renameFile.get("type").toString()); inputFile.put("path", parentPath); inputFile.setFilename(newName + renameFile.get("type").toString()); inputFile.save(); file.remove(file.findOne(query)); collection.remove(renameFile); return true; } else { return false; } } else { return false; } } finally { mymongo.closeConnection(); } }
From source file:com.hangum.tadpole.mongodb.core.test.MongoTestGridFS.java
License:Open Source License
private static void getImage(DB db) throws Exception { String newFileName = "mkyong-java-image"; GridFS gfsPhoto = new GridFS(db, "photo"); GridFSDBFile imageForOutput = gfsPhoto.findOne(newFileName); System.out.println(imageForOutput); }
From source file:com.ikanow.aleph2.management_db.mongodb.services.MongoDbManagementDbService.java
License:Apache License
@SuppressWarnings("unchecked") public <T> Optional<T> getUnderlyingPlatformDriver(Class<T> driver_class, Optional<String> driver_options) { if (DBCollection.class == driver_class) { if (!driver_options.isPresent()) { throw new RuntimeException("If requesting DB collection, need to specify db_name.coll_name"); }//from w w w .j a v a 2s .c o m final String[] db_coll = driver_options.get().split("[.]", 2); if (2 != db_coll.length) { throw new RuntimeException( "If requesting DB collection, need to specify db_name.coll_name: " + driver_options.get()); } return (Optional<T>) Optional.of(_crud_factory.getMongoDbCollection(db_coll[0], db_coll[1])); } if (GridFS.class == driver_class) { if (!driver_options.isPresent()) { throw new RuntimeException("If requesting GridFS, need to specify db_name.coll_name"); } final String[] db_coll = driver_options.get().split("[.]", 2); if (2 != db_coll.length) { throw new RuntimeException( "If requesting GridFS, need to specify db_name.coll_name: " + driver_options.get()); } return (Optional<T>) Optional.of(new GridFS(_crud_factory.getMongoDb(db_coll[0]), db_coll[1])); } else if (DB.class == driver_class) { if (!driver_options.isPresent()) { throw new RuntimeException("If requesting DB, need to specify db_name"); } return (Optional<T>) Optional.of(_crud_factory.getMongoDb(driver_options.get())); } else if (ICrudService.class == driver_class) { if (!driver_options.isPresent()) { throw new RuntimeException( "If requesting a CRUD service, need to specify db_name.coll_name[/fully.qualified.bean.class]"); } final String[] dbcoll_clazz = driver_options.get().split("[/]", 2); final String[] db_coll = dbcoll_clazz[0].split("[.]", 2); if (2 != db_coll.length) { throw new RuntimeException( "If requesting a CRUD service, need to specify db_name.coll_name[/fully.qualified.bean.class]: " + driver_options.get()); } try { if (2 != dbcoll_clazz.length) { // returns the JSON version return (Optional<T>) Optional.of(_crud_factory.getMongoDbCrudService(JsonNode.class, Object.class, _crud_factory.getMongoDbCollection(db_coll[0], db_coll[1]), Optional.empty(), Optional.empty(), Optional.empty())); } else { return (Optional<T>) Optional .of(_crud_factory.getMongoDbCrudService(Class.forName(dbcoll_clazz[1]), Object.class, _crud_factory.getMongoDbCollection(db_coll[0], db_coll[1]), Optional.empty(), Optional.empty(), Optional.empty())); } } catch (Exception e) { throw new RuntimeException( "Requesting CRUD service, specified invalid class name: " + driver_options.get()); } } return Optional.empty(); }
From source file:com.imaginea.mongodb.services.GridFSServiceImpl.java
License:Apache License
/** * Service implementation for creating GridFS store in the specified database. * * @param dbName Name of Database//from w w w . j av a 2 s . co m * @param bucketName Name of GridFS Bucket * @returns Status message. */ public String createStore(String dbName, String bucketName) throws EmptyDatabaseNameException, EmptyCollectionNameException { mongoInstance = mongoInstanceProvider.getMongoInstance(); if (dbName == null) { throw new EmptyDatabaseNameException("Database Name Is Null"); } if (dbName.equals("")) { throw new EmptyDatabaseNameException("Database Name Empty"); } if (bucketName == null) { throw new EmptyCollectionNameException("Bucket name is null"); } if (bucketName.equals("")) { throw new EmptyCollectionNameException("Bucket Name Empty"); } new GridFS(mongoInstance.getDB(dbName), bucketName); return "Bucket '" + bucketName + "' successfully added to" + dbName + "'s GridFS"; }
From source file:com.imaginea.mongodb.services.GridFSServiceImpl.java
License:Apache License
/** * Service implementation for getting the list of files stored in GridFS of specified database. * * @param dbName Name of Database/*from w ww . j ava2 s. c o m*/ * @param bucketName Name of GridFS Bucket * @returns JSON representation of list of all files as a String. */ public ArrayList<DBObject> getFileList(String dbName, String bucketName) throws ValidationException, DatabaseException, CollectionException { mongoInstance = mongoInstanceProvider.getMongoInstance(); if (dbName == null) { throw new EmptyDatabaseNameException("Database Name Is Null"); } if (dbName.equals("")) { throw new EmptyDatabaseNameException("Database Name Empty"); } ArrayList<DBObject> fileList = new ArrayList<DBObject>(); try { if (!mongoInstance.getDatabaseNames().contains(dbName)) { throw new UndefinedDatabaseException( "Database with dbName [ " + dbName + "] does not exist"); } GridFS gridFS = new GridFS(mongoInstance.getDB(dbName), bucketName); Field field = GridFS.class.getDeclaredField("_filesCollection"); field.setAccessible(true); DBCollection filesCollection = (DBCollection) field.get(gridFS); DBCursor cursor = filesCollection.find().sort(new BasicDBObject("uploadDate", -1)); Iterator<DBObject> it = cursor.iterator(); while (it.hasNext()) { fileList.add(it.next()); } } catch (Exception m) { CollectionException e = new CollectionException(ErrorCodes.GET_COLLECTION_LIST_EXCEPTION, "GET_FILES_LIST_EXCEPTION", m.getCause()); throw e; } return fileList; }