Add a connector for storage service

This commit is contained in:
Raymond Feng 2013-07-03 16:58:20 -07:00
parent 4edfaae2de
commit 3fc500a37c
3 changed files with 48 additions and 44 deletions

View File

@ -1,7 +1,7 @@
var asteroid = require('asteroid')
, app = module.exports = asteroid();
var StorageService = require('../');
// var StorageService = require('../');
// expose a rest api
app.use(asteroid.rest());
@ -10,6 +10,22 @@ app.configure(function () {
app.set('port', process.env.PORT || 3000);
});
var ds = asteroid.createDataSource({
connector: require('../lib/storage-connector'),
provider: 'filesystem',
root: '/tmp/storage'
});
var Container = ds.createModel('container', {name: String});
console.log(Container);
Container.getContainers(console.log);
console.log('shared', Container.getContainers.shared);
app.model(Container);
/*
var handler = new StorageService({provider: 'filesystem', root: '/tmp/storage'});
app.service('storage', handler);
@ -28,46 +44,8 @@ app.get('/', function (req, res, next) {
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);
}
});
});
*/
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);
});
});
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);
});
});
app.get('/download/:container/:file', function (req, res, next) {
handler.download(req, res, function (err, result) {
if (err) {
res.send(500, err);
}
});
});
app.listen(app.get('port'));
console.log('http://127.0.0.1:' + app.get('port'));

View File

@ -132,15 +132,15 @@ StorageService.prototype.removeFile.http = [
];
StorageService.prototype.upload.shared = true;
StorageService.prototype.upload.accepts = [{arg: 'file', type: 'object', 'http': {source: 'req'}}];
StorageService.prototype.upload.returns = {arg: 'description', type: 'object'};
StorageService.prototype.upload.accepts = [{arg: 'req', type: 'undefined', 'http': {source: 'req'}}];
StorageService.prototype.upload.returns = {arg: 'result', type: 'object'};
StorageService.prototype.upload.http = [
{verb: 'post', path: '/:container/upload/:file'}
];
StorageService.prototype.download.shared = true;
StorageService.prototype.download.accepts = [{arg: 'file', type: 'object', 'http': {source: 'req'}}];
StorageService.prototype.download.returns = {arg: 'body', type: 'object'};
StorageService.prototype.download.accepts = [{arg: 'req', type: 'undefined', 'http': {source: 'req'}}];
StorageService.prototype.download.returns = {arg: 'res', type: 'stream'};
StorageService.prototype.download.http = [
{verb: 'get', path: '/:container/download/:file'}
];

26
lib/storage-connector.js Normal file
View File

@ -0,0 +1,26 @@
var StorageService = require('./index');
/**
* Export the initialize method to JDB
* @param schema
* @param callback
*/
exports.initialize = function (schema, callback) {
var settings = schema.settings || {};
var adapter = new StorageService(settings);
schema.adapter = adapter;
schema.adapter.schema = schema;
adapter.DataAccessObject = function() {};
for (var m in StorageService.prototype) {
var method = StorageService.prototype[m];
if ('function' === typeof method) {
adapter.DataAccessObject[m] = method.bind(adapter);
for(var k in method) {
adapter.DataAccessObject[m][k] = method[k];
}
}
}
adapter.define = function(model, properties, settings) {};
}