Address some of the PR comments

This commit is contained in:
Raymond Feng 2014-01-24 09:32:48 -08:00
parent 0f94079082
commit 160e1a44bf
4 changed files with 133 additions and 131 deletions

91
example/app-cloud.js Normal file
View File

@ -0,0 +1,91 @@
var StorageService = require('../').StorageService;
var path = require('path');
var rs = StorageService({
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);
});
});
});
});
/*
client.createContainer(options, function (err, container) { });
client.destroyContainer(containerName, function (err) { });
client.getContainer(containerName, function (err, container) { });
// File
client.upload(options, function (err) { });
client.download(options, function (err) { });
client.getFiles(container, function (err, files) { });
client.getFile(container, file, function (err, server) { });
client.removeFile(container, file, function (err) { });
*/
var s3 = StorageService({
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);
});
});
});
});
var fs = require('fs');
var path = require('path');
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')
});
// 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);
});
});
});
});

View File

@ -1,40 +0,0 @@
var loopback = require('loopback')
, app = module.exports = loopback();
var path = require('path');
app.use(app.router);
// expose a rest api
app.use(loopback.rest());
app.configure(function () {
app.set('port', process.env.PORT || 3000);
});
var ds = loopback.createDataSource({
connector: require('../index'),
provider: 'filesystem',
root: path.join(__dirname, 'storage')
});
var Storage = ds.createModel('container');
app.model(Storage);
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();
});
app.listen(app.get('port'));
console.log('http://127.0.0.1:' + app.get('port'));

View File

@ -1,91 +1,40 @@
var StorageService = require('../').StorageService;
var loopback = require('loopback')
, app = module.exports = loopback();
var path = require('path');
var rs = StorageService({
provider: 'rackspace',
username: 'strongloop',
apiKey: 'your-rackspace-api-key'
app.use(app.router);
// expose a rest api
app.use(loopback.rest());
app.configure(function () {
app.set('port', process.env.PORT || 3000);
});
// 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);
});
});
});
});
/*
client.createContainer(options, function (err, container) { });
client.destroyContainer(containerName, function (err) { });
client.getContainer(containerName, function (err, container) { });
// File
client.upload(options, function (err) { });
client.download(options, function (err) { });
client.getFiles(container, function (err, files) { });
client.getFile(container, file, function (err, server) { });
client.removeFile(container, file, function (err) { });
*/
var s3 = StorageService({
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);
});
});
});
});
var fs = require('fs');
var path = require('path');
var stream = s3.uploadStream('con1','test.jpg');
var input = fs.createReadStream(path.join(__dirname, 'test.jpg')).pipe(stream);
var local = StorageService({
var ds = loopback.createDataSource({
connector: require('../index'),
provider: 'filesystem',
root: path.join(__dirname, 'storage')
});
// Container
var container = ds.createModel('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);
});
});
});
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();
});
app.listen(app.get('port'));
console.log('http://127.0.0.1:' + app.get('port'));

View File

@ -20,11 +20,11 @@ function FileSystemProvider(options) {
this.root = options.root;
var exists = fs.existsSync(this.root);
if (!exists) {
throw new Error('Path does not exist: ' + this.root);
throw new Error('FileSystemProvider: Path does not exist: ' + this.root);
}
var stat = fs.statSync(this.root);
if (!stat.isDirectory()) {
throw new Error('Invalid directory: ' + this.root);
throw new Error('FileSystemProvider: Invalid directory: ' + this.root);
}
}
@ -34,7 +34,7 @@ function validateName(name, cb) {
if (!name) {
cb && process.nextTick(cb.bind(null, new Error('Invalid name: ' + name)));
if (!cb) {
console.error('Invalid name: ', name);
console.error('FileSystemProvider: Invalid name: ', name);
}
return false;
}
@ -42,9 +42,10 @@ function validateName(name, cb) {
if (match && match.index === 0 && match[0].length === name.length) {
return true;
} else {
cb && process.nextTick(cb.bind(null, new Error('Invalid name: ' + name)));
cb && process.nextTick(cb.bind(null,
new Error('FileSystemProvider: Invalid name: ' + name)));
if (!cb) {
console.error('Invalid name: ', name);
console.error('FileSystemProvider: Invalid name: ', name);
}
return false;
}
@ -57,7 +58,7 @@ FileSystemProvider.prototype.getContainers = function (cb) {
var containers = [];
var tasks = [];
files.forEach(function (f) {
tasks.push(fs.stat.bind(null, path.join(self.root, f)));
tasks.push(fs.stat.bind(fs, path.join(self.root, f)));
});
async.parallel(tasks, function (err, stats) {
if (err) {
@ -95,7 +96,7 @@ FileSystemProvider.prototype.destroyContainer = function (containerName, cb) {
fs.readdir(dir, function (err, files) {
var tasks = [];
files.forEach(function (f) {
tasks.push(fs.unlink.bind(null, path.join(dir, f)));
tasks.push(fs.unlink.bind(fs, path.join(dir, f)));
});
async.parallel(tasks, function (err) {
if (err) {
@ -132,9 +133,10 @@ FileSystemProvider.prototype.upload = function (options, cb) {
if (!validateName(file, cb)) return;
var filePath = path.join(this.root, container, file);
var fileOpts = {flags: 'w+',
encoding: null,
mode: 0666 };
var fileOpts = {flags: options.flags || 'w+',
encoding: options.encoding || null,
mode: options.mode || 0666
};
try {
return fs.createWriteStream(filePath, fileOpts);
@ -173,7 +175,7 @@ FileSystemProvider.prototype.getFiles = function (container, download, cb) {
var files = [];
var tasks = [];
entries.forEach(function (f) {
tasks.push(fs.stat.bind(null, path.join(dir, f)));
tasks.push(fs.stat.bind(fs, path.join(dir, f)));
});
async.parallel(tasks, function (err, stats) {
if (err) {