From ec4000745af799fd8868a7fc12e693eb07343507 Mon Sep 17 00:00:00 2001 From: Benjamin Boudreau Date: Thu, 29 Jan 2015 09:32:51 -0500 Subject: [PATCH] Adding key renaming support on Service.upload and Handler.upload --- lib/storage-handler.js | 20 +++++++++++++----- lib/storage-service.js | 15 +++++++++++-- test/images/album1/.gitignore | 1 + test/upload-download.test.js | 40 +++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 7 deletions(-) diff --git a/lib/storage-handler.js b/lib/storage-handler.js index 5a75437..65dd441 100644 --- a/lib/storage-handler.js +++ b/lib/storage-handler.js @@ -6,13 +6,18 @@ var StringDecoder = require('string_decoder').StringDecoder; * @param {Object} provider The storage service provider * @param {Request} req The HTTP request * @param {Response} res The HTTP response - * @param {String} container The container name + * @param {Object} [options] The container name * @callback {Function} cb Callback function - * @header storageService.upload(provider, req, res, container, cb) + * @header storageService.upload(provider, req, res, options, cb) */ -exports.upload = function (provider, req, res, container, cb) { +exports.upload = function (provider, req, res, options, cb) { + if (!cb && 'function' === typeof options) { + cb = options; + options = {}; + } + var form = new IncomingForm(this.options); - container = container || req.params.container; + container = options.container || req.params.container; var fields = {}, files = {}; form.handlePart = function (part) { var self = this; @@ -51,13 +56,18 @@ exports.upload = function (provider, req, res, container, cb) { type: part.mime }; + if ('function' === typeof options.getFilename) { + file.name = options.getFilename(file, req, res); + } + self.emit('fileBegin', part.name, file); var headers = {}; if ('content-type' in part.headers) { headers['content-type'] = part.headers['content-type']; } - var writer = provider.upload({container: container, remote: part.filename}); + + var writer = provider.upload({container: container, remote: file.name}); var endFunc = function () { self._flushing--; diff --git a/lib/storage-service.js b/lib/storage-service.js index f71a04a..79d96cd 100644 --- a/lib/storage-service.js +++ b/lib/storage-service.js @@ -27,6 +27,9 @@ function StorageService(options) { } this.provider = options.provider; this.client = factory.createClient(options); + if ('function' === typeof options.getFilename) { + this.getFilename = options.getFilename; + } } function map(obj) { @@ -204,10 +207,18 @@ StorageService.prototype.removeFile = function (container, file, cb) { * Upload middleware for the HTTP request/response * @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, cb) { - return handler.upload(this.client, req, res, req.params.container, cb); +StorageService.prototype.upload = function(req, res, options, cb) { + if (!cb && 'function' === typeof options) { + cb = options; + options = {}; + } + if (this.getFilename && !options.getFilename) { + options.getFilename = this.getFilename; + } + return handler.upload(this.client, req, res, options, cb); }; /** diff --git a/test/images/album1/.gitignore b/test/images/album1/.gitignore index a25287a..ac4f9b5 100644 --- a/test/images/album1/.gitignore +++ b/test/images/album1/.gitignore @@ -1 +1,2 @@ test.jpg +image-test.jpg diff --git a/test/upload-download.test.js b/test/upload-download.test.js index 20a0150..1e198e4 100644 --- a/test/upload-download.test.js +++ b/test/upload-download.test.js @@ -8,6 +8,19 @@ var path = require('path'); // expose a rest api app.use(loopback.rest()); +var dsImage = loopback.createDataSource({ + connector: require('../lib/storage-connector'), + provider: 'filesystem', + root: path.join(__dirname, 'images'), + + getFilename: function(fileInfo) { + return 'image-' + fileInfo.name; + } +}); + +var ImageContainer = dsImage.createModel('imageContainer'); +app.model(ImageContainer); + var ds = loopback.createDataSource({ connector: require('../lib/storage-connector'), provider: 'filesystem', @@ -153,6 +166,21 @@ describe('storage service', function () { }); }); + it('uploads files with renamer', function (done) { + + request('http://localhost:3000') + .post('/imageContainers/album1/upload') + .attach('image', path.join(__dirname, '../example/test.jpg')) + .set('Accept', 'application/json') + .expect('Content-Type', /json/) + .expect(200, function (err, res) { + assert.deepEqual(res.body, {"result": {"files": {"image": [ + {"container": "album1", "name": "image-test.jpg", "type": "image/jpeg"} + ]}, "fields": {}}}); + done(); + }); + }); + it('should get file by name', function (done) { request('http://localhost:3000') @@ -165,6 +193,18 @@ describe('storage service', function () { }); }); + it('should get file by renamed file name', function (done) { + + request('http://localhost:3000') + .get('/imageContainers/album1/files/image-test.jpg') + .set('Accept', 'application/json') + .expect('Content-Type', /json/) + .expect(200, function (err, res) { + verifyMetadata(res.body, 'image-test.jpg'); + done(); + }); + }); + it('downloads files', function (done) { request('http://localhost:3000')