Merge branch 'seriousben-feature/upload_key_renaming'
This commit is contained in:
commit
c34f0f6dfd
|
@ -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 });
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -1 +1,2 @@
|
|||
test.jpg
|
||||
image-test.jpg
|
||||
|
|
|
@ -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')
|
||||
|
|
Loading…
Reference in New Issue