From 160e1a44bf80297c9c9a5733aa4dac8d036b75a7 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Fri, 24 Jan 2014 09:32:48 -0800 Subject: [PATCH] Address some of the PR comments --- example/app-cloud.js | 91 +++++++++++++++++++++++++ example/app-loopback.js | 40 ----------- example/app.js | 109 ++++++++---------------------- lib/providers/filesystem/index.js | 24 ++++--- 4 files changed, 133 insertions(+), 131 deletions(-) create mode 100644 example/app-cloud.js delete mode 100644 example/app-loopback.js diff --git a/example/app-cloud.js b/example/app-cloud.js new file mode 100644 index 0000000..e5a6463 --- /dev/null +++ b/example/app-cloud.js @@ -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); + }); + }); + }); +}); + diff --git a/example/app-loopback.js b/example/app-loopback.js deleted file mode 100644 index 291c1ca..0000000 --- a/example/app-loopback.js +++ /dev/null @@ -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 = "

Storage Service Demo

" + - "List all containers

" + - "Upload to container c1:

" + - "

" - + "File to upload:
" - + "Notes about the file:
" - + "
" + - ""; - res.send(form); - res.end(); -}); - -app.listen(app.get('port')); -console.log('http://127.0.0.1:' + app.get('port')); diff --git a/example/app.js b/example/app.js index e5a6463..339b367 100644 --- a/example/app.js +++ b/example/app.js @@ -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 = "

Storage Service Demo

" + + "List all containers

" + + "Upload to container c1:

" + + "

" + + "File to upload:
" + + "Notes about the file:
" + + "
" + + ""; + res.send(form); + res.end(); }); +app.listen(app.get('port')); +console.log('http://127.0.0.1:' + app.get('port')); diff --git a/lib/providers/filesystem/index.js b/lib/providers/filesystem/index.js index 7c29dee..12a7c70 100644 --- a/lib/providers/filesystem/index.js +++ b/lib/providers/filesystem/index.js @@ -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) {