List of usage examples for com.mongodb.gridfs GridFS createFile
public GridFSInputFile createFile(final String filename)
From source file:com.cyslab.craftvm.rest.mongo.GridfsServlet.java
License:GNU General Public License
@Override protected void doPut(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { log.trace("doPut()"); if (!can_write(req)) { res.sendError(SC_UNAUTHORIZED);// w w w .ja v a2 s.com return; } InputStream tmp = req.getInputStream(); InputStream is = new BufferedInputStream(tmp); String db_name = req.getParameter("dbname"); String bucket_name = req.getParameter("bucketname"); if (db_name == null || bucket_name == null) { String names[] = req2mongonames(req); if (names != null) { db_name = names[0]; bucket_name = names[1]; } if (db_name == null) { error(res, SC_BAD_REQUEST, Status.get("param name missing")); return; } } if (bucket_name == null) bucket_name = "fs"; String file_name = req.getParameter("filename"); if (file_name == null) { error(res, SC_BAD_REQUEST, Status.get("param name missing")); return; } DB db = mongo.getDB(db_name); String fs_cache_key = db_name + bucket_name; GridFS fs = fs_cache.get(fs_cache_key); if (fs == null) { fs = new GridFS(db, bucket_name); fs_cache.put(fs_cache_key, fs); } GridFSDBFile db_file_old = fs.findOne(file_name); if (db_file_old != null) { error(res, SC_BAD_REQUEST, Status.get("file already exists, use POST")); return; } String ct = req.getContentType(); GridFSInputFile db_file = fs.createFile(file_name); if (ct != null) db_file.setContentType(ct); OutputStream os = db_file.getOutputStream(); final int len = 4096; byte data[] = new byte[len]; int n = 0, bytesno = 0; while ((n = is.read(data, 0, len)) > 0) { os.write(data, 0, n); bytesno += n; } os.flush(); os.close(); if (is != null) is.close(); out_json(req, Status.OK); }
From source file:com.englishtown.integration.java.IntegrationTestHelper.java
License:Open Source License
public static String createFile(JsonObject config, String bucket) { GridFS gridFS = IntegrationTestHelper.getGridFS(config, bucket); GridFSInputFile inputFile = gridFS .createFile(IntegrationTestHelper.class.getResourceAsStream("/EF_Labs_ENG_logo.JPG")); inputFile.setContentType(DEFAULT_CONTENT_TYPE); inputFile.setFilename(DEFAULT_FILENAME); inputFile.setChunkSize(DEFAULT_CHUNK_SIZE); inputFile.setMetaData(DEFAULT_METADATA); inputFile.save();/*w w w. j a va 2 s . c om*/ return inputFile.getId().toString(); }
From source file:com.fileoperations.CopyClass.java
public Boolean forSingleFile() { try {//from w ww . java 2 s . com 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 Boolean copyFolder(String newPath) throws IOException { try {//from w w w.j av 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 {/*from w w w . j a va2 s . co 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 ww w.ja v a2s. c om*/ 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.foodtruckdata.mongodb.UsersInput.java
private void storeFile(String filepath, String filetype, ObjectId truck_id) { try {//from ww w .jav a 2 s . c o m GridFS gridFS = new GridFS(mongoDB); InputStream file_stream = getFTPInputStream(filepath); StringWriter writer = new StringWriter(); Charset par = null; IOUtils.copy(file_stream, writer, par); GridFSInputFile in = gridFS.createFile(file_stream); in.setFilename(filepath); in.put("TruckID", truck_id); in.put("FileType", filetype); in.save(); } catch (IOException ex) { Logger.getLogger(UsersInput.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.hangum.tadpole.mongodb.core.query.MongoDBQuery.java
License:Open Source License
/** * insert gridfs// w w w.j av a2 s. co m * * @param userDB * @param strBucket * @param fileList * @throws Exception */ public static void insertGridFS(UserDBDAO userDB, String strBucket, List<String> fileList) throws Exception { DB mongoDb = findDB(userDB); GridFS gridFs = null; if ("".equals(strBucket)) gridFs = new GridFS(mongoDb); else gridFs = new GridFS(mongoDb, strBucket); for (String strFile : fileList) { String saveFileName = strFile.substring(strFile.lastIndexOf(File.separator) + 1); GridFSInputFile gfsFile = gridFs.createFile(new File(strFile)); gfsFile.setFilename(saveFileName); gfsFile.save(); } }
From source file:com.hangum.tadpole.mongodb.core.test.MongoTestGridFS.java
License:Open Source License
private static void saveImage(DB db) throws Exception { String newFileName = "currentop"; File imageFile = new File("c:/temp/currentop.png"); GridFS gfsPhoto = new GridFS(db); GridFSInputFile gfsFile = gfsPhoto.createFile(imageFile); gfsFile.setFilename(newFileName);/* www.j a v a 2 s . c om*/ gfsFile.save(); }
From source file:com.impetus.client.mongodb.DefaultMongoDBDataHandler.java
License:Apache License
/** * Creates the GFS Input file./* w w w.j ava 2 s . co m*/ * * @param gfs * the gfs * @param entity * the entity * @param f * the f * @return the grid fs input file */ private GridFSInputFile createGFSInputFile(GridFS gfs, Object entity, Field f) { Object obj = PropertyAccessorHelper.getObject(entity, f); GridFSInputFile gridFSInputFile = null; if (f.getType().isAssignableFrom(byte[].class)) gridFSInputFile = gfs.createFile((byte[]) obj); else if (f.getType().isAssignableFrom(File.class)) { try { gridFSInputFile = gfs.createFile((File) obj); } catch (IOException e) { log.error("Error while creating GridFS file for \"" + f.getName() + "\". Caused by: ", e); throw new KunderaException( "Error while creating GridFS file for \"" + f.getName() + "\". Caused by: ", e); } } else new UnsupportedOperationException(f.getType().getSimpleName() + " is unsupported Lob object"); return gridFSInputFile; }