Class GridFsRepositoryImpl

java.lang.Object
org.saidone.component.BaseComponent
org.saidone.repository.GridFsRepositoryImpl
All Implemented Interfaces:
GridFsRepository, org.springframework.beans.factory.Aware, org.springframework.context.ApplicationContextAware
Direct Known Subclasses:
EncryptedGridFsRepositoryImpl

@Repository @ConditionalOnExpression("${application.service.vault.encryption.enabled}.equals(false) and \'${application.service.vault.storage.impl}\'.equals(\'gridfs\')") public class GridFsRepositoryImpl extends BaseComponent implements GridFsRepository
Repository implementation for the management of files in MongoDB's GridFS without encryption support.

Implements the GridFsRepository interface, enabling file storage, retrieval, metadata updates and file deletion in the GridFS storage system. Utilizes Spring Data's GridFsTemplate, GridFsOperations, and MongoTemplate for interactions with MongoDB and GridFS collections.

On initialization, an ascending index is automatically created on the metadata.uuid field in the fs.files collection to improve query performance for operations that search files by UUID.

Supports the following main operations:

  • Store files with a name, content type and associated metadata.
  • Update the metadata of an existing file identified by a unique uuid.
  • Retrieve a single file from GridFS by its uuid, returning a GridFSFile.
  • Delete a file from GridFS using its uuid as key.
  • Provide file content streams via InputStream for efficient reading of file contents.
  • Compute cryptographic hash values of files using MongoDB commands, with the hash algorithm selectable at runtime.

This implementation is registered in the Spring context only when the property application.service.vault.encryption.enabled is set to false (or is missing) and application.service.vault.storage.impl equals "gridfs".

Extends BaseComponent to inherit standardized component lifecycle logging.

Thread safety is guaranteed by relying on the thread-safe beans of the Spring container for all MongoDB operations.

  • Constructor Details

    • GridFsRepositoryImpl

      public GridFsRepositoryImpl()
  • Method Details

    • init

      @PostConstruct public void init()
      Initializes the component by creating an index on the metadata.uuid field of the fs.files collection. This ensures fast lookups when retrieving files by UUID.

      If the index creation fails the component is shut down.

      Overrides:
      init in class BaseComponent
    • saveFile

      public void saveFile(InputStream inputStream, String fileName, String contentType, Map<String,String> metadata)
      Stores a new file in GridFS.
      Specified by:
      saveFile in interface GridFsRepository
      Parameters:
      inputStream - the stream containing the file content
      fileName - name of the file to store
      contentType - MIME type of the file
      metadata - additional metadata to associate with the file
    • updateFileMetadata

      public void updateFileMetadata(String uuid, Map<String,String> metadata)
      Updates the metadata document of a stored file.
      Specified by:
      updateFileMetadata in interface GridFsRepository
      Parameters:
      uuid - unique identifier of the file
      metadata - key/value pairs to merge into the existing metadata
    • findFileById

      public com.mongodb.client.gridfs.model.GridFSFile findFileById(String uuid)
      Retrieves a file descriptor by UUID.
      Specified by:
      findFileById in interface GridFsRepository
      Parameters:
      uuid - unique identifier of the file
      Returns:
      the matching GridFSFile or null if not found
    • deleteFileById

      public void deleteFileById(String uuid)
      Deletes a file and its metadata from GridFS by UUID.
      Specified by:
      deleteFileById in interface GridFsRepository
      Parameters:
      uuid - unique identifier of the file
    • getFileContent

      public InputStream getFileContent(com.mongodb.client.gridfs.model.GridFSFile file)
      Returns an input stream for the given GridFS file.
      Parameters:
      file - the file retrieved from GridFS
      Returns:
      input stream positioned at the beginning of the file or null if file is null
    • computeHash

      protected String computeHash(com.mongodb.client.gridfs.model.GridFSFile file, String algorithm)
      Computes the cryptographic hash of the provided GridFS file using the specified algorithm.
      Parameters:
      file - the file whose hash should be calculated
      algorithm - name of the hash algorithm supported by MongoDB
      Returns:
      the hexadecimal encoded hash value
    • computeHash

      public String computeHash(String uuid, String algorithm)
      Computes the hash of a file identified by its UUID.
      Specified by:
      computeHash in interface GridFsRepository
      Parameters:
      uuid - unique identifier of the file
      algorithm - name of the hash algorithm supported by MongoDB
      Returns:
      the hexadecimal encoded hash value