Merge branch 'hgouveia-master'

This commit is contained in:
Raymond Feng 2017-03-01 15:32:23 -08:00
commit 98ded2eed2
4 changed files with 39 additions and 3 deletions

View File

@ -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

View File

@ -249,6 +249,7 @@ StorageService.prototype.upload = function(req, res, options, cb) {
}
if (this.nameConflict && !options.nameConflict) {
options.nameConflict = this.nameConflict;
}
if (this.maxFieldsSize && !options.maxFieldsSize) {
options.maxFieldsSize = this.maxFieldsSize;
}

View File

@ -1,2 +1,3 @@
test.jpg
image-*.jpg
customimagefield_test.jpg

View File

@ -14,6 +14,23 @@ 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);
}
});
});
// expose a rest api
app.use(loopback.rest());
@ -166,8 +183,7 @@ describe('storage service', function() {
.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},
{'container': 'album1', 'name': 'test.jpg', 'type': 'image/jpeg', 'field': 'image', 'size': 60475},
]}, 'fields': {}}});
done();
});
@ -217,7 +233,7 @@ describe('storage service', function() {
.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},
{'container': 'album1', 'name': 'image-test.jpg', 'originalFilename': 'test.jpg', 'type': 'image/jpeg', 'field': 'image', 'acl': 'public-read', 'size': 60475},
]}, 'fields': {}}});
done();
});
@ -387,4 +403,21 @@ 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();
});
});
});