public void delete(Query query) { for (GridFSFile gridFSFile : find(query)) { getGridFs().delete(((BsonObjectId) gridFSFile.getId()).getValue()); } }
public void delete(Query query) { for (GridFSFile x : find(query)) { getGridFs().delete(((BsonObjectId) x.getId()).getValue()); } }
public void remove() { getBucket().delete(mongoFileId); ds = null; mongoFileId = null; }
private void deleteAllExcept(GridFSBucket bucket, String filename, ObjectId objectID) { List<ObjectId> idsToDelete = new ArrayList<>(); bucket.find(Filters.eq(FILENAME_TAG, filename)).forEach((Consumer<GridFSFile>) file -> idsToDelete.add(file.getObjectId())); LOGGER.debug("**** number of files to delete:" + idsToDelete.size()); idsToDelete.forEach(id -> { if (!id.equals(objectID)) { LOGGER.debug("**** delete:" + id); bucket.delete(id); } }); }
@Override public void removeBlob(BucketPath bucketPath) { LOGGER.debug("start removeBlob for " + bucketPath); GridFSBucket bucket = getGridFSBucket(bucketPath); checkBucketExists(bucket); String filename = bucketPath.getObjectHandle().getName(); List<ObjectId> ids = new ArrayList<>(); bucket.find(Filters.eq(FILENAME_TAG, filename)).forEach((Consumer<GridFSFile>) file -> ids.add(file.getObjectId())); ids.forEach(id -> bucket.delete(id)); LOGGER.debug("finished removeBlob for " + bucketPath); }
@Override public void deleteAssociatedDocuments(String uniqueId) { GridFSBucket gridFS = createGridFSConnection(); gridFS.find(new Document(ASSOCIATED_METADATA + "." + DOCUMENT_UNIQUE_ID_KEY, uniqueId)) .forEach((Block<com.mongodb.client.gridfs.model.GridFSFile>) gridFSFile -> gridFS.delete(gridFSFile.getObjectId())); }
@Override public void stop(boolean delete) { gridFSBucket.find(Filters.exists(String.format("%s.%s", METADATA_PROPERTY_METADATA, msKey), false)) // .forEach((Block<GridFSFile>) file -> { status.numBinariesGC += 1; status.sizeBinariesGC += file.getLength(); if (delete) { gridFSBucket.delete(file.getId()); } }); startTime = 0; } }
protected void saveDataToGridFS(byte[] data, String fileid) { Bson fileQuery = Filters.regex("filename", getId() + ".*"); try { final ArrayList<GridFSFile> es = new ArrayList<>(); gridFS.find(fileQuery).into(es); for (GridFSFile e : es) { gridFS.delete(e.getObjectId()); } } catch (Exception e) { log.error("failed to delete old gridfsfile", e); } gridFS.uploadFromStream(getId() + fileid, new ByteArrayInputStream(data)); }
protected void saveDataToGridFS(byte[] data, String fileid) { Bson fileQuery = Filters.regex("filename", getId() + ".*"); try { final ArrayList<GridFSFile> es = new ArrayList<>(); gridFS.find(fileQuery).into(es); for (GridFSFile e : es) { gridFS.delete(e.getObjectId()); } } catch (Exception e) { log.error("failed to delete old gridfsfile", e); } gridFS.uploadFromStream(getId() + fileid, new ByteArrayInputStream(data)); }
@Override public void deleteAssociatedDocument(String uniqueId, String fileName) { GridFSBucket gridFS = createGridFSConnection(); gridFS.find(new Document(ASSOCIATED_METADATA + "." + FILE_UNIQUE_ID_KEY, getGridFsId(uniqueId, fileName))) .forEach((Block<com.mongodb.client.gridfs.model.GridFSFile>) gridFSFile -> gridFS.delete(gridFSFile.getObjectId())); }
@Override public void removeBlobFolder(BucketDirectory bucketDirectory) { LOGGER.debug("start removeBlobFolder for " + bucketDirectory); if (bucketDirectory.getObjectHandle().getName() == null) { throw new StorageConnectionException("not a valid bucket directory " + bucketDirectory); } GridFSBucket bucket = getGridFSBucket(bucketDirectory); String directoryname = bucketDirectory.getObjectHandle().getName() + BucketPath.BUCKET_SEPARATOR; String pattern = "^" + directoryname + ".*"; GridFSFindIterable list = bucket.find(regex(FILENAME_TAG, pattern, "i")); list.forEach((Consumer<GridFSFile>) file -> bucket.delete(file.getObjectId())); LOGGER.debug("finished removeBlobFolder for " + bucketDirectory); }
gridFSBucket.delete(fileId); LOGGER.info("Succesfully deleted fileId {}", fileId); } catch (MongoGridFSException e) {
@Override protected Binary getBinary(InputStream in) throws IOException { try { // save the file to GridFS String inputName = "tmp-" + System.nanoTime(); ObjectId id = gridFSBucket.uploadFromStream(inputName, in); // now we know length and digest GridFSFile inputFile = gridFSBucket.find(Filters.eq(METADATA_PROPERTY_FILENAME, inputName)).first(); String digest = inputFile.getMD5(); // if the digest is already known then reuse it instead GridFSFile dbFile = gridFSBucket.find(Filters.eq(METADATA_PROPERTY_FILENAME, digest)).first(); if (dbFile == null) { // no existing file, set its filename as the digest gridFSBucket.rename(id, digest); } else { // file already existed, no need for the temporary one gridFSBucket.delete(id); } return new GridFSBinary(digest, blobProviderId); } finally { in.close(); } }
private void deleteExistingContent(String fieldName, Object documentId, GridFSBucket gridFSFilesBucket) { GridFSFindIterable results = gridFSFilesBucket.find( Filters.and( Filters.eq( "filename", fileName( fieldName, documentId ) ) ) ); try ( MongoCursor<GridFSFile> iterator = results.iterator() ) { while ( iterator.hasNext() ) { GridFSFile next = iterator.next(); gridFSFilesBucket.delete( next.getId() ); } } }
private void deleteExistingContent(String fieldName, Object documentId, GridFSBucket gridFSFilesBucket) { GridFSFindIterable results = gridFSFilesBucket.find( Filters.and( Filters.eq( "filename", fileName( fieldName, documentId ) ) ) ); try ( MongoCursor<GridFSFile> iterator = results.iterator() ) { while ( iterator.hasNext() ) { GridFSFile next = iterator.next(); gridFSFilesBucket.delete( next.getId() ); } } }