2013-07-02 15:57:53 +00:00
|
|
|
var StorageService = require('../');
|
2013-06-26 21:43:53 +00:00
|
|
|
|
|
|
|
var express = require('express');
|
|
|
|
var app = express();
|
|
|
|
|
|
|
|
app.configure(function () {
|
2013-07-01 17:54:53 +00:00
|
|
|
app.set('port', process.env.PORT || 3001);
|
2013-06-26 21:43:53 +00:00
|
|
|
app.set('views', __dirname + '/views');
|
|
|
|
app.set('view engine', 'ejs');
|
|
|
|
app.use(express.favicon());
|
|
|
|
// app.use(express.logger('dev'));
|
|
|
|
app.use(express.methodOverride());
|
|
|
|
app.use(app.router);
|
|
|
|
});
|
|
|
|
|
2013-07-02 15:57:53 +00:00
|
|
|
var handler = new StorageService({provider: 'filesystem', root: '/tmp/storage'});
|
2013-06-26 21:43:53 +00:00
|
|
|
|
|
|
|
app.get('/', function (req, res, next) {
|
|
|
|
res.setHeader('Content-Type', 'text/html');
|
2013-06-27 16:56:29 +00:00
|
|
|
var form = "<html><body><h1>Storage Service Demo</h1>" +
|
|
|
|
"<a href='/download'>List all containers</a><p>" +
|
|
|
|
"Upload to container c1: <p>" +
|
|
|
|
"<form method='POST' enctype='multipart/form-data' action='/upload/c1'>"
|
2013-06-27 20:50:57 +00:00
|
|
|
+ "File to upload: <input type=file name=uploadedFiles multiple=true><br>"
|
2013-06-27 16:56:29 +00:00
|
|
|
+ "Notes about the file: <input type=text name=note><br>"
|
|
|
|
+ "<input type=submit value=Upload></form>" +
|
|
|
|
"</body></html>";
|
2013-06-26 21:43:53 +00:00
|
|
|
res.send(form);
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
app.post('/upload/:container', function (req, res, next) {
|
2013-07-02 15:57:53 +00:00
|
|
|
handler.upload(req, res, function (err, result) {
|
2013-06-26 21:43:53 +00:00
|
|
|
if (!err) {
|
|
|
|
res.setHeader('Content-Type', 'application/json');
|
|
|
|
res.send(200, result);
|
|
|
|
} else {
|
|
|
|
res.send(500, err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/download', function (req, res, next) {
|
2013-07-02 15:57:53 +00:00
|
|
|
handler.getContainers(function (err, containers) {
|
2013-06-27 15:51:46 +00:00
|
|
|
var html = "<html><body><h1>Containers</h1><ul>";
|
2013-06-26 21:43:53 +00:00
|
|
|
containers.forEach(function (f) {
|
2013-06-27 15:51:46 +00:00
|
|
|
html += "<li><a href='/download/" + f.name + "'>" + f.name + "</a></li>"
|
2013-06-26 21:43:53 +00:00
|
|
|
});
|
2013-06-27 16:56:29 +00:00
|
|
|
html += "</ul><p><a href='/'>Home</a></p></body></html>";
|
2013-06-26 21:43:53 +00:00
|
|
|
res.send(200, html);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/download/:container', function (req, res, next) {
|
2013-07-02 15:57:53 +00:00
|
|
|
handler.getFiles(req.params.container, function (err, files) {
|
2013-06-27 16:56:29 +00:00
|
|
|
var html = "<html><body><h1>Files in container " + req.params.container + "</h1><ul>";
|
2013-06-26 21:43:53 +00:00
|
|
|
files.forEach(function (f) {
|
2013-06-27 15:51:46 +00:00
|
|
|
html += "<li><a href='/download/" + f.container + "/" + f.name + "'>" + f.container + "/" + f.name + "</a></li>"
|
2013-06-26 21:43:53 +00:00
|
|
|
});
|
2013-06-27 16:56:29 +00:00
|
|
|
html += "</ul><p><a href='/'>Home</a></p></body></html>";
|
2013-06-26 21:43:53 +00:00
|
|
|
res.send(200, html);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/download/:container/:file', function (req, res, next) {
|
2013-07-02 15:57:53 +00:00
|
|
|
handler.download(req, res, function (err, result) {
|
2013-06-26 21:43:53 +00:00
|
|
|
if (err) {
|
|
|
|
res.send(500, err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-06-27 20:50:57 +00:00
|
|
|
app.listen(app.get('port'));
|
2013-07-02 15:57:53 +00:00
|
|
|
console.log('http://127.0.0.1:' + app.get('port'));
|