public class GridFsTemplate extends Object implements GridFsOperations, ResourcePatternResolver
GridFsOperations
implementation to store content into MongoDB GridFS.CLASSPATH_ALL_URL_PREFIX
CLASSPATH_URL_PREFIX
Constructor and Description |
---|
GridFsTemplate(MongoDatabaseFactory dbFactory,
MongoConverter converter)
|
GridFsTemplate(MongoDatabaseFactory dbFactory,
MongoConverter converter,
String bucket)
|
Modifier and Type | Method and Description |
---|---|
protected com.mongodb.client.gridfs.model.GridFSUploadOptions |
computeUploadOptionsFor(String contentType,
org.bson.Document metadata)
Compute the
GridFSUploadOptions to be used from the given contentType and metadata
Document . |
void |
delete(Query query)
Deletes all files matching the given
Query . |
com.mongodb.client.gridfs.GridFSFindIterable |
find(Query query)
Returns all files matching the given query.
|
com.mongodb.client.gridfs.model.GridFSFile |
findOne(Query query)
Returns a single
GridFSFile matching the given query or null in
case no file matches. |
ClassLoader |
getClassLoader() |
protected org.bson.Document |
getMappedQuery(org.bson.Document query) |
GridFsResource |
getResource(com.mongodb.client.gridfs.model.GridFSFile file)
Returns the
GridFsResource for a GridFSFile . |
GridFsResource |
getResource(String location)
Returns the
GridFsResource with the given file name. |
GridFsResource[] |
getResources(String locationPattern)
Returns all
GridFsResource s matching the given file name pattern. |
<T> T |
store(GridFsObject<T,InputStream> upload)
|
org.bson.types.ObjectId |
store(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.
|
protected org.bson.Document |
toDocument(Object value)
Convert a given value into a
Document . |
public GridFsTemplate(MongoDatabaseFactory dbFactory, MongoConverter converter)
dbFactory
- must not be null.converter
- must not be null.public GridFsTemplate(MongoDatabaseFactory dbFactory, MongoConverter converter, @Nullable String bucket)
dbFactory
- must not be null.converter
- must not be null.bucket
- can be null.public org.bson.types.ObjectId store(InputStream content, @Nullable String filename, @Nullable String contentType, @Nullable Object metadata)
GridFsOperations
store
in interface GridFsOperations
content
- must not be null.filename
- must not be null or empty.contentType
- can be null.metadata
- can be nullObjectId
of the GridFSFile
just created.public <T> T store(GridFsObject<T,InputStream> upload)
GridFsOperations
GridFsObject
, likely a GridFsUpload
, into into a file with given
name
. If the GridFsObject.getFileId()
is set, the file will be stored
with that id, otherwise the server auto creates a new id. store
in interface GridFsOperations
T
- id type of the underlying GridFSFile
upload
- the GridFsObject
(most likely a GridFsUpload
) to be stored.GridFsObject.getFileId()
, but never
null.public com.mongodb.client.gridfs.GridFSFindIterable find(Query query)
GridFsOperations
Sort
criterias defined at the
Query
will not be regarded as MongoDB does not support ordering for GridFS file access.find
in interface GridFsOperations
query
- must not be null.GridFSFindIterable
to obtain results from. Eg. by calling
MongoIterable.into(java.util.Collection)
.public com.mongodb.client.gridfs.model.GridFSFile findOne(Query query)
GridFsOperations
GridFSFile
matching the given query or null in
case no file matches.findOne
in interface GridFsOperations
query
- must not be null.public void delete(Query query)
GridFsOperations
Query
.delete
in interface GridFsOperations
query
- must not be null.public ClassLoader getClassLoader()
getClassLoader
in interface ResourceLoader
public GridFsResource getResource(String location)
GridFsOperations
GridFsResource
with the given file name.getResource
in interface ResourceLoader
getResource
in interface GridFsOperations
location
- must not be null.Resource.exists()
to check if the returned
GridFsResource
is actually present.ResourceLoader.getResource(String)
public GridFsResource getResource(com.mongodb.client.gridfs.model.GridFSFile file)
GridFsOperations
GridFsResource
for a GridFSFile
.getResource
in interface GridFsOperations
file
- must not be null.public GridFsResource[] getResources(String locationPattern)
GridFsOperations
GridFsResource
s matching the given file name pattern.getResources
in interface ResourcePatternResolver
getResources
in interface GridFsOperations
locationPattern
- must not be null.ResourcePatternResolver.getResources(String)
protected org.bson.Document getMappedQuery(org.bson.Document query)
query
- pass the given query though a QueryMapper
to apply type conversion.protected com.mongodb.client.gridfs.model.GridFSUploadOptions computeUploadOptionsFor(@Nullable String contentType, @Nullable org.bson.Document metadata)
GridFSUploadOptions
to be used from the given contentType and metadata
Document
.contentType
- can be null.metadata
- can be nullCopyright © 2011–2022 Pivotal Software, Inc.. All rights reserved.