db._svc.GridFsService¶
Functionality around dealing with reading and writing files to/from Mongo’s gridfs. Wraps gridfs API adding noonian metadata, id’s and logic.
Todo
need to refactor away from gridfs-stream: https://www.npmjs.com/package/mongoose-gridfs Updated mongo api supports streaming: https://mongodb.github.io/node-mongodb-native/3.1/tutorials/gridfs/streaming/ ancient bug report: https://github.com/aheckmann/gridfs-stream/issues/125
Todo
API is a little sloppy; saveFile creates a file by passing a readstream, writeFile creates a file by retreiving a writeStream
-
gridfs.
init
()¶ Initialize gridfs servce
- Returns
Promise – resolving when init is complete
-
saveFile
(readStream, metadata)¶ Stream a file to gridfs
- Arguments
readStream (stream.Readable) – node readable stream containing contents of file to write
metadata (Object) –
- Returns
Promise.<string> – file id that can later be used to getFile;
-
writeFile
(metadata)¶ Open a write stream to a file; augments metadata with an attachment_id
- Arguments
metadata (Object) –
- Returns
stream.Writable – stream that receives file contents
-
getFile
(fileId)¶ Retreive a file by id
- Arguments
fileId (string) –
- Returns
Promise.<{{readstream:stream.Readable, metadata:object}}> –
-
deleteFile
(fileId)¶ Delete a file from gridfs
- Arguments
fileId (string) –
- Returns
Promise.<{{result:'success', file:fileId}}> –
-
annotateIncomingRef
(fileId, boClass, boId, field)¶ Add metadata to track incoming reference to a file
- Arguments
fileId (string) –
boClass (string) – class name of BusinessObject containing reference
boId (string) – id of BusinessObject containing reference
field (string) – field name containing reference
- Returns
null
-
getAllFileMetadata
()¶ Get all files in fs.files
- Returns
Promise.<object> – object mapping filenames to metadata.
-
exportFile
(attachmentId, outPath)¶ Export an attachment from gridfs to the filesystem
- Arguments
attachmentId (string) –
outPath (string) – fully-qualified filename of target file
- Returns
Promise – resolving to outPath when file write is completed
-
importFile
(inPath, contentType)¶ Import a file from the filesystem
- Arguments
inPath (string) – fully-qualified path to file to import
contentType (string) – ContentType of given file. if not provided, file extension is used to detect
- Returns
Promise.<object> – resolving to metadata object which can be assigned to a field of type “attachment”
-
cleanup
()¶ Scan the database for files in gridfs that do not have any references from attachment fields, and delete them.
- Returns
Promise.<{{deleteCount:number, deleted:object}}> –