Refactor the code to use StorageService as the facade
This commit is contained in:
parent
564d8c3432
commit
ad41d47b71
|
@ -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')
|
||||
});
|
||||
|
|
|
@ -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 = "<html><body><h1>Containers</h1><ul>";
|
||||
containers.forEach(function (f) {
|
||||
html += "<li><a href='/download/" + f.name + "'>" + f.name + "</a></li>"
|
||||
|
@ -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 = "<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>"
|
||||
|
@ -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'));
|
||||
|
|
|
@ -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;
|
||||
module.exports = createProvider;
|
||||
module.exports.createProvider = createProvider;
|
|
@ -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);
|
||||
}
|
|
@ -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
|
||||
});
|
||||
|
|
|
@ -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"
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue