Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Setting up storage using multer-gridfs-storage */
- var storage = GridFsStorage({
- gfs : gfs,
- filename: function (req, file, cb) {
- var datetimestamp = Date.now();
- cb(null, file.fieldname + '-' + datetimestamp + '.' + file.originalname.split('.')[file.originalname.split('.').length -1]);
- },
- /** With gridfs we can store aditional meta-data along with the file */
- metadata: function(req, file, cb) {
- cb(null, { originalname: file.originalname });
- },
- root: 'ctFiles' //root name for collection to store files into
- });
- var upload = multer({ //multer settings for single upload
- storage: storage
- }).single('file');
- /** API path that will upload the files */
- app.post('/upload', function(req, res) {
- upload(req,res,function(err){
- if(err){
- res.json({error_code:1,err_desc:err});
- return;
- }
- console.log(res.file);
- console.log(res[0].file);
- res.json({error_code:0,err_desc:null});
- });
- });
- var UserSchema = new Schema({
- name: String,
- email: {
- type: String,
- lowercase: true
- },
- filepath: String,
- });
- {
- "_id" : ObjectId("58fb894111387b23a0bf2ccc"),
- "filename" : "file-1492879681306.PNG",
- "contentType" : "image/png",
- "length" : 67794,
- "chunkSize" : 261120,
- "uploadDate" : ISODate("2017-04-22T16:48:01.350Z"),
- "aliases" : null,
- "metadata" : {
- "originalname" : "Front.PNG"
- },
- "md5" : "404787a5534d0479bd55b2793f2a74b5"
- }
- {
- "name" :"asdf",
- "email" : "asdf@gmail.com",
- "filepath":"file/file-1492879681306.PNG"
- }
- {
- "_id" : ObjectId("58fb894111387b23a0bf2ccc"),
- "filename" : "file-1492879681306.PNG",
- "contentType" : "image/png",
- "length" : 67794,
- "chunkSize" : 261120,
- "uploadDate" : ISODate("2017-04-22T16:48:01.350Z"),
- "aliases" : null,
- "metadata" : {
- "originalname" : "Front.PNG",
- "name" :"asdf",
- "email" : "asdf@gmail.com",
- "filepath":"file/file-1492879681306.PNG"
- },
- "md5" : "404787a5534d0479bd55b2793f2a74b5"
- }
- ....
- /** With gridfs we can store aditional meta-data along with the file */
- metadata: function(req, file, cb) {
- var metadata = {
- originalname: file.originalname,
- // get this information somehow
- name :"asdf",
- email : "asdf@gmail.com",
- filepath:"file/file-1492879681306.PNG"
- };
- cb(null, metadata);
- },
- ....
- const mongodb = require('mongodb');
- const GridFSBucket = mongodb.GridFSBucket;
- const MongoClient = mongodb.MongoClient;
- MongoClient.connect('mongodb://yourhost:27017/database').then((db) => {
- const bucket = new GridFSBucket(db, {bucketName: 'ctFiles'});
- bucket
- .find({metadata: {'email' : 'asdf@gmail.com'}})
- .toArray()
- .then((fileInfoArr) => {
- console.log(fileInfoArr);
- });
- });
- var UserSchema = new Schema({
- name: String,
- email: {
- type: String,
- lowercase: true
- },
- filepath: String,
- fileId: Schema.Types.ObjectId
- });
- UserModel.findOne({'email' : 'asdf@gmail.com'}, function (err, user) {
- // deal with error
- // store and get the db object somehow
- const bucket = new GridFSBucket(db, {bucketName: 'ctFiles'});
- // A download stream reads the file from GridFs
- const readStream = bucket.openDownloadStream(user.fileId));
- readStream.pipe(/* some writable stream */);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement