Merge branch 'release/1.2.0' into production

This commit is contained in:
Raymond Feng 2015-01-30 11:35:33 -08:00
commit 2d313ac25b
6 changed files with 81 additions and 11 deletions

View File

@ -1,3 +1,11 @@
2015-01-30, Version 1.2.0
=========================
* Fix deprecation (Raymond Feng)
* Adding key renaming support on Service.upload and Handler.upload (Benjamin Boudreau)
2015-01-16, Version 1.1.0
=========================

View File

@ -6,13 +6,18 @@ var StringDecoder = require('string_decoder').StringDecoder;
* @param {Object} provider The storage service provider
* @param {Request} req The HTTP request
* @param {Response} res The HTTP response
* @param {String} container The container name
* @param {Object} [options] The container name
* @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);
container = container || req.params.container;
container = options.container || req.params.container;
var fields = {}, files = {};
form.handlePart = function (part) {
var self = this;
@ -51,13 +56,18 @@ exports.upload = function (provider, req, res, container, cb) {
type: part.mime
};
if ('function' === typeof options.getFilename) {
file.name = options.getFilename(file, req, res);
}
self.emit('fileBegin', part.name, file);
var headers = {};
if ('content-type' in part.headers) {
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 () {
self._flushing--;
@ -124,12 +134,12 @@ exports.download = function (provider, req, res, container, file, cb) {
reader.on('error', function (err) {
if (err.code === 'ENOENT') {
res.type('application/json');
res.send(404, { error: err });
res.status(404).send({ error: err });
return;
}
res.type('application/json');
res.send(500, { error: err });
res.status(500).send({ error: err });
});
}

View File

@ -27,6 +27,9 @@ function StorageService(options) {
}
this.provider = options.provider;
this.client = factory.createClient(options);
if ('function' === typeof options.getFilename) {
this.getFilename = options.getFilename;
}
}
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? -->
* @param {Request} req Request object
* @param {Response} res Response object
* @param {Object} [options] Options for upload
* @param {Function} cb Callback function
*/
StorageService.prototype.upload = function (req, res, cb) {
return handler.upload(this.client, req, res, req.params.container, cb);
StorageService.prototype.upload = function(req, res, options, 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,7 +1,7 @@
{
"name": "loopback-component-storage",
"description": "Loopback Storage Service",
"version": "1.1.0",
"version": "1.2.0",
"main": "index.js",
"scripts": {
"test": "./node_modules/.bin/mocha --timeout 30000 test/*test.js"
@ -27,6 +27,6 @@
"url": "https://github.com/strongloop/loopback-strorage-service/blob/master/LICENSE"
},
"optionalDependencies": {
"sl-blip": "http://blip.strongloop.com/loopback-component-storage@1.1.0"
"sl-blip": "http://blip.strongloop.com/loopback-component-storage@1.2.0"
}
}

View File

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

View File

@ -8,6 +8,19 @@ var path = require('path');
// expose a rest api
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({
connector: require('../lib/storage-connector'),
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) {
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) {
request('http://localhost:3000')