List of usage examples for com.mongodb.gridfs GridFS GridFS
public GridFS(final DB db, final String bucket)
From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java
License:EUPL
/** * Lists all the versions of the specified file. * @param namespace - (optional) name space to be searched for files. When nothing specified, the default bucket is used * @param filename - filename to be searched for in the database * @param sortCriteria - objects in the collection are sorted with this criteria * @param start - starting index/*from w w w . jav a2 s. c om*/ * @param size - maximum number of objects returned * @param count - (optional) is updated with the number of objects in the database * @return a view of the versions stored of the specified file that contains the specified range. */ public List<GridFSDBFile> listFileVersions(final @Nullable String namespace, final String filename, final DBObject sortCriteria, final int start, final int size, final @Nullable MutableLong count) { checkArgument(isNotBlank(filename), "Uninitialized or invalid filename"); final List<GridFSDBFile> list = newArrayList(); final DB db = client().getDB(CONFIG_MANAGER.getDbName()); final GridFS gfsNs = isNotBlank(namespace) ? new GridFS(db, namespace.trim()) : new GridFS(db); final DBCursor cursor = gfsNs.getFileList(new BasicDBObject("filename", filename.trim()), sortCriteria); cursor.skip(start).limit(size); try { while (cursor.hasNext()) { list.add((GridFSDBFile) cursor.next()); } } finally { cursor.close(); } if (count != null) { count.setValue(cursor.count()); } return list; }
From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java
License:EUPL
/** * Removes a file (with all its versions) from the specified name space. * @param namespace - (optional) name space where the file was stored under. When nothing specified, the default bucket is used * @param filename - filename to be removed from the database *//*from w w w . j av a 2 s .c o m*/ public void removeFile(final @Nullable String namespace, final String filename) { checkArgument(isNotBlank(filename), "Uninitialized or invalid filename"); final DB db = client().getDB(CONFIG_MANAGER.getDbName()); final GridFS gfsNs = isNotBlank(namespace) ? new GridFS(db, namespace.trim()) : new GridFS(db); gfsNs.remove(filename); }
From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java
License:EUPL
/** * Deletes the latest version from the database. When a previous version exists for the file in the specified namespace, the latest * uploaded version will be the new latest version. * @param namespace - (optional) name space where the file was stored under. When nothing specified, the default bucket is used * @param filename - filename to be removed from the database */// w w w. j av a 2 s .c om public void undoLatestVersion(final @Nullable String namespace, final String filename) { checkArgument(isNotBlank(filename), "Uninitialized or invalid filename"); final String filename2 = filename.trim(); final DB db = client().getDB(CONFIG_MANAGER.getDbName()); final GridFS gfsNs = isNotBlank(namespace) ? new GridFS(db, namespace.trim()) : new GridFS(db); try { // remove latest version from the database gfsNs.remove(new BasicDBObject(FILE_VERSION_PROP, filename2)); } finally { // enforce versioning property by always restoring the latest version in the database restoreLatestVersion(gfsNs, filename2); } }
From source file:fr.wseduc.gridfs.GridFSPersistor.java
License:Apache License
private void writeTo(Message<JsonObject> message) { String path = message.body().getString("path"); if (path == null) { sendError(message, "Invalid output path."); return;//w w w.j av a 2s .com } JsonObject query = message.body().getObject("query"); if (query == null) { sendError(message, "Invalid query."); return; } JsonObject alias = message.body().getObject("alias", new JsonObject()); boolean renameIfExists = message.body().getBoolean("rename-if-exists", true); GridFS fs = new GridFS(db, bucket); try { List<GridFSDBFile> files = fs.find(jsonToDBObject(query)); FileSystem fileSystem = vertx.fileSystem(); for (GridFSDBFile f : files) { String a = alias.getString(f.getId().toString()); String p = path + File.separator + ((a != null) ? a : f.getFilename()); if (renameIfExists && fileSystem.existsSync(p)) { p = path + File.separator + f.getId().toString() + "_" + ((a != null) ? a : f.getFilename()); } f.writeTo(p); } sendOK(message, new JsonObject().putNumber("number", files.size())); } catch (IOException | MongoException e) { logger.error(e.getMessage(), e); sendError(message, e.getMessage()); } }
From source file:fr.wseduc.gridfs.GridFSPersistor.java
License:Apache License
private void getFile(Message<Buffer> message, JsonObject json) { JsonObject query = json.getObject("query"); if (query == null) { return;//from w w w . ja va 2s . co m } GridFS fs = new GridFS(db, bucket); try { GridFSDBFile f = fs.findOne(jsonToDBObject(query)); if (f == null) { replyError(message, "File not found with query : " + query.encode()); return; } ByteArrayOutputStream os = new ByteArrayOutputStream(); f.writeTo(os); message.reply(new Buffer(os.toByteArray())); } catch (IOException | MongoException e) { container.logger().error(e.getMessage(), e); JsonObject j = new JsonObject().putString("status", "error").putString("message", e.getMessage()); try { message.reply(new Buffer(j.encode().getBytes("UTF-8"))); } catch (UnsupportedEncodingException e1) { container.logger().error(e1.getMessage(), e1); } } }
From source file:fr.wseduc.gridfs.GridFSPersistor.java
License:Apache License
private void persistFile(Message<Buffer> message, byte[] data, JsonObject header) { GridFS fs = new GridFS(db, bucket); GridFSInputFile f = fs.createFile(data); String id = header.getString("_id"); if (id == null || id.trim().isEmpty()) { id = UUID.randomUUID().toString(); }/*from w w w.j a va 2 s.c om*/ f.setId(id); f.setContentType(header.getString("content-type")); f.setFilename(header.getString("filename")); f.save(); JsonObject reply = new JsonObject(); reply.putString("_id", id); replyOK(message, reply); }
From source file:fr.wseduc.gridfs.GridFSPersistor.java
License:Apache License
private void copyFile(Message<Buffer> message, JsonObject json) { JsonObject query = json.getObject("query"); if (query == null) { return;// w ww . j a v a 2 s .c o m } GridFS fs = new GridFS(db, bucket); try { GridFSDBFile f = fs.findOne(jsonToDBObject(query)); if (f == null) { replyError(message, "File not found with query : " + query.encode()); return; } ByteArrayOutputStream os = new ByteArrayOutputStream(); f.writeTo(os); JsonObject j = new JsonObject(); j.putString("content-type", f.getContentType()); j.putString("filename", f.getFilename()); persistFile(message, os.toByteArray(), j); } catch (IOException | MongoException e) { replyError(message, e.getMessage()); } }
From source file:fr.wseduc.gridfs.GridFSPersistor.java
License:Apache License
private void removeFile(Message<Buffer> message, JsonObject json) { JsonObject query = json.getObject("query"); if (query == null) { return;/* w w w. ja va 2 s.co m*/ } GridFS fs = new GridFS(db, bucket); try { fs.remove(jsonToDBObject(query)); replyOK(message, null); } catch (MongoException e) { replyError(message, e.getMessage()); } }
From source file:fr.wseduc.resizer.GridFsFileAccess.java
License:Apache License
@Override public void read(String src, Handler<ImageFile> handler) { String[] path = parsePath(src); if (path == null || path.length != 2) { handler.handle(null);//from w ww . ja va2s .co m return; } GridFS fs = new GridFS(db, path[0]); GridFSDBFile f = fs.findOne(pathToDbObject(path[1])); if (f != null) { handler.handle(new ImageFile(f.getInputStream(), f.getFilename(), f.getContentType())); } else { handler.handle(null); } }
From source file:fr.wseduc.resizer.GridFsFileAccess.java
License:Apache License
@Override public void write(String dest, ImageFile img, Handler<String> handler) { String[] path = parsePath(dest); if (path == null || path.length < 1) { handler.handle(null);//www . j ava 2 s . c om return; } String id; if (path.length == 2 && path[1] != null && !path[1].trim().isEmpty()) { id = path[1]; } else { id = UUID.randomUUID().toString(); } GridFS fs = new GridFS(db, path[0]); try { saveFile(img, id, fs); } catch (DuplicateKeyException e) { fs.remove(new BasicDBObject("_id", id)); saveFile(img, id, fs); } handler.handle(id); }