-
Notifications
You must be signed in to change notification settings - Fork 21
Added download by id and support to send upload stream in req.filedata #7
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,7 +131,15 @@ MongoStorage = (function() { | |
return callback(null, res); | ||
})["catch"](callback); | ||
}); | ||
return req.pipe(busboy); | ||
//return req.pipe(busboy); | ||
|
||
// There may be a requirement to read the req stream before calling this function. e.g. to do virus scanning in temporary location. | ||
// The file stream can again be attached to req.filedata and passed to this function in that case. | ||
if (req.filedata) { | ||
return req.filedata.pipe(busboy); | ||
}else { | ||
return req.pipe(busboy); | ||
} | ||
}; | ||
|
||
MongoStorage.prototype.uploadFile = function(container, file, options, callback) { | ||
|
@@ -208,6 +216,46 @@ MongoStorage = (function() { | |
return read.pipe(res); | ||
}); | ||
}; | ||
|
||
MongoStorage.prototype.getFileById = function(container, fileId, callback) { | ||
return this.db.collection('fs.files').findOne({ | ||
'metadata.mongo-storage': true, | ||
'metadata.container': container, | ||
'_id': new ObjectID(fileId) | ||
}, function(err, file) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
if (!file) { | ||
err = new Error('File not found'); | ||
err.status = 404; | ||
return callback(err); | ||
} | ||
return callback(null, file); | ||
}); | ||
}; | ||
MongoStorage.prototype.downloadFileById = function(container, fileId, res, callback) { | ||
console.log("inside custom method"); | ||
var self; | ||
if (callback == null) { | ||
callback = (function() {}); | ||
} | ||
self = this; | ||
return this.getFileById(container, fileId, function(err, file) { | ||
var gfs, read; | ||
if (err) { | ||
return callback(err); | ||
} | ||
gfs = Grid(self.db, mongodb); | ||
read = gfs.createReadStream({ | ||
_id: file._id | ||
}); | ||
res.set('Content-Disposition', "attachment; filename=\"" + file.filename + "\""); | ||
res.set('Content-Type', file.metadata.mimetype); | ||
res.set('Content-Length', file.length); | ||
return read.pipe(res); | ||
}); | ||
}; | ||
|
||
return MongoStorage; | ||
|
||
|
@@ -377,11 +425,37 @@ MongoStorage.prototype.download.accepts = [ | |
} | ||
]; | ||
|
||
|
||
|
||
MongoStorage.prototype.download.http = { | ||
verb: 'get', | ||
path: '/:container/download/:file' | ||
}; | ||
|
||
//New API endpoint to download file by id | ||
MongoStorage.prototype.downloadFileById.shared = true; | ||
|
||
MongoStorage.prototype.downloadFileById.accepts = [ | ||
{ | ||
arg: 'container', | ||
type: 'string' | ||
}, { | ||
arg: 'fileId', | ||
type: 'string' | ||
}, { | ||
arg: 'res', | ||
type: 'object', | ||
http: { | ||
source: 'res' | ||
} | ||
} | ||
]; | ||
|
||
MongoStorage.prototype.downloadFileById.http = { | ||
verb: 'get', | ||
path: '/:container/downloadFile/:fileId' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how loopback makes the difference between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops. The path is supposed to be /:container/downloadFileById/:fileId. I will fix and resend the pull request. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sachinhub you have to make it in coffee-script ;) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. Sending a patch. I am struggling to see the output of the tests. I am doing "gulp test" but could not see if tests are passing or failing. |
||
}; | ||
|
||
exports.initialize = function(dataSource, callback) { | ||
var connector, k, m, method, opt, ref, settings; | ||
settings = dataSource.settings || {}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you store it in temporary location, you store it on the filesystem ? What is the difference between storing it in the filesystem and storing it in database ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes @jdrouet. We store in a folder on file system and then execute a virus scan script on it. The folder is quarantine.
Once the virus scan command returns okay, we upload it in database. We dont want to upload in database until it passes virus scanning tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sachinhub I think that you need to use the standard component storage first then upload it to database