Adding key renaming support on Service.upload and Handler.upload

This commit is contained in:
Benjamin Boudreau 2015-01-29 09:32:51 -05:00
parent 3e17a880f0
commit ec4000745a
4 changed files with 69 additions and 7 deletions

View File

@ -6,13 +6,18 @@ var StringDecoder = require('string_decoder').StringDecoder;
* @param {Object} provider The storage service provider * @param {Object} provider The storage service provider
* @param {Request} req The HTTP request * @param {Request} req The HTTP request
* @param {Response} res The HTTP response * @param {Response} res The HTTP response
* @param {String} container The container name * @param {Object} [options] The container name
* @callback {Function} cb Callback function * @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); var form = new IncomingForm(this.options);
container = container || req.params.container; container = options.container || req.params.container;
var fields = {}, files = {}; var fields = {}, files = {};
form.handlePart = function (part) { form.handlePart = function (part) {
var self = this; var self = this;
@ -51,13 +56,18 @@ exports.upload = function (provider, req, res, container, cb) {
type: part.mime type: part.mime
}; };
if ('function' === typeof options.getFilename) {
file.name = options.getFilename(file, req, res);
}
self.emit('fileBegin', part.name, file); self.emit('fileBegin', part.name, file);
var headers = {}; var headers = {};
if ('content-type' in part.headers) { if ('content-type' in part.headers) {
headers['content-type'] = part.headers['content-type']; 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 () { var endFunc = function () {
self._flushing--; self._flushing--;

View File

@ -27,6 +27,9 @@ function StorageService(options) {
} }
this.provider = options.provider; this.provider = options.provider;
this.client = factory.createClient(options); this.client = factory.createClient(options);
if ('function' === typeof options.getFilename) {
this.getFilename = options.getFilename;
}
} }
function map(obj) { function map(obj) {
@ -204,10 +207,18 @@ StorageService.prototype.removeFile = function (container, file, cb) {
* Upload middleware for the HTTP request/response <!-- Should this be documented? --> * Upload middleware for the HTTP request/response <!-- Should this be documented? -->
* @param {Request} req Request object * @param {Request} req Request object
* @param {Response} res Response object * @param {Response} res Response object
* @param {Object} [options] Options for upload
* @param {Function} cb Callback function * @param {Function} cb Callback function
*/ */
StorageService.prototype.upload = function (req, res, cb) { StorageService.prototype.upload = function(req, res, options, cb) {
return handler.upload(this.client, req, res, req.params.container, 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);
}; };
/** /**

View File

@ -1 +1,2 @@
test.jpg test.jpg
image-test.jpg

View File

@ -8,6 +8,19 @@ var path = require('path');
// expose a rest api // expose a rest api
app.use(loopback.rest()); 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({ var ds = loopback.createDataSource({
connector: require('../lib/storage-connector'), connector: require('../lib/storage-connector'),
provider: 'filesystem', 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) { it('should get file by name', function (done) {
request('http://localhost:3000') 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) { it('downloads files', function (done) {
request('http://localhost:3000') request('http://localhost:3000')