Class GridFsTemplate
java.lang.Object
org.springframework.data.mongodb.gridfs.GridFsTemplate
- All Implemented Interfaces:
ResourceLoader,ResourcePatternResolver,GridFsOperations
GridFsOperations implementation to store content into MongoDB GridFS.- Author:
- Oliver Gierke, Philipp Schneider, Thomas Darimont, Martin Baumgartner, Christoph Strobl, Mark Paluch, Hartmut Lang, Niklas Helge Hanft, Denis Zavedeev
-
Field Summary
Fields inherited from interface org.springframework.core.io.ResourceLoader
CLASSPATH_URL_PREFIXFields inherited from interface org.springframework.core.io.support.ResourcePatternResolver
CLASSPATH_ALL_URL_PREFIX -
Constructor Summary
ConstructorsConstructorDescriptionGridFsTemplate(MongoConverter converter, Supplier<com.mongodb.client.gridfs.GridFSBucket> gridFSBucket) Creates a newGridFsTemplateusing the givenMongoConverterandSupplierproviding the requiredGridFSBucket.GridFsTemplate(MongoDatabaseFactory dbFactory, MongoConverter converter) GridFsTemplate(MongoDatabaseFactory dbFactory, MongoConverter converter, String bucket) -
Method Summary
Modifier and TypeMethodDescriptionprotected com.mongodb.client.gridfs.model.GridFSUploadOptionscomputeUploadOptionsFor(String contentType, org.bson.Document metadata) Compute theGridFSUploadOptionsto be used from the given contentType and metadataDocument.voidDeletes all files matching the givenQuery.com.mongodb.client.gridfs.GridFSFindIterableReturns all files matching the given query.com.mongodb.client.gridfs.model.GridFSFileReturns a singleGridFSFilematching the given query or null in case no file matches.protected org.bson.DocumentgetMappedQuery(org.bson.Document query) getResource(com.mongodb.client.gridfs.model.GridFSFile file) Returns theGridFsResourcefor aGridFSFile.getResource(String location) Returns theGridFsResourcewith the given file name.getResources(String locationPattern) Returns allGridFsResources matching the given file name pattern.org.bson.types.ObjectIdstore(InputStream content, String filename, String contentType, Object metadata) Stores the given content into a file with the given name and content type using the given metadata.<T> Tstore(GridFsObject<T, InputStream> upload) protected org.bson.DocumenttoDocument(Object value) Convert a given value into aDocument.
-
Constructor Details
-
GridFsTemplate
Creates a newGridFsTemplateusing the givenMongoDatabaseFactoryandMongoConverter.Note that the
GridFSBucketis obtained only once fromMongoDatabase. UseGridFsTemplate(MongoConverter, Supplier)if you want to use different buckets from the same Template instance.- Parameters:
dbFactory- must not be null.converter- must not be null.
-
GridFsTemplate
public GridFsTemplate(MongoDatabaseFactory dbFactory, MongoConverter converter, @Nullable String bucket) Creates a newGridFsTemplateusing the givenMongoDatabaseFactoryandMongoConverter.Note that the
GridFSBucketis obtained only once fromMongoDatabase. UseGridFsTemplate(MongoConverter, Supplier)if you want to use different buckets from the same Template instance.- Parameters:
dbFactory- must not be null.converter- must not be null.bucket- can be null.
-
GridFsTemplate
public GridFsTemplate(MongoConverter converter, Supplier<com.mongodb.client.gridfs.GridFSBucket> gridFSBucket) Creates a newGridFsTemplateusing the givenMongoConverterandSupplierproviding the requiredGridFSBucket.- Parameters:
converter- must not be null.gridFSBucket- must not be null.- Since:
- 4.2
-
-
Method Details
-
store
public org.bson.types.ObjectId store(InputStream content, @Nullable String filename, @Nullable String contentType, @Nullable Object metadata) Description copied from interface:GridFsOperationsStores the given content into a file with the given name and content type using the given metadata. The metadata object will be marshalled before writing.- Specified by:
storein interfaceGridFsOperations- Parameters:
content- must not be null.filename- must not be null or empty.contentType- can be null.metadata- can be null- Returns:
- the
ObjectIdof theGridFSFilejust created.
-
store
Description copied from interface:GridFsOperationsStores the givenGridFsObject, likely aGridFsUpload, into into a file with givenname. If theGridFsObject.getFileId()is set, the file will be stored with that id, otherwise the server auto creates a new id.- Specified by:
storein interfaceGridFsOperations- Type Parameters:
T- id type of the underlyingGridFSFile- Parameters:
upload- theGridFsObject(most likely aGridFsUpload) to be stored.- Returns:
- the id of the stored file. Either an auto created value or
GridFsObject.getFileId(), but never null.
-
find
Description copied from interface:GridFsOperationsReturns all files matching the given query. Note, that currentlySortcriterias defined at theQuerywill not be regarded as MongoDB does not support ordering for GridFS file access.- Specified by:
findin interfaceGridFsOperations- Parameters:
query- must not be null.- Returns:
GridFSFindIterableto obtain results from. Eg. by callingMongoIterable.into(java.util.Collection).- See Also:
-
findOne
Description copied from interface:GridFsOperationsReturns a singleGridFSFilematching the given query or null in case no file matches.- Specified by:
findOnein interfaceGridFsOperations- Parameters:
query- must not be null.- Returns:
- can be null.
-
delete
Description copied from interface:GridFsOperationsDeletes all files matching the givenQuery.- Specified by:
deletein interfaceGridFsOperations- Parameters:
query- must not be null.
-
getClassLoader
- Specified by:
getClassLoaderin interfaceResourceLoader
-
getResource
Description copied from interface:GridFsOperationsReturns theGridFsResourcewith the given file name.- Specified by:
getResourcein interfaceGridFsOperations- Specified by:
getResourcein interfaceResourceLoader- Parameters:
location- must not be null.- Returns:
- the resource. Use
Resource.exists()to check if the returnedGridFsResourceis actually present. - See Also:
-
getResource
Description copied from interface:GridFsOperationsReturns theGridFsResourcefor aGridFSFile.- Specified by:
getResourcein interfaceGridFsOperations- Parameters:
file- must not be null.- Returns:
- the resource for the file.
-
getResources
Description copied from interface:GridFsOperationsReturns allGridFsResources matching the given file name pattern.- Specified by:
getResourcesin interfaceGridFsOperations- Specified by:
getResourcesin interfaceResourcePatternResolver- Parameters:
locationPattern- must not be null.- Returns:
- an empty array if none found.
- See Also:
-
getMappedQuery
protected org.bson.Document getMappedQuery(org.bson.Document query) - Parameters:
query- pass the given query though aQueryMapperto apply type conversion.- Returns:
- never null.
-
computeUploadOptionsFor
protected com.mongodb.client.gridfs.model.GridFSUploadOptions computeUploadOptionsFor(@Nullable String contentType, @Nullable org.bson.Document metadata) Compute theGridFSUploadOptionsto be used from the given contentType and metadataDocument.- Parameters:
contentType- can be null.metadata- can be null- Returns:
- never null.
-
toDocument
Convert a given value into aDocument.- Parameters:
value- can be null.- Returns:
- an empty
Documentif the source value is null.
-