Skip to content
This repository has been archived by the owner on Oct 27, 2020. It is now read-only.

Added download by id and support to send upload stream in req.filedata #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 75 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Owner

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 ?

Copy link
Author

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.

Copy link
Owner

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

if (req.filedata) {
return req.filedata.pipe(busboy);
}else {
return req.pipe(busboy);
}
};

MongoStorage.prototype.uploadFile = function(container, file, options, callback) {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how loopback makes the difference between /:container/downloadFile/:fileId and /:container/downloadFile/:filename ?

Copy link
Author

Choose a reason for hiding this comment

The 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.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sachinhub you have to make it in coffee-script ;)

Copy link
Author

Choose a reason for hiding this comment

The 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 || {};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "loopback-component-storage-mongo",
"version": "1.0.2",
"version": "1.0.3",
"description": "",
"main": "lib/index.js",
"scripts": {
Expand Down