commit
fed898b1c5
|
@ -0,0 +1,14 @@
|
||||||
|
{
|
||||||
|
"extends": "loopback",
|
||||||
|
"rules": {
|
||||||
|
"max-len": ["error", 90, 4, {
|
||||||
|
"ignoreComments": true,
|
||||||
|
"ignoreUrls": true,
|
||||||
|
"ignorePattern": "^\\s*var\\s.+=\\s*(require\\s*\\()|(/)"
|
||||||
|
}],
|
||||||
|
// NOTE we should eventually remove this override
|
||||||
|
// and fix all of those 100+ violations
|
||||||
|
"one-var": "off",
|
||||||
|
"no-unused-expressions": "off"
|
||||||
|
}
|
||||||
|
}
|
1
index.js
1
index.js
|
@ -2,6 +2,7 @@
|
||||||
// Node module: loopback-component-storage
|
// Node module: loopback-component-storage
|
||||||
// This file is licensed under the Artistic License 2.0.
|
// This file is licensed under the Artistic License 2.0.
|
||||||
// License text available at https://opensource.org/licenses/Artistic-2.0
|
// License text available at https://opensource.org/licenses/Artistic-2.0
|
||||||
|
'use strict';
|
||||||
|
|
||||||
var SG = require('strong-globalize');
|
var SG = require('strong-globalize');
|
||||||
SG.SetRootDir(__dirname);
|
SG.SetRootDir(__dirname);
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
// Node module: loopback-component-storage
|
// Node module: loopback-component-storage
|
||||||
// This file is licensed under the Artistic License 2.0.
|
// This file is licensed under the Artistic License 2.0.
|
||||||
// License text available at https://opensource.org/licenses/Artistic-2.0
|
// License text available at https://opensource.org/licenses/Artistic-2.0
|
||||||
|
'use strict';
|
||||||
|
|
||||||
var pkgcloud = require('pkgcloud');
|
var pkgcloud = require('pkgcloud');
|
||||||
|
|
||||||
|
@ -13,9 +14,9 @@ function patchBaseClass(cls) {
|
||||||
var proto = cls.prototype;
|
var proto = cls.prototype;
|
||||||
var found = false;
|
var found = false;
|
||||||
// Find the prototype that owns the _setProperties method
|
// Find the prototype that owns the _setProperties method
|
||||||
while (proto
|
while (proto &&
|
||||||
&& proto.constructor !== pkgcloud.storage.Container
|
proto.constructor !== pkgcloud.storage.Container &&
|
||||||
&& proto.constructor !== pkgcloud.storage.File) {
|
proto.constructor !== pkgcloud.storage.File) {
|
||||||
if (proto.hasOwnProperty('_setProperties')) {
|
if (proto.hasOwnProperty('_setProperties')) {
|
||||||
found = true;
|
found = true;
|
||||||
break;
|
break;
|
||||||
|
@ -33,7 +34,7 @@ function patchBaseClass(cls) {
|
||||||
// Pass in some context as non-enumerable properties
|
// Pass in some context as non-enumerable properties
|
||||||
Object.defineProperties(receiver, {
|
Object.defineProperties(receiver, {
|
||||||
client: {value: this.client},
|
client: {value: this.client},
|
||||||
files: {value: this.files}
|
files: {value: this.files},
|
||||||
});
|
});
|
||||||
m1.call(receiver, details);
|
m1.call(receiver, details);
|
||||||
// Apply the calculated properties to this
|
// Apply the calculated properties to this
|
||||||
|
@ -43,7 +44,7 @@ function patchBaseClass(cls) {
|
||||||
// Keep references to raw and the calculated properties
|
// Keep references to raw and the calculated properties
|
||||||
this._rawMetadata = details;
|
this._rawMetadata = details;
|
||||||
this._metadata = receiver; // Use _metadata to avoid conflicts
|
this._metadata = receiver; // Use _metadata to avoid conflicts
|
||||||
}
|
};
|
||||||
|
|
||||||
proto.toJSON = function() {
|
proto.toJSON = function() {
|
||||||
return this._metadata;
|
return this._metadata;
|
||||||
|
@ -56,7 +57,6 @@ function patchBaseClass(cls) {
|
||||||
proto.getRawMetadata = function() {
|
proto.getRawMetadata = function() {
|
||||||
return this._rawMetadata;
|
return this._rawMetadata;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
/*!
|
/*!
|
||||||
* Patch the pkgcloud Container/File classes so that the metadata are separately
|
* Patch the pkgcloud Container/File classes so that the metadata are separately
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
// Node module: loopback-component-storage
|
// Node module: loopback-component-storage
|
||||||
// This file is licensed under the Artistic License 2.0.
|
// This file is licensed under the Artistic License 2.0.
|
||||||
// License text available at https://opensource.org/licenses/Artistic-2.0
|
// License text available at https://opensource.org/licenses/Artistic-2.0
|
||||||
|
'use strict';
|
||||||
|
|
||||||
var base = require('pkgcloud').storage;
|
var base = require('pkgcloud').storage;
|
||||||
var util = require('util');
|
var util = require('util');
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
// Node module: loopback-component-storage
|
// Node module: loopback-component-storage
|
||||||
// This file is licensed under the Artistic License 2.0.
|
// This file is licensed under the Artistic License 2.0.
|
||||||
// License text available at https://opensource.org/licenses/Artistic-2.0
|
// License text available at https://opensource.org/licenses/Artistic-2.0
|
||||||
|
'use strict';
|
||||||
|
|
||||||
var base = require('pkgcloud').storage;
|
var base = require('pkgcloud').storage;
|
||||||
var util = require('util');
|
var util = require('util');
|
||||||
|
|
|
@ -3,6 +3,10 @@
|
||||||
// This file is licensed under the Artistic License 2.0.
|
// This file is licensed under the Artistic License 2.0.
|
||||||
// License text available at https://opensource.org/licenses/Artistic-2.0
|
// License text available at https://opensource.org/licenses/Artistic-2.0
|
||||||
|
|
||||||
|
// Turning on strict for this file breaks;
|
||||||
|
// disabling strict for this file
|
||||||
|
/* eslint-disable strict */
|
||||||
|
|
||||||
// Globalization
|
// Globalization
|
||||||
var g = require('strong-globalize')();
|
var g = require('strong-globalize')();
|
||||||
|
|
||||||
|
@ -172,7 +176,7 @@ FileSystemProvider.prototype.upload = function (options, cb) {
|
||||||
|
|
||||||
var fileOpts = {flags: options.flags || 'w+',
|
var fileOpts = {flags: options.flags || 'w+',
|
||||||
encoding: options.encoding || null,
|
encoding: options.encoding || null,
|
||||||
mode: options.mode || 0666
|
mode: options.mode || 0666,
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -201,8 +205,8 @@ FileSystemProvider.prototype.download = function (options, cb) {
|
||||||
autoClose: true};
|
autoClose: true};
|
||||||
|
|
||||||
if (options.start) {
|
if (options.start) {
|
||||||
fileOpts.start = options.start
|
fileOpts.start = options.start;
|
||||||
fileOpts.end = options.end
|
fileOpts.end = options.end;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
// Node module: loopback-component-storage
|
// Node module: loopback-component-storage
|
||||||
// This file is licensed under the Artistic License 2.0.
|
// This file is licensed under the Artistic License 2.0.
|
||||||
// License text available at https://opensource.org/licenses/Artistic-2.0
|
// License text available at https://opensource.org/licenses/Artistic-2.0
|
||||||
|
'use strict';
|
||||||
|
|
||||||
var StorageService = require('./storage-service');
|
var StorageService = require('./storage-service');
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
// Node module: loopback-component-storage
|
// Node module: loopback-component-storage
|
||||||
// This file is licensed under the Artistic License 2.0.
|
// This file is licensed under the Artistic License 2.0.
|
||||||
// License text available at https://opensource.org/licenses/Artistic-2.0
|
// License text available at https://opensource.org/licenses/Artistic-2.0
|
||||||
|
'use strict';
|
||||||
|
|
||||||
// Globalization
|
// Globalization
|
||||||
var g = require('strong-globalize')();
|
var g = require('strong-globalize')();
|
||||||
|
@ -11,7 +12,7 @@ var StringDecoder = require('string_decoder').StringDecoder;
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
|
|
||||||
var defaultOptions = {
|
var defaultOptions = {
|
||||||
maxFileSize: 10 * 1024 * 1024 // 10 MB
|
maxFileSize: 10 * 1024 * 1024, // 10 MB
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -35,18 +36,22 @@ exports.upload = function(provider, req, res, options, cb) {
|
||||||
|
|
||||||
var form = new IncomingForm(options);
|
var form = new IncomingForm(options);
|
||||||
var container = options.container || req.params.container;
|
var container = options.container || req.params.container;
|
||||||
var fields = {}, files = {};
|
var fields = {};
|
||||||
|
var files = {};
|
||||||
form.handlePart = function(part) {
|
form.handlePart = function(part) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
if (part.filename === undefined) {
|
if (part.filename === undefined) {
|
||||||
var value = ''
|
var value = '';
|
||||||
, decoder = new StringDecoder(this.encoding);
|
var decoder = new StringDecoder(this.encoding);
|
||||||
|
|
||||||
part.on('data', function(buffer) {
|
part.on('data', function(buffer) {
|
||||||
self._fieldsSize += buffer.length;
|
self._fieldsSize += buffer.length;
|
||||||
if (self._fieldsSize > self.maxFieldsSize) {
|
if (self._fieldsSize > self.maxFieldsSize) {
|
||||||
self._error(new Error(g.f('{{maxFieldsSize}} exceeded, received %s bytes of field data', self._fieldsSize)));
|
self._error(new Error(
|
||||||
|
g.f('{{maxFieldsSize}} exceeded, received %s bytes of field data',
|
||||||
|
self._fieldsSize
|
||||||
|
)));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
value += decoder.write(buffer);
|
value += decoder.write(buffer);
|
||||||
|
@ -70,7 +75,7 @@ exports.upload = function(provider, req, res, options, cb) {
|
||||||
var file = {
|
var file = {
|
||||||
container: container,
|
container: container,
|
||||||
name: part.filename,
|
name: part.filename,
|
||||||
type: part.mime
|
type: part.mime,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Options for this file
|
// Options for this file
|
||||||
|
@ -91,7 +96,11 @@ exports.upload = function(provider, req, res, options, cb) {
|
||||||
}
|
}
|
||||||
if (Array.isArray(allowedContentTypes) && allowedContentTypes.length !== 0) {
|
if (Array.isArray(allowedContentTypes) && allowedContentTypes.length !== 0) {
|
||||||
if (allowedContentTypes.indexOf(file.type) === -1) {
|
if (allowedContentTypes.indexOf(file.type) === -1) {
|
||||||
self._error(new Error(g.f('{{contentType}} "%s" is not allowed (Must be in [%s])', file.type, allowedContentTypes.join(', '))));
|
self._error(new Error(
|
||||||
|
g.f('{{contentType}} "%s" is not allowed (Must be in [%s])',
|
||||||
|
file.type,
|
||||||
|
allowedContentTypes.join(', ')
|
||||||
|
)));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -121,7 +130,7 @@ exports.upload = function(provider, req, res, options, cb) {
|
||||||
var uploadParams = {
|
var uploadParams = {
|
||||||
container: container,
|
container: container,
|
||||||
remote: file.name,
|
remote: file.name,
|
||||||
contentType: file.type
|
contentType: file.type,
|
||||||
};
|
};
|
||||||
if (file.acl) {
|
if (file.acl) {
|
||||||
uploadParams.acl = file.acl;
|
uploadParams.acl = file.acl;
|
||||||
|
@ -161,13 +170,17 @@ exports.upload = function(provider, req, res, options, cb) {
|
||||||
// We are missing some way to tell the provider to cancel upload/multipart upload of the current file.
|
// 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
|
// - 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)));
|
self._error(new Error(
|
||||||
|
g.f('{{maxFileSize}} exceeded, received %s bytes of field data (max is %s)',
|
||||||
|
fileSize,
|
||||||
|
maxFileSize
|
||||||
|
)));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
part.on("end", function() {
|
part.on('end', function() {
|
||||||
writer.end();
|
writer.end();
|
||||||
});
|
});
|
||||||
part.pipe(writer, {end: false});
|
part.pipe(writer, {end: false});
|
||||||
|
@ -177,6 +190,7 @@ exports.upload = function(provider, req, res, options, cb) {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
}
|
}
|
||||||
|
// eslint-disable-next-line no-unused-expressions
|
||||||
cb && cb(err, {files: files, fields: fields});
|
cb && cb(err, {files: files, fields: fields});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -191,7 +205,6 @@ function handleError(res, err) {
|
||||||
res.status(500).send({error: err});
|
res.status(500).send({error: err});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle download from a container/file.
|
* Handle download from a container/file.
|
||||||
* @param {Object} provider The storage service provider
|
* @param {Object} provider The storage service provider
|
||||||
|
@ -203,42 +216,39 @@ function handleError(res, err) {
|
||||||
* @header storageService.download(provider, req, res, container, file, cb)
|
* @header storageService.download(provider, req, res, container, file, cb)
|
||||||
*/
|
*/
|
||||||
exports.download = function(provider, req, res, container, file, cb) {
|
exports.download = function(provider, req, res, container, file, cb) {
|
||||||
|
|
||||||
var fileName = path.basename(file);
|
var fileName = path.basename(file);
|
||||||
var params = {
|
var params = {
|
||||||
container: container || req && req.params.container,
|
container: container || req && req.params.container,
|
||||||
remote: file || req && req.params.file
|
remote: file || req && req.params.file,
|
||||||
};
|
};
|
||||||
|
|
||||||
var range = null;
|
var range = null;
|
||||||
|
|
||||||
if (req) {
|
if (req) {
|
||||||
|
|
||||||
if (req.headers) {
|
if (req.headers) {
|
||||||
range = req.headers.range || '';
|
range = req.headers.range || '';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (range) {
|
if (range) {
|
||||||
provider.getFile(params.container, params.remote, function(err, stats) {
|
provider.getFile(params.container, params.remote, function(err, stats) {
|
||||||
|
|
||||||
if (err) {
|
if (err) {
|
||||||
handleError(res, err);
|
handleError(res, err);
|
||||||
} else {
|
} else {
|
||||||
var total = stats.size;
|
var total = stats.size;
|
||||||
|
|
||||||
var parts = range.replace(/bytes=/, "").split("-")
|
var parts = range.replace(/bytes=/, '').split('-');
|
||||||
var partialstart = parts[0]
|
var partialstart = parts[0];
|
||||||
var partialend = parts[1]
|
var partialend = parts[1];
|
||||||
|
|
||||||
params.start = parseInt(partialstart, 10)
|
params.start = parseInt(partialstart, 10);
|
||||||
params.end = partialend ? parseInt(partialend, 10) : total - 1
|
params.end = partialend ? parseInt(partialend, 10) : total - 1;
|
||||||
|
|
||||||
var chunksize = (params.end - params.start) + 1
|
var chunksize = (params.end - params.start) + 1;
|
||||||
|
|
||||||
res.status(206)
|
res.status(206);
|
||||||
res.set("Content-Range", "bytes " + params.start + "-" + params.end + "/" + total);
|
res.set('Content-Range', 'bytes ' + params.start + '-' + params.end + '/' + total);
|
||||||
res.set("Accept-Ranges", "bytes");
|
res.set('Accept-Ranges', 'bytes');
|
||||||
res.set("Content-Length", chunksize);
|
res.set('Content-Length', chunksize);
|
||||||
|
|
||||||
var reader = provider.download(params);
|
var reader = provider.download(params);
|
||||||
|
|
||||||
|
@ -253,9 +263,7 @@ exports.download = function(provider, req, res, container, file, cb) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
var reader = provider.download(params);
|
var reader = provider.download(params);
|
||||||
|
|
||||||
res.type(fileName);
|
res.type(fileName);
|
||||||
|
@ -271,6 +279,3 @@ exports.download = function(provider, req, res, container, file, cb) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
// Node module: loopback-component-storage
|
// Node module: loopback-component-storage
|
||||||
// This file is licensed under the Artistic License 2.0.
|
// This file is licensed under the Artistic License 2.0.
|
||||||
// License text available at https://opensource.org/licenses/Artistic-2.0
|
// License text available at https://opensource.org/licenses/Artistic-2.0
|
||||||
|
'use strict';
|
||||||
|
|
||||||
var factory = require('./factory');
|
var factory = require('./factory');
|
||||||
var handler = require('./storage-handler');
|
var handler = require('./storage-handler');
|
||||||
|
@ -45,7 +46,6 @@ function StorageService(options) {
|
||||||
if (options.maxFileSize) {
|
if (options.maxFileSize) {
|
||||||
this.maxFileSize = options.maxFileSize;
|
this.maxFileSize = options.maxFileSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function map(obj) {
|
function map(obj) {
|
||||||
|
@ -260,29 +260,39 @@ StorageService.modelName = 'storage';
|
||||||
|
|
||||||
StorageService.prototype.getContainers.shared = true;
|
StorageService.prototype.getContainers.shared = true;
|
||||||
StorageService.prototype.getContainers.accepts = [];
|
StorageService.prototype.getContainers.accepts = [];
|
||||||
StorageService.prototype.getContainers.returns = {arg: 'containers', type: 'array', root: true};
|
StorageService.prototype.getContainers.returns = {
|
||||||
|
arg: 'containers',
|
||||||
|
type: 'array',
|
||||||
|
root: true,
|
||||||
|
};
|
||||||
StorageService.prototype.getContainers.http =
|
StorageService.prototype.getContainers.http =
|
||||||
{verb: 'get', path: '/'};
|
{verb: 'get', path: '/'};
|
||||||
|
|
||||||
StorageService.prototype.getContainer.shared = true;
|
StorageService.prototype.getContainer.shared = true;
|
||||||
StorageService.prototype.getContainer.accepts = [
|
StorageService.prototype.getContainer.accepts = [
|
||||||
{arg: 'container', type: 'string'}
|
{arg: 'container', type: 'string'},
|
||||||
];
|
];
|
||||||
StorageService.prototype.getContainer.returns = {arg: 'container', type: 'object', root: true};
|
StorageService.prototype.getContainer.returns = {
|
||||||
|
arg: 'container',
|
||||||
|
type: 'object', root: true,
|
||||||
|
};
|
||||||
StorageService.prototype.getContainer.http =
|
StorageService.prototype.getContainer.http =
|
||||||
{verb: 'get', path: '/:container'};
|
{verb: 'get', path: '/:container'};
|
||||||
|
|
||||||
StorageService.prototype.createContainer.shared = true;
|
StorageService.prototype.createContainer.shared = true;
|
||||||
StorageService.prototype.createContainer.accepts = [
|
StorageService.prototype.createContainer.accepts = [
|
||||||
{arg: 'options', type: 'object', http: {source: 'body'}}
|
{arg: 'options', type: 'object', http: {source: 'body'}},
|
||||||
];
|
];
|
||||||
StorageService.prototype.createContainer.returns = {arg: 'container', type: 'object', root: true};
|
StorageService.prototype.createContainer.returns = {
|
||||||
|
arg: 'container',
|
||||||
|
type: 'object', root: true,
|
||||||
|
};
|
||||||
StorageService.prototype.createContainer.http =
|
StorageService.prototype.createContainer.http =
|
||||||
{verb: 'post', path: '/'};
|
{verb: 'post', path: '/'};
|
||||||
|
|
||||||
StorageService.prototype.destroyContainer.shared = true;
|
StorageService.prototype.destroyContainer.shared = true;
|
||||||
StorageService.prototype.destroyContainer.accepts = [
|
StorageService.prototype.destroyContainer.accepts = [
|
||||||
{arg: 'container', type: 'string'}
|
{arg: 'container', type: 'string'},
|
||||||
];
|
];
|
||||||
StorageService.prototype.destroyContainer.returns = {};
|
StorageService.prototype.destroyContainer.returns = {};
|
||||||
StorageService.prototype.destroyContainer.http =
|
StorageService.prototype.destroyContainer.http =
|
||||||
|
@ -290,7 +300,7 @@ StorageService.prototype.destroyContainer.http =
|
||||||
|
|
||||||
StorageService.prototype.getFiles.shared = true;
|
StorageService.prototype.getFiles.shared = true;
|
||||||
StorageService.prototype.getFiles.accepts = [
|
StorageService.prototype.getFiles.accepts = [
|
||||||
{arg: 'container', type: 'string'}
|
{arg: 'container', type: 'string'},
|
||||||
];
|
];
|
||||||
StorageService.prototype.getFiles.returns = {arg: 'files', type: 'array', root: true};
|
StorageService.prototype.getFiles.returns = {arg: 'files', type: 'array', root: true};
|
||||||
StorageService.prototype.getFiles.http =
|
StorageService.prototype.getFiles.http =
|
||||||
|
@ -299,7 +309,7 @@ StorageService.prototype.getFiles.http =
|
||||||
StorageService.prototype.getFile.shared = true;
|
StorageService.prototype.getFile.shared = true;
|
||||||
StorageService.prototype.getFile.accepts = [
|
StorageService.prototype.getFile.accepts = [
|
||||||
{arg: 'container', type: 'string'},
|
{arg: 'container', type: 'string'},
|
||||||
{arg: 'file', type: 'string'}
|
{arg: 'file', type: 'string'},
|
||||||
];
|
];
|
||||||
StorageService.prototype.getFile.returns = {arg: 'file', type: 'object', root: true};
|
StorageService.prototype.getFile.returns = {arg: 'file', type: 'object', root: true};
|
||||||
StorageService.prototype.getFile.http =
|
StorageService.prototype.getFile.http =
|
||||||
|
@ -308,7 +318,7 @@ StorageService.prototype.getFile.http =
|
||||||
StorageService.prototype.removeFile.shared = true;
|
StorageService.prototype.removeFile.shared = true;
|
||||||
StorageService.prototype.removeFile.accepts = [
|
StorageService.prototype.removeFile.accepts = [
|
||||||
{arg: 'container', type: 'string'},
|
{arg: 'container', type: 'string'},
|
||||||
{arg: 'file', type: 'string'}
|
{arg: 'file', type: 'string'},
|
||||||
];
|
];
|
||||||
StorageService.prototype.removeFile.returns = {};
|
StorageService.prototype.removeFile.returns = {};
|
||||||
StorageService.prototype.removeFile.http =
|
StorageService.prototype.removeFile.http =
|
||||||
|
@ -317,7 +327,7 @@ StorageService.prototype.removeFile.http =
|
||||||
StorageService.prototype.upload.shared = true;
|
StorageService.prototype.upload.shared = true;
|
||||||
StorageService.prototype.upload.accepts = [
|
StorageService.prototype.upload.accepts = [
|
||||||
{arg: 'req', type: 'object', 'http': {source: 'req'}},
|
{arg: 'req', type: 'object', 'http': {source: 'req'}},
|
||||||
{arg: 'res', type: 'object', 'http': {source: 'res'}}
|
{arg: 'res', type: 'object', 'http': {source: 'res'}},
|
||||||
];
|
];
|
||||||
StorageService.prototype.upload.returns = {arg: 'result', type: 'object'};
|
StorageService.prototype.upload.returns = {arg: 'result', type: 'object'};
|
||||||
StorageService.prototype.upload.http =
|
StorageService.prototype.upload.http =
|
||||||
|
@ -328,7 +338,7 @@ StorageService.prototype.download.accepts = [
|
||||||
{arg: 'container', type: 'string', 'http': {source: 'path'}},
|
{arg: 'container', type: 'string', 'http': {source: 'path'}},
|
||||||
{arg: 'file', type: 'string', 'http': {source: 'path'}},
|
{arg: 'file', type: 'string', 'http': {source: 'path'}},
|
||||||
{arg: 'req', type: 'object', 'http': {source: 'req'}},
|
{arg: 'req', type: 'object', 'http': {source: 'req'}},
|
||||||
{arg: 'res', type: 'object', 'http': {source: 'res'}}
|
{arg: 'res', type: 'object', 'http': {source: 'res'}},
|
||||||
];
|
];
|
||||||
StorageService.prototype.download.http =
|
StorageService.prototype.download.http =
|
||||||
{verb: 'get', path: '/:container/download/:file'};
|
{verb: 'get', path: '/:container/download/:file'};
|
||||||
|
|
10
package.json
10
package.json
|
@ -4,7 +4,9 @@
|
||||||
"version": "1.9.2",
|
"version": "1.9.2",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "./node_modules/.bin/mocha --timeout 30000 test/*test.js"
|
"lint": "eslint .",
|
||||||
|
"test": "./node_modules/.bin/mocha --timeout 30000 test/*test.js",
|
||||||
|
"posttest": "npm run lint"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"async": "^0.9.0",
|
"async": "^0.9.0",
|
||||||
|
@ -13,11 +15,13 @@
|
||||||
"strong-globalize": "^2.6.2"
|
"strong-globalize": "^2.6.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"eslint": "^2.13.1",
|
||||||
|
"eslint-config-loopback": "^4.0.0",
|
||||||
"express": "^4.11.0",
|
"express": "^4.11.0",
|
||||||
"loopback": "^2.10.0",
|
"loopback": "^2.10.0",
|
||||||
|
"mkdirp": "^0.5.0",
|
||||||
"mocha": "^2.1.0",
|
"mocha": "^2.1.0",
|
||||||
"supertest": "^0.15.0",
|
"supertest": "^0.15.0"
|
||||||
"mkdirp": "^0.5.0"
|
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|
|
@ -2,9 +2,10 @@
|
||||||
// Node module: loopback-component-storage
|
// Node module: loopback-component-storage
|
||||||
// This file is licensed under the Artistic License 2.0.
|
// This file is licensed under the Artistic License 2.0.
|
||||||
// License text available at https://opensource.org/licenses/Artistic-2.0
|
// License text available at https://opensource.org/licenses/Artistic-2.0
|
||||||
|
'use strict';
|
||||||
|
|
||||||
var loopback = require('loopback')
|
var loopback = require('loopback');
|
||||||
, app = module.exports = loopback();
|
var app = module.exports = loopback();
|
||||||
|
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
|
|
||||||
|
@ -18,7 +19,7 @@ app.set('port', process.env.PORT || 3000);
|
||||||
var ds = loopback.createDataSource({
|
var ds = loopback.createDataSource({
|
||||||
connector: require('../index'),
|
connector: require('../index'),
|
||||||
provider: 'filesystem',
|
provider: 'filesystem',
|
||||||
root: path.join(__dirname, 'storage')
|
root: path.join(__dirname, 'storage'),
|
||||||
});
|
});
|
||||||
|
|
||||||
var container = ds.createModel('container');
|
var container = ds.createModel('container');
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
// Node module: loopback-component-storage
|
// Node module: loopback-component-storage
|
||||||
// This file is licensed under the Artistic License 2.0.
|
// This file is licensed under the Artistic License 2.0.
|
||||||
// License text available at https://opensource.org/licenses/Artistic-2.0
|
// License text available at https://opensource.org/licenses/Artistic-2.0
|
||||||
|
'use strict';
|
||||||
|
|
||||||
var FileSystemProvider = require('../lib/providers/filesystem/index.js').Client;
|
var FileSystemProvider = require('../lib/providers/filesystem/index.js').Client;
|
||||||
|
|
||||||
|
@ -20,7 +21,6 @@ function verifyMetadata(fileOrContainer, name) {
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('FileSystem based storage provider', function() {
|
describe('FileSystem based storage provider', function() {
|
||||||
|
|
||||||
describe('container apis', function() {
|
describe('container apis', function() {
|
||||||
var client = null;
|
var client = null;
|
||||||
it('should require an existing directory as the root', function(done) {
|
it('should require an existing directory as the root', function(done) {
|
||||||
|
@ -113,7 +113,7 @@ describe('FileSystem based storage provider', function () {
|
||||||
it('should download a file', function(done) {
|
it('should download a file', function(done) {
|
||||||
var reader = client.download({
|
var reader = client.download({
|
||||||
container: 'c1',
|
container: 'c1',
|
||||||
remote: 'f1.txt'
|
remote: 'f1.txt',
|
||||||
});
|
});
|
||||||
reader.pipe(fs.createWriteStream(path.join(__dirname, 'files/f1_downloaded.txt')));
|
reader.pipe(fs.createWriteStream(path.join(__dirname, 'files/f1_downloaded.txt')));
|
||||||
reader.on('end', done);
|
reader.on('end', done);
|
||||||
|
@ -159,8 +159,6 @@ describe('FileSystem based storage provider', function () {
|
||||||
done(err, container);
|
done(err, container);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,18 +2,20 @@
|
||||||
// Node module: loopback-component-storage
|
// Node module: loopback-component-storage
|
||||||
// This file is licensed under the Artistic License 2.0.
|
// This file is licensed under the Artistic License 2.0.
|
||||||
// License text available at https://opensource.org/licenses/Artistic-2.0
|
// License text available at https://opensource.org/licenses/Artistic-2.0
|
||||||
|
'use strict';
|
||||||
|
|
||||||
var StorageService = require('../lib/storage-service.js');
|
var StorageService = require('../lib/storage-service.js');
|
||||||
|
|
||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
|
|
||||||
var storageService = new StorageService({root: path.join(__dirname, 'storage'), provider: 'filesystem'});
|
var storageService = new StorageService({
|
||||||
|
root: path.join(__dirname, 'storage'),
|
||||||
|
provider: 'filesystem',
|
||||||
|
});
|
||||||
|
|
||||||
describe('Storage service', function() {
|
describe('Storage service', function() {
|
||||||
|
|
||||||
describe('container apis', function() {
|
describe('container apis', function() {
|
||||||
|
|
||||||
it('should return an empty list of containers', function(done) {
|
it('should return an empty list of containers', function(done) {
|
||||||
storageService.getContainers(function(err, containers) {
|
storageService.getContainers(function(err, containers) {
|
||||||
assert(!err);
|
assert(!err);
|
||||||
|
@ -138,8 +140,6 @@ describe('Storage service', function () {
|
||||||
done(err, container);
|
done(err, container);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
// Node module: loopback-component-storage
|
// Node module: loopback-component-storage
|
||||||
// This file is licensed under the Artistic License 2.0.
|
// This file is licensed under the Artistic License 2.0.
|
||||||
// License text available at https://opensource.org/licenses/Artistic-2.0
|
// License text available at https://opensource.org/licenses/Artistic-2.0
|
||||||
|
'use strict';
|
||||||
|
|
||||||
var request = require('supertest');
|
var request = require('supertest');
|
||||||
var loopback = require('loopback');
|
var loopback = require('loopback');
|
||||||
|
@ -23,7 +24,7 @@ var dsImage = loopback.createDataSource({
|
||||||
},
|
},
|
||||||
acl: 'public-read',
|
acl: 'public-read',
|
||||||
allowedContentTypes: ['image/png', 'image/jpeg'],
|
allowedContentTypes: ['image/png', 'image/jpeg'],
|
||||||
maxFileSize: 5 * 1024 * 1024
|
maxFileSize: 5 * 1024 * 1024,
|
||||||
});
|
});
|
||||||
|
|
||||||
var ImageContainer = dsImage.createModel('imageContainer');
|
var ImageContainer = dsImage.createModel('imageContainer');
|
||||||
|
@ -32,7 +33,7 @@ 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',
|
||||||
root: path.join(__dirname, 'images')
|
root: path.join(__dirname, 'images'),
|
||||||
});
|
});
|
||||||
|
|
||||||
var Container = ds.createModel('container', {}, {base: 'Model'});
|
var Container = ds.createModel('container', {}, {base: 'Model'});
|
||||||
|
@ -79,7 +80,6 @@ describe('storage service', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create a container', function(done) {
|
it('should create a container', function(done) {
|
||||||
|
|
||||||
request('http://localhost:' + app.get('port'))
|
request('http://localhost:' + app.get('port'))
|
||||||
.post('/containers')
|
.post('/containers')
|
||||||
.send({name: 'test-container'})
|
.send({name: 'test-container'})
|
||||||
|
@ -93,7 +93,6 @@ describe('storage service', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should get a container', function(done) {
|
it('should get a container', function(done) {
|
||||||
|
|
||||||
request('http://localhost:' + app.get('port'))
|
request('http://localhost:' + app.get('port'))
|
||||||
.get('/containers/test-container')
|
.get('/containers/test-container')
|
||||||
.set('Accept', 'application/json')
|
.set('Accept', 'application/json')
|
||||||
|
@ -105,7 +104,6 @@ describe('storage service', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should list containers', function(done) {
|
it('should list containers', function(done) {
|
||||||
|
|
||||||
request('http://localhost:' + app.get('port'))
|
request('http://localhost:' + app.get('port'))
|
||||||
.get('/containers')
|
.get('/containers')
|
||||||
.set('Accept', 'application/json')
|
.set('Accept', 'application/json')
|
||||||
|
@ -121,7 +119,6 @@ describe('storage service', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should delete a container', function(done) {
|
it('should delete a container', function(done) {
|
||||||
|
|
||||||
request('http://localhost:' + app.get('port'))
|
request('http://localhost:' + app.get('port'))
|
||||||
.del('/containers/test-container')
|
.del('/containers/test-container')
|
||||||
.set('Accept', 'application/json')
|
.set('Accept', 'application/json')
|
||||||
|
@ -132,7 +129,6 @@ describe('storage service', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should list containers after delete', function(done) {
|
it('should list containers after delete', function(done) {
|
||||||
|
|
||||||
request('http://localhost:' + app.get('port'))
|
request('http://localhost:' + app.get('port'))
|
||||||
.get('/containers')
|
.get('/containers')
|
||||||
.set('Accept', 'application/json')
|
.set('Accept', 'application/json')
|
||||||
|
@ -145,7 +141,6 @@ describe('storage service', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should list files', function(done) {
|
it('should list files', function(done) {
|
||||||
|
|
||||||
request('http://localhost:' + app.get('port'))
|
request('http://localhost:' + app.get('port'))
|
||||||
.get('/containers/album1/files')
|
.get('/containers/album1/files')
|
||||||
.set('Accept', 'application/json')
|
.set('Accept', 'application/json')
|
||||||
|
@ -160,32 +155,30 @@ describe('storage service', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('uploads files', function(done) {
|
it('uploads files', function(done) {
|
||||||
|
|
||||||
request('http://localhost:' + app.get('port'))
|
request('http://localhost:' + app.get('port'))
|
||||||
.post('/containers/album1/upload')
|
.post('/containers/album1/upload')
|
||||||
.attach('image', path.join(__dirname, './fixtures/test.jpg'))
|
.attach('image', path.join(__dirname, './fixtures/test.jpg'))
|
||||||
.set('Accept', 'application/json')
|
.set('Accept', 'application/json')
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
assert.deepEqual(res.body, {"result": {"files": {"image": [
|
assert.deepEqual(res.body, {'result': {'files': {'image': [
|
||||||
{"container": "album1", "name": "test.jpg", "type": "image/jpeg",
|
{'container': 'album1', 'name': 'test.jpg', 'type': 'image/jpeg',
|
||||||
"size": 60475}
|
'size': 60475},
|
||||||
]}, "fields": {}}});
|
]}, 'fields': {}}});
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('uploads files with renamer', function(done) {
|
it('uploads files with renamer', function(done) {
|
||||||
|
|
||||||
request('http://localhost:' + app.get('port'))
|
request('http://localhost:' + app.get('port'))
|
||||||
.post('/imageContainers/album1/upload')
|
.post('/imageContainers/album1/upload')
|
||||||
.attach('image', path.join(__dirname, './fixtures/test.jpg'))
|
.attach('image', path.join(__dirname, './fixtures/test.jpg'))
|
||||||
.set('Accept', 'application/json')
|
.set('Accept', 'application/json')
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
assert.deepEqual(res.body, {"result": {"files": {"image": [
|
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', 'acl': 'public-read', 'size': 60475},
|
||||||
]}, "fields": {}}});
|
]}, 'fields': {}}});
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -204,7 +197,6 @@ describe('storage service', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('uploads file too large', function(done) {
|
it('uploads file too large', function(done) {
|
||||||
|
|
||||||
request('http://localhost:' + app.get('port'))
|
request('http://localhost:' + app.get('port'))
|
||||||
.post('/imageContainers/album1/upload')
|
.post('/imageContainers/album1/upload')
|
||||||
.attach('image', path.join(__dirname, './fixtures/largeImage.jpg'))
|
.attach('image', path.join(__dirname, './fixtures/largeImage.jpg'))
|
||||||
|
@ -219,7 +211,6 @@ describe('storage service', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should get file by name', function(done) {
|
it('should get file by name', function(done) {
|
||||||
|
|
||||||
request('http://localhost:' + app.get('port'))
|
request('http://localhost:' + app.get('port'))
|
||||||
.get('/containers/album1/files/test.jpg')
|
.get('/containers/album1/files/test.jpg')
|
||||||
.set('Accept', 'application/json')
|
.set('Accept', 'application/json')
|
||||||
|
@ -231,7 +222,6 @@ describe('storage service', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should get file by renamed file name', function(done) {
|
it('should get file by renamed file name', function(done) {
|
||||||
|
|
||||||
request('http://localhost:' + app.get('port'))
|
request('http://localhost:' + app.get('port'))
|
||||||
.get('/imageContainers/album1/files/image-test.jpg')
|
.get('/imageContainers/album1/files/image-test.jpg')
|
||||||
.set('Accept', 'application/json')
|
.set('Accept', 'application/json')
|
||||||
|
@ -243,7 +233,6 @@ describe('storage service', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('downloads files', function(done) {
|
it('downloads files', function(done) {
|
||||||
|
|
||||||
request('http://localhost:' + app.get('port'))
|
request('http://localhost:' + app.get('port'))
|
||||||
.get('/containers/album1/download/test.jpg')
|
.get('/containers/album1/download/test.jpg')
|
||||||
.expect('Content-Type', 'image/jpeg')
|
.expect('Content-Type', 'image/jpeg')
|
||||||
|
@ -294,7 +283,6 @@ describe('storage service', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should delete a file', function(done) {
|
it('should delete a file', function(done) {
|
||||||
|
|
||||||
request('http://localhost:' + app.get('port'))
|
request('http://localhost:' + app.get('port'))
|
||||||
.del('/containers/album1/files/test.jpg')
|
.del('/containers/album1/files/test.jpg')
|
||||||
.set('Accept', 'application/json')
|
.set('Accept', 'application/json')
|
||||||
|
@ -305,7 +293,6 @@ describe('storage service', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('reports errors if it fails to find the file to download', function(done) {
|
it('reports errors if it fails to find the file to download', function(done) {
|
||||||
|
|
||||||
request('http://localhost:' + app.get('port'))
|
request('http://localhost:' + app.get('port'))
|
||||||
.get('/containers/album1/download/test_not_exist.jpg')
|
.get('/containers/album1/download/test_not_exist.jpg')
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
|
|
Loading…
Reference in New Issue