/** * Create a new GridFS bucket with a custom bucket name * * @param database the database instance to use with GridFS * @param bucketName the custom bucket name to use * @return the GridFSBucket */ public static GridFSBucket create(final MongoDatabase database, final String bucketName) { return new GridFSBucketImpl(database, bucketName); }
private void checkCreateIndex(@Nullable final ClientSession clientSession) { if (!checkedIndexes) { if (collectionIsEmpty(clientSession, filesCollection.withDocumentClass(Document.class).withReadPreference(primary()))) { Document filesIndex = new Document("filename", 1).append("uploadDate", 1); if (!hasIndex(clientSession, filesCollection.withReadPreference(primary()), filesIndex)) { createIndex(clientSession, filesCollection, filesIndex, new IndexOptions()); } Document chunksIndex = new Document("files_id", 1).append("n", 1); if (!hasIndex(clientSession, chunksCollection.withReadPreference(primary()), chunksIndex)) { createIndex(clientSession, chunksCollection, chunksIndex, new IndexOptions().unique(true)); } } checkedIndexes = true; } }
private GridFSFile getFileInfoById(@Nullable final ClientSession clientSession, final BsonValue id) { notNull("id", id); GridFSFile fileInfo = createFindIterable(clientSession, new Document("_id", id)).first(); if (fileInfo == null) { throw new MongoGridFSException(format("No file found with the id: %s", id)); } return fileInfo; }
@Override public GridFSDownloadStream openDownloadStream(final BsonValue id) { return createGridFSDownloadStream(null, getFileInfoById(null, id)); }
@Override public GridFSDownloadStream openDownloadStream(final String filename, final GridFSDownloadOptions options) { return createGridFSDownloadStream(null, getFileByName(null, filename, options)); }
@Override public void downloadToStream(final String filename, final OutputStream destination, final GridFSDownloadOptions options) { downloadToStream(openDownloadStream(filename, options), destination); }
@Override public GridFSFindIterable find() { return createGridFSFindIterable(null, null); }
@Override public void downloadToStream(final ClientSession clientSession, final ObjectId id, final OutputStream destination) { downloadToStream(clientSession, new BsonObjectId(id), destination); }
@Override public GridFSUploadStream openUploadStream(final BsonValue id, final String filename, final GridFSUploadOptions options) { return createGridFSUploadStream(null, id, filename, options); }
@Override public void delete(final ClientSession clientSession, final ObjectId id) { delete(clientSession, new BsonObjectId(id)); }
private GridFSUploadStream createGridFSUploadStream(@Nullable final ClientSession clientSession, final BsonValue id, final String filename, final GridFSUploadOptions options) { notNull("options", options); Integer chunkSizeBytes = options.getChunkSizeBytes(); int chunkSize = chunkSizeBytes == null ? this.chunkSizeBytes : chunkSizeBytes; checkCreateIndex(clientSession); return new GridFSUploadStreamImpl(clientSession, filesCollection, chunksCollection, id, filename, chunkSize, disableMD5, options.getMetadata()); }
@Override public void downloadToStream(final BsonValue id, final OutputStream destination) { downloadToStream(openDownloadStream(id), destination); }
@Override public GridFSDownloadStream openDownloadStream(final ClientSession clientSession, final BsonValue id) { notNull("clientSession", clientSession); return createGridFSDownloadStream(clientSession, getFileInfoById(clientSession, id)); }
@Override public GridFSDownloadStream openDownloadStream(final ClientSession clientSession, final String filename, final GridFSDownloadOptions options) { notNull("clientSession", clientSession); return createGridFSDownloadStream(clientSession, getFileByName(clientSession, filename, options)); }
@Override public GridFSFindIterable find(final Bson filter) { notNull("filter", filter); return createGridFSFindIterable(null, filter); }
@Override public void downloadToStream(final String filename, final OutputStream destination) { downloadToStream(filename, destination, new GridFSDownloadOptions()); }
@Override public GridFSUploadStream openUploadStream(final ClientSession clientSession, final BsonValue id, final String filename, final GridFSUploadOptions options) { notNull("clientSession", clientSession); return createGridFSUploadStream(clientSession, id, filename, options); }
@Override public void delete(final ObjectId id) { delete(new BsonObjectId(id)); }
private GridFSUploadStream createGridFSUploadStream(@Nullable final ClientSession clientSession, final BsonValue id, final String filename, final GridFSUploadOptions options) { notNull("options", options); Integer chunkSizeBytes = options.getChunkSizeBytes(); int chunkSize = chunkSizeBytes == null ? this.chunkSizeBytes : chunkSizeBytes; checkCreateIndex(clientSession); return new GridFSUploadStreamImpl(clientSession, filesCollection, chunksCollection, id, filename, chunkSize, disableMD5, options.getMetadata()); }
@Override public void downloadToStream(final ClientSession clientSession, final BsonValue id, final OutputStream destination) { notNull("clientSession", clientSession); downloadToStream(openDownloadStream(clientSession, id), destination); }