From 31c0bccb9a92356bea7665f948fc7872bc95f36b Mon Sep 17 00:00:00 2001 From: UzverNumber47 Date: Wed, 3 May 2017 12:18:07 +0300 Subject: [PATCH] feat(index): add getFileReadStreamById method You need this method to get a stream on server. Just by call it without creating db connection and GridFS. --- lib/index.js | 15 ++++++++++----- source/index.coffee | 9 ++++++--- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/index.js b/lib/index.js index cefce38..99691d9 100644 --- a/lib/index.js +++ b/lib/index.js @@ -220,15 +220,20 @@ MongoStorage = (function() { }, callback); }; + MongoStorage.prototype.getFileReadStreamById = function(id) { + var gfs; + gfs = Grid(this.db, mongodb); + return gfs.createReadStream({ + _id: id + }); + }; + MongoStorage.prototype.__download = function(file, res, callback) { - var gfs, read; + var read; if (callback == null) { callback = (function() {}); } - gfs = Grid(this.db, mongodb); - read = gfs.createReadStream({ - _id: file._id - }); + read = this.getFileReadStreamById(file._id); res.set('Content-Disposition', "attachment; filename=\"" + file.filename + "\""); res.set('Content-Type', file.metadata.mimetype); res.set('Content-Length', file.length); diff --git a/source/index.coffee b/source/index.coffee index 15fa451..5b130bd 100644 --- a/source/index.coffee +++ b/source/index.coffee @@ -155,10 +155,13 @@ class MongoStorage getFileById: (id, callback) -> @__getFile _id: id, callback - __download: (file, res, callback = (-> return)) -> + getFileReadStreamById: (id) -> gfs = Grid @db, mongodb - read = gfs.createReadStream - _id: file._id + gfs.createReadStream + _id: id + + __download: (file, res, callback = (-> return)) -> + read = @getFileReadStreamById file._id res.set 'Content-Disposition', "attachment; filename=\"#{file.filename}\"" res.set 'Content-Type', file.metadata.mimetype res.set 'Content-Length', file.length