Class GridFsRepositoryImpl
- All Implemented Interfaces:
GridFsRepository,org.springframework.beans.factory.Aware,org.springframework.context.ApplicationContextAware
- Direct Known Subclasses:
EncryptedGridFsRepositoryImpl
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 aGridFSFile. - Delete a file from GridFS using its
uuidas key. - Provide file content streams via
InputStreamfor 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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected StringcomputeHash(com.mongodb.client.gridfs.model.GridFSFile file, String algorithm) Computes the cryptographic hash of the provided GridFS file using the specified algorithm.computeHash(String uuid, String algorithm) Computes the hash of a file identified by its UUID.voiddeleteFileById(String uuid) Deletes a file and its metadata from GridFS by UUID.com.mongodb.client.gridfs.model.GridFSFilefindFileById(String uuid) Retrieves a file descriptor by UUID.getFileContent(com.mongodb.client.gridfs.model.GridFSFile file) Returns an input stream for the given GridFS file.voidinit()Initializes the component by creating an index on themetadata.uuidfield of thefs.filescollection.voidStores a new file in GridFS.voidupdateFileMetadata(String uuid, Map<String, String> metadata) Updates the metadata document of a stored file.Methods inherited from class org.saidone.component.BaseComponent
setApplicationContext, shutDown, stop
-
Constructor Details
-
GridFsRepositoryImpl
public GridFsRepositoryImpl()
-
-
Method Details
-
init
@PostConstruct public void init()Initializes the component by creating an index on themetadata.uuidfield of thefs.filescollection. This ensures fast lookups when retrieving files by UUID.If the index creation fails the component is shut down.
- Overrides:
initin classBaseComponent
-
saveFile
public void saveFile(InputStream inputStream, String fileName, String contentType, Map<String, String> metadata) Stores a new file in GridFS.- Specified by:
saveFilein interfaceGridFsRepository- Parameters:
inputStream- the stream containing the file contentfileName- name of the file to storecontentType- MIME type of the filemetadata- additional metadata to associate with the file
-
updateFileMetadata
Updates the metadata document of a stored file.- Specified by:
updateFileMetadatain interfaceGridFsRepository- Parameters:
uuid- unique identifier of the filemetadata- key/value pairs to merge into the existing metadata
-
findFileById
Retrieves a file descriptor by UUID.- Specified by:
findFileByIdin interfaceGridFsRepository- Parameters:
uuid- unique identifier of the file- Returns:
- the matching
GridFSFileornullif not found
-
deleteFileById
Deletes a file and its metadata from GridFS by UUID.- Specified by:
deleteFileByIdin interfaceGridFsRepository- Parameters:
uuid- unique identifier of the file
-
getFileContent
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
nulliffileisnull
-
computeHash
Computes the cryptographic hash of the provided GridFS file using the specified algorithm.- Parameters:
file- the file whose hash should be calculatedalgorithm- name of the hash algorithm supported by MongoDB- Returns:
- the hexadecimal encoded hash value
-
computeHash
Computes the hash of a file identified by its UUID.- Specified by:
computeHashin interfaceGridFsRepository- Parameters:
uuid- unique identifier of the filealgorithm- name of the hash algorithm supported by MongoDB- Returns:
- the hexadecimal encoded hash value
-