diff --git a/example/app.js b/example/app.js
index 34d9b9e..e011ea6 100644
--- a/example/app.js
+++ b/example/app.js
@@ -1,7 +1,7 @@
-var storage = require('../');
+var StorageService = require('../');
var path = require('path');
-var rs = storage({
+var rs = StorageService({
provider: 'rackspace',
username: 'strongloop',
apiKey: 'your-rackspace-api-key'
@@ -39,7 +39,7 @@ rs.getContainers(function (err, containers) {
*/
-var s3 = storage({
+var s3 = StorageService({
provider: 'amazon',
key: 'your-amazon-key',
keyId: 'your-amazon-key-id'
@@ -60,7 +60,7 @@ s3.getContainers(function (err, containers) {
});
});
-var fs = storage({
+var fs = StorageService({
provider: 'filesystem',
root: path.join(__dirname, 'storage')
});
diff --git a/example/upload.js b/example/upload.js
index 1ce0a30..affb304 100644
--- a/example/upload.js
+++ b/example/upload.js
@@ -1,4 +1,4 @@
-var Uploader = require('../lib/storage-handler.js');
+var StorageService = require('../');
var express = require('express');
var app = express();
@@ -13,7 +13,7 @@ app.configure(function () {
app.use(app.router);
});
-var handler = new Uploader({provider: 'filesystem', root: '/tmp/storage'});
+var handler = new StorageService({provider: 'filesystem', root: '/tmp/storage'});
app.get('/', function (req, res, next) {
res.setHeader('Content-Type', 'text/html');
@@ -30,7 +30,7 @@ app.get('/', function (req, res, next) {
});
app.post('/upload/:container', function (req, res, next) {
- handler.processUpload(req, res, function (err, result) {
+ handler.upload(req, res, function (err, result) {
if (!err) {
res.setHeader('Content-Type', 'application/json');
res.send(200, result);
@@ -41,7 +41,7 @@ app.post('/upload/:container', function (req, res, next) {
});
app.get('/download', function (req, res, next) {
- handler.client.getContainers(function (err, containers) {
+ handler.getContainers(function (err, containers) {
var html = "
Containers
";
containers.forEach(function (f) {
html += "- " + f.name + "
"
@@ -52,7 +52,7 @@ app.get('/download', function (req, res, next) {
});
app.get('/download/:container', function (req, res, next) {
- handler.client.getFiles(req.params.container, function (err, files) {
+ handler.getFiles(req.params.container, function (err, files) {
var html = "Files in container " + req.params.container + "
";
files.forEach(function (f) {
html += "- " + f.container + "/" + f.name + "
"
@@ -63,7 +63,7 @@ app.get('/download/:container', function (req, res, next) {
});
app.get('/download/:container/:file', function (req, res, next) {
- handler.processDownload(req, res, function (err, result) {
+ handler.download(req, res, function (err, result) {
if (err) {
res.send(500, err);
}
@@ -71,3 +71,4 @@ app.get('/download/:container/:file', function (req, res, next) {
});
app.listen(app.get('port'));
+console.log('http://127.0.0.1:' + app.get('port'));
diff --git a/lib/factory.js b/lib/factory.js
index 580ea7d..e3f8b00 100644
--- a/lib/factory.js
+++ b/lib/factory.js
@@ -1,4 +1,4 @@
-function createClient(options) {
+function createProvider(options) {
options = options || {};
var provider = options.provider || 'filesystem';
@@ -16,5 +16,5 @@ function createClient(options) {
}
-module.exports = createClient;
-module.exports.createClient = createClient;
\ No newline at end of file
+module.exports = createProvider;
+module.exports.createProvider = createProvider;
\ No newline at end of file
diff --git a/lib/index.js b/lib/index.js
new file mode 100644
index 0000000..22caa57
--- /dev/null
+++ b/lib/index.js
@@ -0,0 +1,81 @@
+var factory = require('./factory');
+var handler = require('./storage-handler');
+
+module.exports = StorageService;
+
+/**
+ * @param options The provider instance or options to create a provider
+ * @returns {StorageService}
+ * @constructor
+ */
+function StorageService(options) {
+ if (!(this instanceof StorageService)) {
+ return new StorageService(options);
+ }
+ if('function' === typeof options) {
+ this.provider = options;
+ } else {
+ this.provider = factory.createProvider(options);
+ }
+}
+
+StorageService.prototype.getContainers = function (cb) {
+ return this.provider.getContainers(cb);
+}
+
+StorageService.prototype.createContainer = function (options, cb) {
+ return this.provider.createContainer(options, cb);
+}
+
+StorageService.prototype.destroyContainer = function (container, cb) {
+ return this.provider.destroyContainer(container, cb);
+}
+
+StorageService.prototype.getContainer = function (container, cb) {
+ return this.provider.getContainer(container, cb);
+}
+
+// File related functions
+StorageService.prototype.uploadStream = function (container, file, options, cb) {
+ if(!cb && typeof options === 'function') {
+ cb = options;
+ options = {};
+ }
+ options = options || {};
+ options.container = container;
+ options.remote = file;
+
+ return this.provider.upload(options, cb);
+}
+
+StorageService.prototype.downloadStream = function (container, file, options, cb) {
+ if(!cb && typeof options === 'function') {
+ cb = options;
+ options = {};
+ }
+ options = options || {};
+ options.container = container;
+ options.remote = file;
+
+ return this.provider.download(options, cb);
+}
+
+StorageService.prototype.getFiles = function (container, download, cb) {
+ return this.provider.getFiles(container, download, cb);
+}
+
+StorageService.prototype.getFile = function (container, file, cb) {
+ return this.provider.getFile(container, file, cb);
+}
+
+StorageService.prototype.removeFile = function (container, file, cb) {
+ return this.provider.removeFile(container, file, cb);
+}
+
+StorageService.prototype.upload = function (req, res, cb) {
+ return handler.upload(this.provider, req, res, cb);
+}
+
+StorageService.prototype.download = function (req, res, cb) {
+ return handler.download(this.provider, req, res, cb);
+}
\ No newline at end of file
diff --git a/lib/storage-handler.js b/lib/storage-handler.js
index d52d268..e6f350b 100644
--- a/lib/storage-handler.js
+++ b/lib/storage-handler.js
@@ -1,29 +1,14 @@
-var factory = require('./factory');
-
var IncomingForm = require('formidable');
var StringDecoder = require('string_decoder').StringDecoder;
-module.exports = Uploader;
-
/**
- * Constructor
- * @param options The client instance or options to create a client
- * @returns {Uploader}
- * @constructor
+ * Handle multipart/form-data upload to the storage service
+ * @param provider The storage service provider
+ * @param req The HTTP request
+ * @param res The HTTP response
+ * @param cb The callback
*/
-function Uploader(options) {
- if (!(this instanceof Uploader)) {
- return new Uploader(options);
- }
- if('function' === typeof options) {
- this.client = options;
- } else {
- this.client = factory.createClient(options);
- }
-}
-
-Uploader.prototype.processUpload = function (req, res, cb) {
- var client = this.client;
+exports.upload = function (provider, req, res, cb) {
var form = new IncomingForm(this.options);
var container = req.params.container;
var fields = {}, files = {};
@@ -66,7 +51,7 @@ Uploader.prototype.processUpload = function (req, res, cb) {
self.emit('fileBegin', part.name, file);
- var writer = client.upload({container: container, remote: part.filename});
+ var writer = provider.upload({container: container, remote: part.filename});
part.on('data', function (buffer) {
self.pause();
@@ -96,8 +81,15 @@ Uploader.prototype.processUpload = function (req, res, cb) {
});
}
-Uploader.prototype.processDownload = function(req, res, cb) {
- var reader = this.client.download({
+/**
+ * Handle download from a container/file
+ * @param provider The storage service provider
+ * @param req The HTTP request
+ * @param res The HTTP response
+ * @param cb The callback
+ */
+exports.download = function(provider, req, res, cb) {
+ var reader = provider.download({
container: req.params.container,
remote: req.params.file
});
diff --git a/package.json b/package.json
index 7c2ebc4..cff7d96 100644
--- a/package.json
+++ b/package.json
@@ -2,7 +2,7 @@
"name": "asteroid-storage-service",
"description": "Asteroid Storage Service",
"version": "0.0.1",
- "main": "lib/factory.js",
+ "main": "lib/index.js",
"scripts": {
"test": "./node_modules/.bin/mocha --timeout 30000 test/*test.js"
},