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
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected String
computeHash
(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.void
deleteFileById
(String uuid) Deletes a file and its metadata from GridFS by UUID.com.mongodb.client.gridfs.model.GridFSFile
findFileById
(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.void
init()
Initializes the component by creating an index on themetadata.uuid
field of thefs.files
collection.void
Stores a new file in GridFS.void
updateFileMetadata
(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.uuid
field of thefs.files
collection. This ensures fast lookups when retrieving files by UUID.If the index creation fails the component is shut down.
- Overrides:
init
in classBaseComponent
-
saveFile
public void saveFile(InputStream inputStream, String fileName, String contentType, Map<String, String> metadata) Stores a new file in GridFS.- Specified by:
saveFile
in 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:
updateFileMetadata
in 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:
findFileById
in interfaceGridFsRepository
- Parameters:
uuid
- unique identifier of the file- Returns:
- the matching
GridFSFile
ornull
if not found
-
deleteFileById
Deletes a file and its metadata from GridFS by UUID.- Specified by:
deleteFileById
in 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
null
iffile
isnull
-
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:
computeHash
in interfaceGridFsRepository
- Parameters:
uuid
- unique identifier of the filealgorithm
- name of the hash algorithm supported by MongoDB- Returns:
- the hexadecimal encoded hash value
-