gridfs-easy
Store your files in mongodb easily
Installation
$ npm install --save gridfs-easy
Table of Content
Usage
Craete a file like db.js:
//require the gridfs-easy and mongoosevar mongoose = ;var gridfsEasy = ; //establish the connection with mongoosevar dbUri = 'mongodb://serverAddress:portNumber/databaseName';mongoose; //export the gridfsEasy so that you can use it everywheremoduleexports = { //make sure you've successfully connected mongooseconnection;};
Require the db.js wherever you want. (e.g. pictureRouter.js)
var gfsEasy =
Methods
getBase64ById (_id, callback function )
A base64 format of the file (e.g. picture, video, pdf, etc.) from fs.files collection with _id will be retrieved:
pictureRouter ;
getInfoById (_id, callback function )
A document from fs.files collection with _id will be retrieved:
pictureRouter ;
The callback function will be called with info of the document:
"_id" : "filename" : "pic.png" "contentType" : "binary/octet-stream" "length" : 13282 "chunkSize" : 261120 "uploadDate" : "aliases" : null "metadata" : null "md5" : "f0b8b7ba4ea9b70b4d21ad28f962f0ac"
getAllByName (name, callback function )
All documents with the name given as the first parameter will be retrieved as an array from the fs.files collection:
pictureRouter ;
docs:
"_id": "5ab4260c26c6441d946b69e4" "filename": "pic.png" "contentType": "binary/octet-stream" "length": 43630 "chunkSize": 261120 "uploadDate": "2018-03-22T21:54:20.937Z" "aliases": null "metadata": null "md5": "2830695ace2df8a15d3479c432af9f88" "_id": "5ab427a726c6441d946b69e6" "filename": "pic.png" "contentType": "binary/octet-stream" "length": 43630 "chunkSize": 261120 "uploadDate": "2018-03-22T22:01:11.637Z" "aliases": null "metadata": null "md5": "2830695ace2df8a15d3479c432af9f88"
getImageById (_id, callback function )
A base64 format of the image plus the corresponding data URI scheme (e.g. data:image/png;base64,) of a file from fs.files collection with _id will be retrieved:
pictureRouter ;
existFile (_id, callback function )
Check whether a file with _id exists in fs.files collection:
gfsEasy
removeFile (_id, callback function )
Remove a file with _id from fs.files collection:
gfsEasy
removeAll (callback function )
Remove all files from fs.files collection:
gfsEasy
putFile (path, name, callback function )
Store a file in the fs.files collection. Here ng-file-upload is used on the client side to send the file to the server (some tips are provided here).
Don't forget to use connect-multiparty as a middleware:
pictureRouter ;
populateImage (document, callback function )
Your document must have two fields : imgId and imgBase64.
Take this for instance. The document of a user:
"_id" : "name" : "kasra" "age" : 22 "address": "some address" "imgId" : "imgBase64" : "empty"
The document of a picture: ( Remember that all of your pictures are stored in fs.files collection. )
"_id": "5ab4260c26c6441d946b69e4" "filename": "user1.png" "contentType": "binary/octet-stream" "length": 43630 "chunkSize": 261120 "uploadDate": "2018-03-22T21:54:20.937Z" "aliases": null "metadata": null "md5": "2830695ace2df8a15d3479c432af9f88"
Your document (in our example the user document) must hold the id of the picture under the imgId field
Once the populateImage function is called with the user document as the first parameter, the imgBase64 field will be populated with the base64 format of the corresponding picture and the updated user document will be available in the callback function:
The updatedUserDoc:
"_id" : "name" : "kasra" "age" : 22 "address": "some address" "imgId" : "imgBase64" : "data:image/png;base64,iVBORw0KGgoA..." <------
The sample code :
userRouter ;
populateImages (documents array, callback function )
Populate imgBase64 field in documents in the given array:
userRouter ;
Tips (Client side)
As mentioned above, you can use ng-file-upload to upload your files:
We've created two buttons, one for choosing a file and the other for submitting:
HTML:
Select putFile
Assume that the controller below controls the form in our view:
Controller:
$scope { fileUpload;};
We have created a function which receives the file and uploads it using Upload (ng-file-upload) to the server. Please visit ng-file-upload.
fileUpload Service:
this { Upload; };
License
MIT © Kasra Ghasemi