added file field name into getFilename function

This commit is contained in:
Jose De Gouveia 2016-09-07 17:29:12 +02:00
parent 366e29ef44
commit efe4e089f6
3 changed files with 42 additions and 6 deletions

View File

@ -70,7 +70,8 @@ exports.upload = function(provider, req, res, options, cb) {
var file = {
container: container,
name: part.filename,
type: part.mime
type: part.mime,
field: part.name
};
// Options for this file
@ -160,7 +161,7 @@ exports.upload = function(provider, req, res, options, cb) {
if (fileSize > maxFileSize) {
// We are missing some way to tell the provider to cancel upload/multipart upload of the current file.
// - s3-upload-stream doesn't provide a way to do this in it's public interface
// - We could call provider.delete file but it would not delete multipart data
// - We could call provider.delete file but it would not delete multipart data
self._error(new Error(g.f('{{maxFileSize}} exceeded, received %s bytes of field data (max is %s)', fileSize, maxFileSize)));
return;
}
@ -203,7 +204,7 @@ function handleError(res, err) {
* @header storageService.download(provider, req, res, container, file, cb)
*/
exports.download = function(provider, req, res, container, file, cb) {
var fileName = path.basename(file);
var params = {
container: container || req && req.params.container,

View File

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

View File

@ -10,6 +10,25 @@ var assert = require('assert');
var app = loopback();
var path = require('path');
//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());
@ -168,8 +187,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();
});
@ -184,7 +202,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();
});
@ -315,4 +333,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();
});
});
});