added file field name into getFilename function

This commit is contained in:
Jose De Gouveia 2016-09-07 17:29:12 +02:00 committed by Miguel González Aravena
parent 64c723e123
commit ff200bf328
3 changed files with 46 additions and 27 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

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

View File

@ -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();
});
});
});