Class GridFsTemplate

java.lang.Object
org.springframework.data.mongodb.gridfs.GridFsTemplate
All Implemented Interfaces:
org.springframework.core.io.ResourceLoader, org.springframework.core.io.support.ResourcePatternResolver, GridFsOperations

public class GridFsTemplate extends Object implements GridFsOperations, org.springframework.core.io.support.ResourcePatternResolver
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
  • Constructor Details

  • Method Details

    • store

      public org.bson.types.ObjectId store(InputStream content, @Nullable String filename, @Nullable String contentType, @Nullable Object metadata)
      Description copied from interface: GridFsOperations
      Stores 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:
      store in interface GridFsOperations
      Parameters:
      content - must not be null.
      filename - must not be null or empty.
      contentType - can be null.
      metadata - can be null
      Returns:
      the ObjectId of the GridFSFile just created.
    • store

      public <T> T store(GridFsObject<T,InputStream> upload)
      Description copied from interface: GridFsOperations
      Stores the given 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.
      Specified by:
      store in interface GridFsOperations
      Type Parameters:
      T - id type of the underlying GridFSFile
      Parameters:
      upload - the GridFsObject (most likely a GridFsUpload) to be stored.
      Returns:
      the id of the stored file. Either an auto created value or GridFsObject.getFileId(), but never null.
    • find

      public com.mongodb.client.gridfs.GridFSFindIterable find(Query query)
      Description copied from interface: GridFsOperations
      Returns all files matching the given query. Note, that currently Sort criterias defined at the Query will not be regarded as MongoDB does not support ordering for GridFS file access.
      Specified by:
      find in interface GridFsOperations
      Parameters:
      query - must not be null.
      Returns:
      GridFSFindIterable to obtain results from. Eg. by calling MongoIterable.into(java.util.Collection).
      See Also:
    • findOne

      public com.mongodb.client.gridfs.model.GridFSFile findOne(Query query)
      Description copied from interface: GridFsOperations
      Returns a single GridFSFile matching the given query or null in case no file matches.
      Specified by:
      findOne in interface GridFsOperations
      Parameters:
      query - must not be null.
      Returns:
      can be null.
    • delete

      public void delete(Query query)
      Description copied from interface: GridFsOperations
      Deletes all files matching the given Query.
      Specified by:
      delete in interface GridFsOperations
      Parameters:
      query - must not be null.
    • getClassLoader

      public ClassLoader getClassLoader()
      Specified by:
      getClassLoader in interface org.springframework.core.io.ResourceLoader
    • getResource

      public GridFsResource getResource(String location)
      Description copied from interface: GridFsOperations
      Returns the GridFsResource with the given file name.
      Specified by:
      getResource in interface GridFsOperations
      Specified by:
      getResource in interface org.springframework.core.io.ResourceLoader
      Parameters:
      location - must not be null.
      Returns:
      the resource. Use Resource.exists() to check if the returned GridFsResource is actually present.
      See Also:
      • ResourceLoader.getResource(String)
    • getResource

      public GridFsResource getResource(com.mongodb.client.gridfs.model.GridFSFile file)
      Description copied from interface: GridFsOperations
      Returns the GridFsResource for a GridFSFile.
      Specified by:
      getResource in interface GridFsOperations
      Parameters:
      file - must not be null.
      Returns:
      the resource for the file.
    • getResources

      public GridFsResource[] getResources(String locationPattern)
      Description copied from interface: GridFsOperations
      Returns all GridFsResources matching the given file name pattern.
      Specified by:
      getResources in interface GridFsOperations
      Specified by:
      getResources in interface org.springframework.core.io.support.ResourcePatternResolver
      Parameters:
      locationPattern - must not be null.
      Returns:
      an empty array if none found.
      See Also:
      • ResourcePatternResolver.getResources(String)
    • getMappedQuery

      protected org.bson.Document getMappedQuery(org.bson.Document query)
      Parameters:
      query - pass the given query though a QueryMapper to apply type conversion.
      Returns:
      never null.
    • computeUploadOptionsFor

      protected com.mongodb.client.gridfs.model.GridFSUploadOptions computeUploadOptionsFor(@Nullable String contentType, @Nullable org.bson.Document metadata)
      Compute the GridFSUploadOptions to be used from the given contentType and metadata Document.
      Parameters:
      contentType - can be null.
      metadata - can be null
      Returns:
      never null.
    • toDocument

      protected org.bson.Document toDocument(@Nullable Object value)
      Convert a given value into a Document.
      Parameters:
      value - can be null.
      Returns:
      an empty Document if the source value is null.