List of usage examples for com.mongodb.client.gridfs GridFSBucket find
GridFSFindIterable find(ClientSession clientSession);
From source file:org.restheart.db.GridFsDAO.java
License:Open Source License
@Override public OperationResult deleteFile(final Database db, final String dbName, final String bucketName, final BsonObjectId fileId, final String requestEtag, final boolean checkEtag) { final String bucket = extractBucketName(bucketName); GridFSBucket gridFSBucket = GridFSBuckets.create(db.getDatabase(dbName), bucket); GridFSFile file = gridFSBucket.find(eq("_id", fileId)).limit(1).iterator().tryNext(); if (file == null) { return new OperationResult(HttpStatus.SC_NOT_FOUND); } else if (checkEtag) { Object oldEtag = file.getMetadata().get("_etag"); if (oldEtag != null) { if (requestEtag == null) { return new OperationResult(HttpStatus.SC_CONFLICT, oldEtag); } else if (!Objects.equals(oldEtag.toString(), requestEtag)) { return new OperationResult(HttpStatus.SC_PRECONDITION_FAILED, oldEtag); }// w w w . j a v a 2 s .c o m } } gridFSBucket.delete(fileId.asObjectId().getValue()); return new OperationResult(HttpStatus.SC_NO_CONTENT); }
From source file:org.restheart.handlers.files.GetFileBinaryHandler.java
License:Open Source License
@Override public void handleRequest(HttpServerExchange exchange, RequestContext context) throws Exception { if (context.isInError()) { next(exchange, context);/*from w ww.ja v a 2 s. co m*/ return; } LOGGER.trace("GET " + exchange.getRequestURL()); final String bucket = extractBucketName(context.getCollectionName()); GridFSBucket gridFSBucket = GridFSBuckets .create(MongoDBClientSingleton.getInstance().getClient().getDatabase(context.getDBName()), bucket); GridFSFile dbsfile = gridFSBucket.find(eq("_id", context.getDocumentId())).limit(1).iterator().tryNext(); if (dbsfile == null) { fileNotFound(context, exchange); } else if (!checkEtag(exchange, dbsfile)) { sendBinaryContent(gridFSBucket, dbsfile, exchange); } next(exchange, context); }