Reformat code
This commit is contained in:
parent
5db8da8c23
commit
70f5d6dc35
|
@ -2,26 +2,26 @@ var StorageService = require('../').StorageService;
|
|||
var path = require('path');
|
||||
|
||||
var rs = StorageService({
|
||||
provider: 'rackspace',
|
||||
username: 'strongloop',
|
||||
apiKey: 'your-rackspace-api-key'
|
||||
provider: 'rackspace',
|
||||
username: 'strongloop',
|
||||
apiKey: 'your-rackspace-api-key'
|
||||
});
|
||||
|
||||
// Container
|
||||
|
||||
rs.getContainers(function (err, containers) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return;
|
||||
}
|
||||
containers.forEach(function (c) {
|
||||
console.log('rackspace: ', c.name);
|
||||
c.getFiles(function (err, files) {
|
||||
files.forEach(function (f) {
|
||||
console.log('....', f.name);
|
||||
});
|
||||
});
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return;
|
||||
}
|
||||
containers.forEach(function (c) {
|
||||
console.log('rackspace: ', c.name);
|
||||
c.getFiles(function (err, files) {
|
||||
files.forEach(function (f) {
|
||||
console.log('....', f.name);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
/*
|
||||
|
@ -38,54 +38,51 @@ rs.getContainers(function (err, containers) {
|
|||
client.removeFile(container, file, function (err) { });
|
||||
*/
|
||||
|
||||
|
||||
var s3 = StorageService({
|
||||
provider: 'amazon',
|
||||
key: 'your-amazon-key',
|
||||
keyId: 'your-amazon-key-id'
|
||||
provider: 'amazon',
|
||||
key: 'your-amazon-key',
|
||||
keyId: 'your-amazon-key-id'
|
||||
});
|
||||
|
||||
s3.getContainers(function (err, containers) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return;
|
||||
}
|
||||
containers.forEach(function (c) {
|
||||
console.log('amazon: ', c.name);
|
||||
c.getFiles(function (err, files) {
|
||||
files.forEach(function (f) {
|
||||
console.log('....', f.name);
|
||||
});
|
||||
});
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return;
|
||||
}
|
||||
containers.forEach(function (c) {
|
||||
console.log('amazon: ', c.name);
|
||||
c.getFiles(function (err, files) {
|
||||
files.forEach(function (f) {
|
||||
console.log('....', f.name);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
var stream = s3.uploadStream('con1','test.jpg');
|
||||
var stream = s3.uploadStream('con1', 'test.jpg');
|
||||
var input = fs.createReadStream(path.join(__dirname, 'test.jpg')).pipe(stream);
|
||||
|
||||
|
||||
var local = StorageService({
|
||||
provider: 'filesystem',
|
||||
root: path.join(__dirname, 'storage')
|
||||
provider: 'filesystem',
|
||||
root: path.join(__dirname, 'storage')
|
||||
});
|
||||
|
||||
// Container
|
||||
|
||||
local.getContainers(function (err, containers) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return;
|
||||
}
|
||||
containers.forEach(function (c) {
|
||||
console.log('filesystem: ', c.name);
|
||||
c.getFiles(function (err, files) {
|
||||
files.forEach(function (f) {
|
||||
console.log('....', f.name);
|
||||
});
|
||||
});
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return;
|
||||
}
|
||||
containers.forEach(function (c) {
|
||||
console.log('filesystem: ', c.name);
|
||||
c.getFiles(function (err, files) {
|
||||
files.forEach(function (f) {
|
||||
console.log('....', f.name);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
var loopback = require('loopback')
|
||||
, app = module.exports = loopback();
|
||||
, app = module.exports = loopback();
|
||||
|
||||
var path = require('path');
|
||||
|
||||
|
@ -9,13 +9,13 @@ app.use(app.router);
|
|||
app.use(loopback.rest());
|
||||
|
||||
app.configure(function () {
|
||||
app.set('port', process.env.PORT || 3000);
|
||||
app.set('port', process.env.PORT || 3000);
|
||||
});
|
||||
|
||||
var ds = loopback.createDataSource({
|
||||
connector: require('../index'),
|
||||
provider: 'filesystem',
|
||||
root: path.join(__dirname, 'storage')
|
||||
connector: require('../index'),
|
||||
provider: 'filesystem',
|
||||
root: path.join(__dirname, 'storage')
|
||||
});
|
||||
|
||||
var container = ds.createModel('container');
|
||||
|
@ -23,17 +23,17 @@ var container = ds.createModel('container');
|
|||
app.model(container);
|
||||
|
||||
app.get('/', function (req, res, next) {
|
||||
res.setHeader('Content-Type', 'text/html');
|
||||
var form = "<html><body><h1>Storage Service Demo</h1>" +
|
||||
"<a href='/containers'>List all containers</a><p>" +
|
||||
"Upload to container c1: <p>" +
|
||||
"<form method='POST' enctype='multipart/form-data' action='/containers/container1/upload'>"
|
||||
+ "File to upload: <input type=file name=uploadedFiles multiple=true><br>"
|
||||
+ "Notes about the file: <input type=text name=note><br>"
|
||||
+ "<input type=submit value=Upload></form>" +
|
||||
"</body></html>";
|
||||
res.send(form);
|
||||
res.end();
|
||||
res.setHeader('Content-Type', 'text/html');
|
||||
var form = "<html><body><h1>Storage Service Demo</h1>" +
|
||||
"<a href='/containers'>List all containers</a><p>" +
|
||||
"Upload to container c1: <p>" +
|
||||
"<form method='POST' enctype='multipart/form-data' action='/containers/container1/upload'>"
|
||||
+ "File to upload: <input type=file name=uploadedFiles multiple=true><br>"
|
||||
+ "Notes about the file: <input type=text name=note><br>"
|
||||
+ "<input type=submit value=Upload></form>" +
|
||||
"</body></html>";
|
||||
res.send(form);
|
||||
res.end();
|
||||
});
|
||||
|
||||
app.listen(app.get('port'));
|
||||
|
|
|
@ -4,75 +4,75 @@ var express = require('express');
|
|||
var app = express();
|
||||
|
||||
app.configure(function () {
|
||||
app.set('port', process.env.PORT || 3001);
|
||||
app.set('views', __dirname + '/views');
|
||||
app.set('view engine', 'ejs');
|
||||
app.use(express.favicon());
|
||||
// app.use(express.logger('dev'));
|
||||
app.use(express.methodOverride());
|
||||
app.use(app.router);
|
||||
app.set('port', process.env.PORT || 3001);
|
||||
app.set('views', __dirname + '/views');
|
||||
app.set('view engine', 'ejs');
|
||||
app.use(express.favicon());
|
||||
// app.use(express.logger('dev'));
|
||||
app.use(express.methodOverride());
|
||||
app.use(app.router);
|
||||
});
|
||||
|
||||
var handler = new StorageService(
|
||||
{
|
||||
{
|
||||
provider: 'amazon',
|
||||
key: 'your-amazon-key',
|
||||
keyId: 'your-amazon-key-id'
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/', function (req, res, next) {
|
||||
res.setHeader('Content-Type', 'text/html');
|
||||
var form = "<html><body><h1>Storage Service Demo</h1>" +
|
||||
"<a href='/download'>List all containers</a><p>" +
|
||||
"Upload to container con1: <p>" +
|
||||
"<form method='POST' enctype='multipart/form-data' action='/upload/con1'>"
|
||||
+ "File to upload: <input type=file name=uploadedFiles multiple=true><br>"
|
||||
+ "Notes about the file: <input type=text name=note><br>"
|
||||
+ "<input type=submit value=Upload></form>" +
|
||||
"</body></html>";
|
||||
res.send(form);
|
||||
res.end();
|
||||
res.setHeader('Content-Type', 'text/html');
|
||||
var form = "<html><body><h1>Storage Service Demo</h1>" +
|
||||
"<a href='/download'>List all containers</a><p>" +
|
||||
"Upload to container con1: <p>" +
|
||||
"<form method='POST' enctype='multipart/form-data' action='/upload/con1'>"
|
||||
+ "File to upload: <input type=file name=uploadedFiles multiple=true><br>"
|
||||
+ "Notes about the file: <input type=text name=note><br>"
|
||||
+ "<input type=submit value=Upload></form>" +
|
||||
"</body></html>";
|
||||
res.send(form);
|
||||
res.end();
|
||||
});
|
||||
|
||||
app.post('/upload/:container', function (req, res, next) {
|
||||
handler.upload(req, res, function (err, result) {
|
||||
if (!err) {
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.send(200, result);
|
||||
} else {
|
||||
res.send(500, err);
|
||||
}
|
||||
});
|
||||
handler.upload(req, res, function (err, result) {
|
||||
if (!err) {
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.send(200, result);
|
||||
} else {
|
||||
res.send(500, err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/download', function (req, res, next) {
|
||||
handler.getContainers(function (err, containers) {
|
||||
var html = "<html><body><h1>Containers</h1><ul>";
|
||||
containers.forEach(function (f) {
|
||||
html += "<li><a href='/download/" + f.name + "'>" + f.name + "</a></li>"
|
||||
});
|
||||
html += "</ul><p><a href='/'>Home</a></p></body></html>";
|
||||
res.send(200, html);
|
||||
handler.getContainers(function (err, containers) {
|
||||
var html = "<html><body><h1>Containers</h1><ul>";
|
||||
containers.forEach(function (f) {
|
||||
html += "<li><a href='/download/" + f.name + "'>" + f.name + "</a></li>"
|
||||
});
|
||||
html += "</ul><p><a href='/'>Home</a></p></body></html>";
|
||||
res.send(200, html);
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/download/:container', function (req, res, next) {
|
||||
handler.getFiles(req.params.container, function (err, files) {
|
||||
var html = "<html><body><h1>Files in container " + req.params.container + "</h1><ul>";
|
||||
files.forEach(function (f) {
|
||||
html += "<li><a href='/download/" + f.container + "/" + f.name + "'>" + f.container + "/" + f.name + "</a></li>"
|
||||
});
|
||||
html += "</ul><p><a href='/'>Home</a></p></body></html>";
|
||||
res.send(200, html);
|
||||
handler.getFiles(req.params.container, function (err, files) {
|
||||
var html = "<html><body><h1>Files in container " + req.params.container + "</h1><ul>";
|
||||
files.forEach(function (f) {
|
||||
html += "<li><a href='/download/" + f.container + "/" + f.name + "'>" + f.container + "/" + f.name + "</a></li>"
|
||||
});
|
||||
html += "</ul><p><a href='/'>Home</a></p></body></html>";
|
||||
res.send(200, html);
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/download/:container/:file', function (req, res, next) {
|
||||
handler.download(req, res, function (err, result) {
|
||||
if (err) {
|
||||
res.send(500, err);
|
||||
}
|
||||
});
|
||||
handler.download(req, res, function (err, result) {
|
||||
if (err) {
|
||||
res.send(500, err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
app.listen(app.get('port'));
|
||||
|
|
|
@ -4,13 +4,13 @@ var express = require('express');
|
|||
var app = express();
|
||||
|
||||
app.configure(function () {
|
||||
app.set('port', process.env.PORT || 3001);
|
||||
app.set('views', __dirname + '/views');
|
||||
app.set('view engine', 'ejs');
|
||||
app.use(express.favicon());
|
||||
// app.use(express.logger('dev'));
|
||||
app.use(express.methodOverride());
|
||||
app.use(app.router);
|
||||
app.set('port', process.env.PORT || 3001);
|
||||
app.set('views', __dirname + '/views');
|
||||
app.set('view engine', 'ejs');
|
||||
app.use(express.favicon());
|
||||
// app.use(express.logger('dev'));
|
||||
app.use(express.methodOverride());
|
||||
app.use(app.router);
|
||||
});
|
||||
|
||||
// Create the container
|
||||
|
@ -20,58 +20,58 @@ mkdirp.sync('/tmp/storage/con1');
|
|||
var handler = new StorageService({provider: 'filesystem', root: '/tmp/storage'});
|
||||
|
||||
app.get('/', function (req, res, next) {
|
||||
res.setHeader('Content-Type', 'text/html');
|
||||
var form = "<html><body><h1>Storage Service Demo</h1>" +
|
||||
"<a href='/download'>List all containers</a><p>" +
|
||||
"Upload to container con1: <p>" +
|
||||
"<form method='POST' enctype='multipart/form-data' action='/upload/con1'>"
|
||||
+ "File to upload: <input type=file name=uploadedFiles multiple=true><br>"
|
||||
+ "Notes about the file: <input type=text name=note><br>"
|
||||
+ "<input type=submit value=Upload></form>" +
|
||||
"</body></html>";
|
||||
res.send(form);
|
||||
res.end();
|
||||
res.setHeader('Content-Type', 'text/html');
|
||||
var form = "<html><body><h1>Storage Service Demo</h1>" +
|
||||
"<a href='/download'>List all containers</a><p>" +
|
||||
"Upload to container con1: <p>" +
|
||||
"<form method='POST' enctype='multipart/form-data' action='/upload/con1'>"
|
||||
+ "File to upload: <input type=file name=uploadedFiles multiple=true><br>"
|
||||
+ "Notes about the file: <input type=text name=note><br>"
|
||||
+ "<input type=submit value=Upload></form>" +
|
||||
"</body></html>";
|
||||
res.send(form);
|
||||
res.end();
|
||||
});
|
||||
|
||||
app.post('/upload/:container', function (req, res, next) {
|
||||
handler.upload(req, res, function (err, result) {
|
||||
if (!err) {
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.send(200, result);
|
||||
} else {
|
||||
res.send(500, err);
|
||||
}
|
||||
});
|
||||
handler.upload(req, res, function (err, result) {
|
||||
if (!err) {
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.send(200, result);
|
||||
} else {
|
||||
res.send(500, err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/download', function (req, res, next) {
|
||||
handler.getContainers(function (err, containers) {
|
||||
var html = "<html><body><h1>Containers</h1><ul>";
|
||||
containers.forEach(function (f) {
|
||||
html += "<li><a href='/download/" + f.name + "'>" + f.name + "</a></li>"
|
||||
});
|
||||
html += "</ul><p><a href='/'>Home</a></p></body></html>";
|
||||
res.send(200, html);
|
||||
handler.getContainers(function (err, containers) {
|
||||
var html = "<html><body><h1>Containers</h1><ul>";
|
||||
containers.forEach(function (f) {
|
||||
html += "<li><a href='/download/" + f.name + "'>" + f.name + "</a></li>"
|
||||
});
|
||||
html += "</ul><p><a href='/'>Home</a></p></body></html>";
|
||||
res.send(200, html);
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/download/:container', function (req, res, next) {
|
||||
handler.getFiles(req.params.container, function (err, files) {
|
||||
var html = "<html><body><h1>Files in container " + req.params.container + "</h1><ul>";
|
||||
files.forEach(function (f) {
|
||||
html += "<li><a href='/download/" + f.container + "/" + f.name + "'>" + f.container + "/" + f.name + "</a></li>"
|
||||
});
|
||||
html += "</ul><p><a href='/'>Home</a></p></body></html>";
|
||||
res.send(200, html);
|
||||
handler.getFiles(req.params.container, function (err, files) {
|
||||
var html = "<html><body><h1>Files in container " + req.params.container + "</h1><ul>";
|
||||
files.forEach(function (f) {
|
||||
html += "<li><a href='/download/" + f.container + "/" + f.name + "'>" + f.container + "/" + f.name + "</a></li>"
|
||||
});
|
||||
html += "</ul><p><a href='/'>Home</a></p></body></html>";
|
||||
res.send(200, html);
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/download/:container/:file', function (req, res, next) {
|
||||
handler.download(req, res, function (err, result) {
|
||||
if (err) {
|
||||
res.send(500, err);
|
||||
}
|
||||
});
|
||||
handler.download(req, res, function (err, result) {
|
||||
if (err) {
|
||||
res.send(500, err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
app.listen(app.get('port'));
|
||||
|
|
|
@ -4,19 +4,19 @@
|
|||
* @returns {*}
|
||||
*/
|
||||
function createClient(options) {
|
||||
options = options || {};
|
||||
var provider = options.provider || 'filesystem';
|
||||
var handler;
|
||||
options = options || {};
|
||||
var provider = options.provider || 'filesystem';
|
||||
var handler;
|
||||
|
||||
try {
|
||||
// Try to load the provider from providers folder
|
||||
handler = require('./providers/' + provider);
|
||||
} catch (err) {
|
||||
// Fall back to pkgcloud
|
||||
handler = require('pkgcloud').storage;
|
||||
}
|
||||
|
||||
return handler.createClient(options);
|
||||
try {
|
||||
// Try to load the provider from providers folder
|
||||
handler = require('./providers/' + provider);
|
||||
} catch (err) {
|
||||
// Fall back to pkgcloud
|
||||
handler = require('pkgcloud').storage;
|
||||
}
|
||||
|
||||
return handler.createClient(options);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -25,13 +25,13 @@ function createClient(options) {
|
|||
* @returns {*}
|
||||
*/
|
||||
function getProvider(provider) {
|
||||
try {
|
||||
// Try to load the provider from providers folder
|
||||
return require('./providers/' + provider);
|
||||
} catch (err) {
|
||||
// Fall back to pkgcloud
|
||||
return require('pkgcloud').providers[provider];
|
||||
}
|
||||
try {
|
||||
// Try to load the provider from providers folder
|
||||
return require('./providers/' + provider);
|
||||
} catch (err) {
|
||||
// Fall back to pkgcloud
|
||||
return require('pkgcloud').providers[provider];
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.createClient = createClient;
|
||||
|
|
|
@ -5,22 +5,24 @@ var StorageService = require('./storage-service');
|
|||
* @param callback
|
||||
*/
|
||||
exports.initialize = function (dataSource, callback) {
|
||||
var settings = dataSource.settings || {};
|
||||
var settings = dataSource.settings || {};
|
||||
|
||||
var connector = new StorageService(settings);
|
||||
dataSource.connector = connector;
|
||||
dataSource.connector.dataSource = dataSource;
|
||||
var connector = new StorageService(settings);
|
||||
dataSource.connector = connector;
|
||||
dataSource.connector.dataSource = dataSource;
|
||||
|
||||
connector.DataAccessObject = function() {};
|
||||
for (var m in StorageService.prototype) {
|
||||
var method = StorageService.prototype[m];
|
||||
if ('function' === typeof method) {
|
||||
connector.DataAccessObject[m] = method.bind(connector);
|
||||
for(var k in method) {
|
||||
connector.DataAccessObject[m][k] = method[k];
|
||||
}
|
||||
}
|
||||
connector.DataAccessObject = function () {
|
||||
};
|
||||
for (var m in StorageService.prototype) {
|
||||
var method = StorageService.prototype[m];
|
||||
if ('function' === typeof method) {
|
||||
connector.DataAccessObject[m] = method.bind(connector);
|
||||
for (var k in method) {
|
||||
connector.DataAccessObject[m][k] = method[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
connector.define = function(model, properties, settings) {};
|
||||
connector.define = function (model, properties, settings) {
|
||||
};
|
||||
};
|
||||
|
|
|
@ -10,96 +10,96 @@ var StringDecoder = require('string_decoder').StringDecoder;
|
|||
* @param {Function} cb The callback
|
||||
*/
|
||||
exports.upload = function (provider, req, res, container, cb) {
|
||||
var form = new IncomingForm(this.options);
|
||||
container = container || req.params.container;
|
||||
var fields = {}, files = {};
|
||||
form.handlePart = function (part) {
|
||||
var self = this;
|
||||
var form = new IncomingForm(this.options);
|
||||
container = container || req.params.container;
|
||||
var fields = {}, files = {};
|
||||
form.handlePart = function (part) {
|
||||
var self = this;
|
||||
|
||||
if (part.filename === undefined) {
|
||||
var value = ''
|
||||
, decoder = new StringDecoder(this.encoding);
|
||||
if (part.filename === undefined) {
|
||||
var value = ''
|
||||
, decoder = new StringDecoder(this.encoding);
|
||||
|
||||
part.on('data', function (buffer) {
|
||||
self._fieldsSize += buffer.length;
|
||||
if (self._fieldsSize > self.maxFieldsSize) {
|
||||
self._error(new Error('maxFieldsSize exceeded, received ' + self._fieldsSize + ' bytes of field data'));
|
||||
return;
|
||||
}
|
||||
value += decoder.write(buffer);
|
||||
});
|
||||
|
||||
part.on('end', function () {
|
||||
var values = fields[part.name];
|
||||
if(values === undefined) {
|
||||
values = [value];
|
||||
fields[part.name] = values;
|
||||
} else {
|
||||
values.push(value);
|
||||
}
|
||||
self.emit('field', part.name, value);
|
||||
});
|
||||
return;
|
||||
part.on('data', function (buffer) {
|
||||
self._fieldsSize += buffer.length;
|
||||
if (self._fieldsSize > self.maxFieldsSize) {
|
||||
self._error(new Error('maxFieldsSize exceeded, received ' + self._fieldsSize + ' bytes of field data'));
|
||||
return;
|
||||
}
|
||||
value += decoder.write(buffer);
|
||||
});
|
||||
|
||||
this._flushing++;
|
||||
|
||||
var file = {
|
||||
container: container,
|
||||
name: part.filename,
|
||||
type: part.mime
|
||||
};
|
||||
|
||||
self.emit('fileBegin', part.name, file);
|
||||
|
||||
var headers = {};
|
||||
if('content-type' in part.headers) {
|
||||
headers['content-type'] = part.headers['content-type'];
|
||||
part.on('end', function () {
|
||||
var values = fields[part.name];
|
||||
if (values === undefined) {
|
||||
values = [value];
|
||||
fields[part.name] = values;
|
||||
} else {
|
||||
values.push(value);
|
||||
}
|
||||
var writer = provider.upload({container: container, remote: part.filename});
|
||||
self.emit('field', part.name, value);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
var endFunc = function () {
|
||||
self._flushing--;
|
||||
var values = files[part.name];
|
||||
if(values === undefined) {
|
||||
values = [file];
|
||||
files[part.name] = values;
|
||||
} else {
|
||||
values.push(file);
|
||||
}
|
||||
self.emit('file', part.name, file);
|
||||
self._maybeEnd();
|
||||
};
|
||||
this._flushing++;
|
||||
|
||||
/*
|
||||
part.on('data', function (buffer) {
|
||||
self.pause();
|
||||
writer.write(buffer, function () {
|
||||
// pkgcloud stream doesn't make callbacks
|
||||
});
|
||||
self.resume();
|
||||
});
|
||||
|
||||
part.on('end', function () {
|
||||
|
||||
writer.end(); // pkgcloud stream doesn't make callbacks
|
||||
endFunc();
|
||||
});
|
||||
*/
|
||||
|
||||
part.pipe(writer, { end: false });
|
||||
part.on("end", function() {
|
||||
writer.end();
|
||||
endFunc();
|
||||
});
|
||||
var file = {
|
||||
container: container,
|
||||
name: part.filename,
|
||||
type: part.mime
|
||||
};
|
||||
|
||||
form.parse(req, function (err, _fields, _files) {
|
||||
if(err) {
|
||||
console.error(err);
|
||||
}
|
||||
cb && cb(err, {files: files, fields: fields});
|
||||
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 endFunc = function () {
|
||||
self._flushing--;
|
||||
var values = files[part.name];
|
||||
if (values === undefined) {
|
||||
values = [file];
|
||||
files[part.name] = values;
|
||||
} else {
|
||||
values.push(file);
|
||||
}
|
||||
self.emit('file', part.name, file);
|
||||
self._maybeEnd();
|
||||
};
|
||||
|
||||
/*
|
||||
part.on('data', function (buffer) {
|
||||
self.pause();
|
||||
writer.write(buffer, function () {
|
||||
// pkgcloud stream doesn't make callbacks
|
||||
});
|
||||
self.resume();
|
||||
});
|
||||
|
||||
part.on('end', function () {
|
||||
|
||||
writer.end(); // pkgcloud stream doesn't make callbacks
|
||||
endFunc();
|
||||
});
|
||||
*/
|
||||
|
||||
part.pipe(writer, { end: false });
|
||||
part.on("end", function () {
|
||||
writer.end();
|
||||
endFunc();
|
||||
});
|
||||
};
|
||||
|
||||
form.parse(req, function (err, _fields, _files) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
}
|
||||
cb && cb(err, {files: files, fields: fields});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -111,15 +111,15 @@ exports.upload = function (provider, req, res, container, cb) {
|
|||
* @param {String} file The file name
|
||||
* @param {Function} cb The callback
|
||||
*/
|
||||
exports.download = function(provider, req, res, container, file, cb) {
|
||||
var reader = provider.download({
|
||||
container: container || req && req.params.container,
|
||||
remote: file || req && req.params.file
|
||||
});
|
||||
reader.pipe(res);
|
||||
reader.on('error', function(err) {
|
||||
res.send(500, { error: err });
|
||||
});
|
||||
exports.download = function (provider, req, res, container, file, cb) {
|
||||
var reader = provider.download({
|
||||
container: container || req && req.params.container,
|
||||
remote: file || req && req.params.file
|
||||
});
|
||||
reader.pipe(res);
|
||||
reader.on('error', function (err) {
|
||||
res.send(500, { error: err });
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -24,22 +24,22 @@ function StorageService(options) {
|
|||
function map(obj) {
|
||||
return obj;
|
||||
/*
|
||||
if (!obj || typeof obj !== 'object') {
|
||||
return obj;
|
||||
}
|
||||
var data = {};
|
||||
for (var i in obj) {
|
||||
if (obj.hasOwnProperty(i) && typeof obj[i] !== 'function'
|
||||
&& typeof obj[i] !== 'object') {
|
||||
if (i === 'newListener' || i === 'delimiter' || i === 'wildcard') {
|
||||
// Skip properties from the base class
|
||||
continue;
|
||||
}
|
||||
data[i] = obj[i];
|
||||
}
|
||||
}
|
||||
return data;
|
||||
*/
|
||||
if (!obj || typeof obj !== 'object') {
|
||||
return obj;
|
||||
}
|
||||
var data = {};
|
||||
for (var i in obj) {
|
||||
if (obj.hasOwnProperty(i) && typeof obj[i] !== 'function'
|
||||
&& typeof obj[i] !== 'object') {
|
||||
if (i === 'newListener' || i === 'delimiter' || i === 'wildcard') {
|
||||
// Skip properties from the base class
|
||||
continue;
|
||||
}
|
||||
data[i] = obj[i];
|
||||
}
|
||||
}
|
||||
return data;
|
||||
*/
|
||||
}
|
||||
|
||||
StorageService.prototype.getContainers = function (cb) {
|
||||
|
|
268
test/fs.test.js
268
test/fs.test.js
|
@ -5,143 +5,143 @@ var path = require('path');
|
|||
|
||||
describe('FileSystem based storage provider', function () {
|
||||
|
||||
describe('container apis', function () {
|
||||
var client = null;
|
||||
it('should require an existing directory as the root', function (done) {
|
||||
client = new FileSystemProvider({root: path.join(__dirname, 'storage')});
|
||||
process.nextTick(done);
|
||||
});
|
||||
|
||||
it('should complain if the root directory doesn\'t exist', function (done) {
|
||||
try {
|
||||
client = new FileSystemProvider({root: path.join(__dirname, '_storage')});
|
||||
process.nextTick(done.bind(null, 'Error'));
|
||||
} catch (err) {
|
||||
// Should be here
|
||||
process.nextTick(done);
|
||||
}
|
||||
});
|
||||
|
||||
it('should return an empty list of containers', function (done) {
|
||||
client.getContainers(function (err, containers) {
|
||||
assert(!err);
|
||||
assert.equal(0, containers.length);
|
||||
done(err, containers);
|
||||
});
|
||||
});
|
||||
|
||||
it('should create a new container', function (done) {
|
||||
client.createContainer({name: 'c1'}, function (err, container) {
|
||||
assert(!err);
|
||||
done(err, container);
|
||||
});
|
||||
});
|
||||
|
||||
it('should get a container c1', function (done) {
|
||||
client.getContainer('c1', function (err, container) {
|
||||
assert(!err);
|
||||
done(err, container);
|
||||
});
|
||||
});
|
||||
|
||||
it('should not get a container c2', function (done) {
|
||||
client.getContainer('c2', function (err, container) {
|
||||
assert(err);
|
||||
done(null, container);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return one container', function (done) {
|
||||
client.getContainers(function (err, containers) {
|
||||
assert(!err);
|
||||
assert.equal(1, containers.length);
|
||||
done(err, containers);
|
||||
});
|
||||
});
|
||||
|
||||
it('should destroy a container c1', function (done) {
|
||||
client.destroyContainer('c1', function (err, container) {
|
||||
assert(!err);
|
||||
done(err, container);
|
||||
});
|
||||
});
|
||||
|
||||
it('should not get a container c1 after destroy', function (done) {
|
||||
client.getContainer('c1', function (err, container) {
|
||||
assert(err);
|
||||
done(null, container);
|
||||
});
|
||||
});
|
||||
describe('container apis', function () {
|
||||
var client = null;
|
||||
it('should require an existing directory as the root', function (done) {
|
||||
client = new FileSystemProvider({root: path.join(__dirname, 'storage')});
|
||||
process.nextTick(done);
|
||||
});
|
||||
|
||||
describe('file apis', function () {
|
||||
var fs = require('fs');
|
||||
var client = new FileSystemProvider({root: path.join(__dirname, 'storage')});
|
||||
|
||||
it('should create a new container', function (done) {
|
||||
client.createContainer({name: 'c1'}, function (err, container) {
|
||||
assert(!err);
|
||||
done(err, container);
|
||||
});
|
||||
});
|
||||
|
||||
it('should upload a file', function (done) {
|
||||
var writer = client.upload({container: 'c1', remote: 'f1.txt'});
|
||||
fs.createReadStream(path.join(__dirname, 'files/f1.txt')).pipe(writer);
|
||||
writer.on('finish', done);
|
||||
writer.on('error', done);
|
||||
});
|
||||
|
||||
it('should download a file', function (done) {
|
||||
var reader = client.download({
|
||||
container: 'c1',
|
||||
remote: 'f1.txt'
|
||||
});
|
||||
reader.pipe(fs.createWriteStream(path.join(__dirname, 'files/f1_downloaded.txt')));
|
||||
reader.on('end', done);
|
||||
reader.on('error', done);
|
||||
});
|
||||
|
||||
it('should get files for a container', function (done) {
|
||||
client.getFiles('c1', function (err, files) {
|
||||
assert(!err);
|
||||
assert.equal(1, files.length);
|
||||
done(err, files);
|
||||
});
|
||||
});
|
||||
|
||||
it('should get a file', function (done) {
|
||||
client.getFile('c1', 'f1.txt', function (err, f) {
|
||||
assert(!err);
|
||||
assert.ok(f);
|
||||
done(err, f);
|
||||
});
|
||||
});
|
||||
|
||||
it('should remove a file', function (done) {
|
||||
client.removeFile('c1', 'f1.txt', function (err) {
|
||||
assert(!err);
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should get no files from a container', function (done) {
|
||||
client.getFiles('c1', function (err, files) {
|
||||
assert(!err);
|
||||
assert.equal(0, files.length);
|
||||
done(err, files);
|
||||
});
|
||||
});
|
||||
|
||||
it('should destroy a container c1', function (done) {
|
||||
client.destroyContainer('c1', function (err, container) {
|
||||
// console.error(err);
|
||||
assert(!err);
|
||||
done(err, container);
|
||||
});
|
||||
});
|
||||
|
||||
it('should complain if the root directory doesn\'t exist', function (done) {
|
||||
try {
|
||||
client = new FileSystemProvider({root: path.join(__dirname, '_storage')});
|
||||
process.nextTick(done.bind(null, 'Error'));
|
||||
} catch (err) {
|
||||
// Should be here
|
||||
process.nextTick(done);
|
||||
}
|
||||
});
|
||||
|
||||
it('should return an empty list of containers', function (done) {
|
||||
client.getContainers(function (err, containers) {
|
||||
assert(!err);
|
||||
assert.equal(0, containers.length);
|
||||
done(err, containers);
|
||||
});
|
||||
});
|
||||
|
||||
it('should create a new container', function (done) {
|
||||
client.createContainer({name: 'c1'}, function (err, container) {
|
||||
assert(!err);
|
||||
done(err, container);
|
||||
});
|
||||
});
|
||||
|
||||
it('should get a container c1', function (done) {
|
||||
client.getContainer('c1', function (err, container) {
|
||||
assert(!err);
|
||||
done(err, container);
|
||||
});
|
||||
});
|
||||
|
||||
it('should not get a container c2', function (done) {
|
||||
client.getContainer('c2', function (err, container) {
|
||||
assert(err);
|
||||
done(null, container);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return one container', function (done) {
|
||||
client.getContainers(function (err, containers) {
|
||||
assert(!err);
|
||||
assert.equal(1, containers.length);
|
||||
done(err, containers);
|
||||
});
|
||||
});
|
||||
|
||||
it('should destroy a container c1', function (done) {
|
||||
client.destroyContainer('c1', function (err, container) {
|
||||
assert(!err);
|
||||
done(err, container);
|
||||
});
|
||||
});
|
||||
|
||||
it('should not get a container c1 after destroy', function (done) {
|
||||
client.getContainer('c1', function (err, container) {
|
||||
assert(err);
|
||||
done(null, container);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('file apis', function () {
|
||||
var fs = require('fs');
|
||||
var client = new FileSystemProvider({root: path.join(__dirname, 'storage')});
|
||||
|
||||
it('should create a new container', function (done) {
|
||||
client.createContainer({name: 'c1'}, function (err, container) {
|
||||
assert(!err);
|
||||
done(err, container);
|
||||
});
|
||||
});
|
||||
|
||||
it('should upload a file', function (done) {
|
||||
var writer = client.upload({container: 'c1', remote: 'f1.txt'});
|
||||
fs.createReadStream(path.join(__dirname, 'files/f1.txt')).pipe(writer);
|
||||
writer.on('finish', done);
|
||||
writer.on('error', done);
|
||||
});
|
||||
|
||||
it('should download a file', function (done) {
|
||||
var reader = client.download({
|
||||
container: 'c1',
|
||||
remote: 'f1.txt'
|
||||
});
|
||||
reader.pipe(fs.createWriteStream(path.join(__dirname, 'files/f1_downloaded.txt')));
|
||||
reader.on('end', done);
|
||||
reader.on('error', done);
|
||||
});
|
||||
|
||||
it('should get files for a container', function (done) {
|
||||
client.getFiles('c1', function (err, files) {
|
||||
assert(!err);
|
||||
assert.equal(1, files.length);
|
||||
done(err, files);
|
||||
});
|
||||
});
|
||||
|
||||
it('should get a file', function (done) {
|
||||
client.getFile('c1', 'f1.txt', function (err, f) {
|
||||
assert(!err);
|
||||
assert.ok(f);
|
||||
done(err, f);
|
||||
});
|
||||
});
|
||||
|
||||
it('should remove a file', function (done) {
|
||||
client.removeFile('c1', 'f1.txt', function (err) {
|
||||
assert(!err);
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should get no files from a container', function (done) {
|
||||
client.getFiles('c1', function (err, files) {
|
||||
assert(!err);
|
||||
assert.equal(0, files.length);
|
||||
done(err, files);
|
||||
});
|
||||
});
|
||||
|
||||
it('should destroy a container c1', function (done) {
|
||||
client.destroyContainer('c1', function (err, container) {
|
||||
// console.error(err);
|
||||
assert(!err);
|
||||
done(err, container);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
|
|
@ -7,124 +7,124 @@ var storageService = new StorageService({root: path.join(__dirname, 'storage'),
|
|||
|
||||
describe('Storage service', function () {
|
||||
|
||||
describe('container apis', function () {
|
||||
describe('container apis', function () {
|
||||
|
||||
it('should return an empty list of containers', function (done) {
|
||||
storageService.getContainers(function (err, containers) {
|
||||
assert(!err);
|
||||
assert.equal(0, containers.length);
|
||||
done(err, containers);
|
||||
});
|
||||
});
|
||||
|
||||
it('should create a new container', function (done) {
|
||||
storageService.createContainer({name: 'c1'}, function (err, container) {
|
||||
assert(!err);
|
||||
done(err, container);
|
||||
});
|
||||
});
|
||||
|
||||
it('should get a container c1', function (done) {
|
||||
storageService.getContainer('c1', function (err, container) {
|
||||
assert(!err);
|
||||
done(err, container);
|
||||
});
|
||||
});
|
||||
|
||||
it('should not get a container c2', function (done) {
|
||||
storageService.getContainer('c2', function (err, container) {
|
||||
assert(err);
|
||||
done(null, container);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return one container', function (done) {
|
||||
storageService.getContainers(function (err, containers) {
|
||||
assert(!err);
|
||||
assert.equal(1, containers.length);
|
||||
done(err, containers);
|
||||
});
|
||||
});
|
||||
|
||||
it('should destroy a container c1', function (done) {
|
||||
storageService.destroyContainer('c1', function (err, container) {
|
||||
assert(!err);
|
||||
done(err, container);
|
||||
});
|
||||
});
|
||||
|
||||
it('should not get a container c1 after destroy', function (done) {
|
||||
storageService.getContainer('c1', function (err, container) {
|
||||
assert(err);
|
||||
done(null, container);
|
||||
});
|
||||
});
|
||||
it('should return an empty list of containers', function (done) {
|
||||
storageService.getContainers(function (err, containers) {
|
||||
assert(!err);
|
||||
assert.equal(0, containers.length);
|
||||
done(err, containers);
|
||||
});
|
||||
});
|
||||
|
||||
describe('file apis', function () {
|
||||
var fs = require('fs');
|
||||
|
||||
it('should create a new container', function (done) {
|
||||
storageService.createContainer({name: 'c1'}, function (err, container) {
|
||||
assert(!err);
|
||||
done(err, container);
|
||||
});
|
||||
});
|
||||
|
||||
it('should upload a file', function (done) {
|
||||
var writer = storageService.uploadStream('c1', 'f1.txt');
|
||||
fs.createReadStream(path.join(__dirname, 'files/f1.txt')).pipe(writer);
|
||||
writer.on('finish', done);
|
||||
writer.on('error', done);
|
||||
});
|
||||
|
||||
it('should download a file', function (done) {
|
||||
var reader = storageService.downloadStream('c1','f1.txt');
|
||||
reader.pipe(fs.createWriteStream(path.join(__dirname, 'files/f1_downloaded.txt')));
|
||||
reader.on('end', done);
|
||||
reader.on('error', done);
|
||||
});
|
||||
|
||||
it('should get files for a container', function (done) {
|
||||
storageService.getFiles('c1', function (err, files) {
|
||||
assert(!err);
|
||||
assert.equal(1, files.length);
|
||||
done(err, files);
|
||||
});
|
||||
});
|
||||
|
||||
it('should get a file', function (done) {
|
||||
storageService.getFile('c1', 'f1.txt', function (err, f) {
|
||||
assert(!err);
|
||||
assert.ok(f);
|
||||
done(err, f);
|
||||
});
|
||||
});
|
||||
|
||||
it('should remove a file', function (done) {
|
||||
storageService.removeFile('c1', 'f1.txt', function (err) {
|
||||
assert(!err);
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should get no files from a container', function (done) {
|
||||
storageService.getFiles('c1', function (err, files) {
|
||||
assert(!err);
|
||||
assert.equal(0, files.length);
|
||||
done(err, files);
|
||||
});
|
||||
});
|
||||
|
||||
it('should destroy a container c1', function (done) {
|
||||
storageService.destroyContainer('c1', function (err, container) {
|
||||
// console.error(err);
|
||||
assert(!err);
|
||||
done(err, container);
|
||||
});
|
||||
});
|
||||
|
||||
it('should create a new container', function (done) {
|
||||
storageService.createContainer({name: 'c1'}, function (err, container) {
|
||||
assert(!err);
|
||||
done(err, container);
|
||||
});
|
||||
});
|
||||
|
||||
it('should get a container c1', function (done) {
|
||||
storageService.getContainer('c1', function (err, container) {
|
||||
assert(!err);
|
||||
done(err, container);
|
||||
});
|
||||
});
|
||||
|
||||
it('should not get a container c2', function (done) {
|
||||
storageService.getContainer('c2', function (err, container) {
|
||||
assert(err);
|
||||
done(null, container);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return one container', function (done) {
|
||||
storageService.getContainers(function (err, containers) {
|
||||
assert(!err);
|
||||
assert.equal(1, containers.length);
|
||||
done(err, containers);
|
||||
});
|
||||
});
|
||||
|
||||
it('should destroy a container c1', function (done) {
|
||||
storageService.destroyContainer('c1', function (err, container) {
|
||||
assert(!err);
|
||||
done(err, container);
|
||||
});
|
||||
});
|
||||
|
||||
it('should not get a container c1 after destroy', function (done) {
|
||||
storageService.getContainer('c1', function (err, container) {
|
||||
assert(err);
|
||||
done(null, container);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('file apis', function () {
|
||||
var fs = require('fs');
|
||||
|
||||
it('should create a new container', function (done) {
|
||||
storageService.createContainer({name: 'c1'}, function (err, container) {
|
||||
assert(!err);
|
||||
done(err, container);
|
||||
});
|
||||
});
|
||||
|
||||
it('should upload a file', function (done) {
|
||||
var writer = storageService.uploadStream('c1', 'f1.txt');
|
||||
fs.createReadStream(path.join(__dirname, 'files/f1.txt')).pipe(writer);
|
||||
writer.on('finish', done);
|
||||
writer.on('error', done);
|
||||
});
|
||||
|
||||
it('should download a file', function (done) {
|
||||
var reader = storageService.downloadStream('c1', 'f1.txt');
|
||||
reader.pipe(fs.createWriteStream(path.join(__dirname, 'files/f1_downloaded.txt')));
|
||||
reader.on('end', done);
|
||||
reader.on('error', done);
|
||||
});
|
||||
|
||||
it('should get files for a container', function (done) {
|
||||
storageService.getFiles('c1', function (err, files) {
|
||||
assert(!err);
|
||||
assert.equal(1, files.length);
|
||||
done(err, files);
|
||||
});
|
||||
});
|
||||
|
||||
it('should get a file', function (done) {
|
||||
storageService.getFile('c1', 'f1.txt', function (err, f) {
|
||||
assert(!err);
|
||||
assert.ok(f);
|
||||
done(err, f);
|
||||
});
|
||||
});
|
||||
|
||||
it('should remove a file', function (done) {
|
||||
storageService.removeFile('c1', 'f1.txt', function (err) {
|
||||
assert(!err);
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should get no files from a container', function (done) {
|
||||
storageService.getFiles('c1', function (err, files) {
|
||||
assert(!err);
|
||||
assert.equal(0, files.length);
|
||||
done(err, files);
|
||||
});
|
||||
});
|
||||
|
||||
it('should destroy a container c1', function (done) {
|
||||
storageService.destroyContainer('c1', function (err, container) {
|
||||
// console.error(err);
|
||||
assert(!err);
|
||||
done(err, container);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue