diff --git a/lib/storage-service.js b/lib/storage-service.js index 322b518..dbb1d50 100644 --- a/lib/storage-service.js +++ b/lib/storage-service.js @@ -225,12 +225,21 @@ StorageService.prototype.removeFile = function(container, file, cb) { /** * Upload middleware for the HTTP request/response + * @param {String} [container] Container name * @param {Request} req Request object * @param {Response} res Response object * @param {Object} [options] Options for upload * @param {Function} cb Callback function */ -StorageService.prototype.upload = function(req, res, options, cb) { +StorageService.prototype.upload = function(container, req, res, options, cb) { + // Test if container is req for backward compatibility + if (typeof container === 'object' && container.url && container.method) { + // First argument is req, shift all args + cb = options; + options = res; + res = req; + req = container; + } if (!cb && 'function' === typeof options) { cb = options; options = {}; @@ -253,6 +262,9 @@ StorageService.prototype.upload = function(req, res, options, cb) { if (this.maxFieldsSize && !options.maxFieldsSize) { options.maxFieldsSize = this.maxFieldsSize; } + if (typeof container === 'string') { + options.container = container; + } return handler.upload(this.client, req, res, options, cb); }; @@ -282,7 +294,7 @@ StorageService.prototype.getContainers.http = StorageService.prototype.getContainer.shared = true; StorageService.prototype.getContainer.accepts = [ - {arg: 'container', type: 'string', required: true}, + {arg: 'container', type: 'string', required: true, 'http': {source: 'path'}}, ]; StorageService.prototype.getContainer.returns = { arg: 'container', @@ -304,7 +316,7 @@ StorageService.prototype.createContainer.http = StorageService.prototype.destroyContainer.shared = true; StorageService.prototype.destroyContainer.accepts = [ - {arg: 'container', type: 'string'}, + {arg: 'container', type: 'string', required: true, 'http': {source: 'path'}}, ]; StorageService.prototype.destroyContainer.returns = {}; StorageService.prototype.destroyContainer.http = @@ -312,7 +324,7 @@ StorageService.prototype.destroyContainer.http = StorageService.prototype.getFiles.shared = true; StorageService.prototype.getFiles.accepts = [ - {arg: 'container', type: 'string', required: true}, + {arg: 'container', type: 'string', required: true, 'http': {source: 'path'}}, ]; StorageService.prototype.getFiles.returns = {arg: 'files', type: 'array', root: true}; StorageService.prototype.getFiles.http = @@ -320,8 +332,8 @@ StorageService.prototype.getFiles.http = StorageService.prototype.getFile.shared = true; StorageService.prototype.getFile.accepts = [ - {arg: 'container', type: 'string', required: true}, - {arg: 'file', type: 'string', required: true}, + {arg: 'container', type: 'string', required: true, 'http': {source: 'path'}}, + {arg: 'file', type: 'string', required: true, 'http': {source: 'path'}}, ]; StorageService.prototype.getFile.returns = {arg: 'file', type: 'object', root: true}; StorageService.prototype.getFile.http = @@ -329,8 +341,8 @@ StorageService.prototype.getFile.http = StorageService.prototype.removeFile.shared = true; StorageService.prototype.removeFile.accepts = [ - {arg: 'container', type: 'string', required: true}, - {arg: 'file', type: 'string', required: true}, + {arg: 'container', type: 'string', required: true, 'http': {source: 'path'}}, + {arg: 'file', type: 'string', required: true, 'http': {source: 'path'}}, ]; StorageService.prototype.removeFile.returns = {}; StorageService.prototype.removeFile.http = @@ -338,6 +350,7 @@ StorageService.prototype.removeFile.http = StorageService.prototype.upload.shared = true; StorageService.prototype.upload.accepts = [ + {arg: 'container', type: 'string', required: true, 'http': {source: 'path'}}, {arg: 'req', type: 'object', 'http': {source: 'req'}}, {arg: 'res', type: 'object', 'http': {source: 'res'}}, ]; diff --git a/test/images/album1/.gitignore b/test/images/album1/.gitignore index 9084277..ea550c4 100644 --- a/test/images/album1/.gitignore +++ b/test/images/album1/.gitignore @@ -1,3 +1,4 @@ test.jpg image-*.jpg -customimagefield_test.jpg \ No newline at end of file +customimagefield_test.jpg +customimagefield1_test.jpg diff --git a/test/upload-download.test.js b/test/upload-download.test.js index 18c041b..7fb6025 100644 --- a/test/upload-download.test.js +++ b/test/upload-download.test.js @@ -32,6 +32,23 @@ app.post('/custom/upload', function(req, res, next) { }); }); +// custom route with renamer +app.post('/custom/uploadWithContainer', function(req, res, next) { + var options = { + getFilename: function(file, req, res) { + return file.field + '_' + file.name; + }, + }; + ds.connector.upload('album1', req, res, options, function(err, result) { + if (!err) { + res.setHeader('Content-Type', 'application/json'); + res.status(200).send({result: result}); + } else { + res.status(500).send(err); + } + }); +}); + // expose a rest api app.use(loopback.rest()); @@ -420,4 +437,20 @@ describe('storage service', function() { done(); }); }); + + it('should upload a file with container param', function(done) { + request('http://localhost:' + app.get('port')) + .post('/custom/uploadWithContainer') + .attach('customimagefield1', path.join(__dirname, './fixtures/test.jpg')) + .set('Accept', 'application/json') + .expect('Content-Type', /json/) + .expect(200, function(err, res) { + assert.deepEqual(res.body, {'result': {'files': {'customimagefield1': [ + {'container': 'album1', 'name': 'customimagefield1_test.jpg', + 'originalFilename': 'test.jpg', 'type': 'image/jpeg', + 'field': 'customimagefield1', 'size': 60475}, + ]}, 'fields': {}}}); + done(); + }); + }); });