From ff200bf3288c9dee4e91d0aa8b0cbf45cbcfec3e Mon Sep 17 00:00:00 2001 From: Jose De Gouveia Date: Wed, 7 Sep 2016 17:29:12 +0200 Subject: [PATCH] added file field name into getFilename function --- lib/storage-handler.js | 1 + test/images/album1/.gitignore | 1 + test/upload-download.test.js | 71 ++++++++++++++++++++++------------- 3 files changed, 46 insertions(+), 27 deletions(-) diff --git a/lib/storage-handler.js b/lib/storage-handler.js index f14a8f7..535a80b 100644 --- a/lib/storage-handler.js +++ b/lib/storage-handler.js @@ -77,6 +77,7 @@ exports.upload = function(provider, req, res, options, cb) { container: container, name: part.filename, type: part.mime, + field: part.name }; // Options for this file diff --git a/test/images/album1/.gitignore b/test/images/album1/.gitignore index 9df892a..9084277 100644 --- a/test/images/album1/.gitignore +++ b/test/images/album1/.gitignore @@ -1,2 +1,3 @@ test.jpg image-*.jpg +customimagefield_test.jpg \ No newline at end of file diff --git a/test/upload-download.test.js b/test/upload-download.test.js index b11c35d..9e8901a 100644 --- a/test/upload-download.test.js +++ b/test/upload-download.test.js @@ -15,6 +15,26 @@ var path = require('path'); // configure errorHandler to show full error message app.set('remoting', {errorHandler: {debug: true, log: false}}); +//custom route with renamer +app.post('/custom/upload', function(req, res, next) { + var options = { + container : 'album1', + getFilename : function(file, req, res) { + return file.field + "_" +file.name; + } + }; + ds.connector.upload(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); + } + }); + +}); +>>>>>>> efe4e08... added file field name into getFilename function + // expose a rest api app.use(loopback.rest()); @@ -164,29 +184,10 @@ describe('storage service', function() { .attach('image', 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': {'image': [ - {'container': 'album1', 'name': 'test.jpg', 'type': 'image/jpeg', - 'size': 60475}, - ]}, 'fields': {}}}); - done(); - }); - }); - - it('fails to upload using dotdot file path', function(done) { - request('http://localhost:' + app.get('port')) - .post('/containers/%2e%2e/upload') - .expect(200, function(err, res) { - assert(err); - done(); - }); - }); - - it('fails to upload using dotdot file path', function(done) { - request('http://localhost:' + app.get('port')) - .post('%2e%2e/containers/upload') - .expect(200, function(err, res) { - assert(err); + .expect(200, function (err, res) { + assert.deepEqual(res.body, {"result": {"files": {"image": [ + {"container": "album1", "name": "test.jpg", "type": "image/jpeg","field":"image","size": 60475} + ]}, "fields": {}}}); done(); }); }); @@ -215,10 +216,10 @@ describe('storage service', function() { .attach('image', 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': {'image': [ - {'container': 'album1', 'name': 'image-test.jpg', 'originalFilename': 'test.jpg', 'type': 'image/jpeg', 'acl': 'public-read', 'size': 60475}, - ]}, 'fields': {}}}); + .expect(200, function (err, res) { + assert.deepEqual(res.body, {"result": {"files": {"image": [ + {"container": "album1", "name": "image-test.jpg", "originalFilename":"test.jpg", "type": "image/jpeg", "field":"image", "acl":"public-read", "size": 60475} + ]}, "fields": {}}}); done(); }); }); @@ -387,4 +388,20 @@ describe('storage service', function() { done(); }); }); + + it('should upload a file with custom route accessing directly to the storage connector with renamer',function (done){ + + request('http://localhost:' + app.get('port')) + .post('/custom/upload') + .attach('customimagefield', 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": {"customimagefield": [ + {"container": "album1", "name": "customimagefield_test.jpg", "originalFilename":"test.jpg", "type": "image/jpeg", "field":"customimagefield", "size": 60475} + ]}, "fields": {}}}); + done(); + }); + }); + });