2014-01-14 18:39:02 +00:00
|
|
|
var StorageService = require('../').StorageService;
|
2015-01-16 18:40:18 +00:00
|
|
|
var providers = null;
|
|
|
|
try {
|
|
|
|
providers = require('./providers-private.json');
|
|
|
|
} catch(err) {
|
|
|
|
providers = require('./providers.json');
|
|
|
|
}
|
2013-07-10 20:08:18 +00:00
|
|
|
var express = require('express');
|
|
|
|
var app = express();
|
|
|
|
|
2015-01-16 18:40:18 +00:00
|
|
|
app.set('port', process.env.PORT || 3000);
|
|
|
|
app.set('views', __dirname + '/views');
|
|
|
|
app.set('view engine', 'ejs');
|
2013-07-10 20:08:18 +00:00
|
|
|
|
|
|
|
var handler = new StorageService(
|
2014-01-24 17:44:58 +00:00
|
|
|
{
|
2013-07-10 20:08:18 +00:00
|
|
|
provider: 'amazon',
|
2014-01-24 18:19:03 +00:00
|
|
|
key: providers.amazon.key,
|
|
|
|
keyId: providers.amazon.keyId
|
2014-01-24 17:44:58 +00:00
|
|
|
});
|
2013-07-10 20:08:18 +00:00
|
|
|
|
2015-01-16 18:40:18 +00:00
|
|
|
app.get('/', function(req, res, next) {
|
2014-01-24 17:44:58 +00:00
|
|
|
res.setHeader('Content-Type', 'text/html');
|
|
|
|
var form = "<html><body><h1>Storage Service Demo</h1>" +
|
|
|
|
"<a href='/download'>List all containers</a><p>" +
|
|
|
|
"Upload to container con1: <p>" +
|
|
|
|
"<form method='POST' enctype='multipart/form-data' action='/upload/con1'>"
|
|
|
|
+ "File to upload: <input type=file name=uploadedFiles multiple=true><br>"
|
|
|
|
+ "Notes about the file: <input type=text name=note><br>"
|
|
|
|
+ "<input type=submit value=Upload></form>" +
|
|
|
|
"</body></html>";
|
|
|
|
res.send(form);
|
|
|
|
res.end();
|
2013-07-10 20:08:18 +00:00
|
|
|
});
|
|
|
|
|
2015-01-16 18:40:18 +00:00
|
|
|
app.post('/upload/:container', function(req, res, next) {
|
|
|
|
handler.upload(req, res, function(err, result) {
|
2014-01-24 17:44:58 +00:00
|
|
|
if (!err) {
|
|
|
|
res.setHeader('Content-Type', 'application/json');
|
2015-01-16 18:40:18 +00:00
|
|
|
res.status(200).send(result);
|
2014-01-24 17:44:58 +00:00
|
|
|
} else {
|
2015-01-16 18:40:18 +00:00
|
|
|
res.status(500).send(err);
|
2014-01-24 17:44:58 +00:00
|
|
|
}
|
|
|
|
});
|
2013-07-10 20:08:18 +00:00
|
|
|
});
|
|
|
|
|
2015-01-16 18:40:18 +00:00
|
|
|
app.get('/download', function(req, res, next) {
|
|
|
|
handler.getContainers(function(err, containers) {
|
2014-01-24 17:44:58 +00:00
|
|
|
var html = "<html><body><h1>Containers</h1><ul>";
|
2015-01-16 18:40:18 +00:00
|
|
|
containers.forEach(function(f) {
|
2014-01-24 17:44:58 +00:00
|
|
|
html += "<li><a href='/download/" + f.name + "'>" + f.name + "</a></li>"
|
2013-07-10 20:08:18 +00:00
|
|
|
});
|
2014-01-24 17:44:58 +00:00
|
|
|
html += "</ul><p><a href='/'>Home</a></p></body></html>";
|
2015-01-16 18:40:18 +00:00
|
|
|
res.status(200).send(html);
|
2014-01-24 17:44:58 +00:00
|
|
|
});
|
2013-07-10 20:08:18 +00:00
|
|
|
});
|
|
|
|
|
2015-01-16 18:40:18 +00:00
|
|
|
app.get('/download/:container', function(req, res, next) {
|
|
|
|
handler.getFiles(req.params.container, function(err, files) {
|
2014-01-24 17:44:58 +00:00
|
|
|
var html = "<html><body><h1>Files in container " + req.params.container + "</h1><ul>";
|
2015-01-16 18:40:18 +00:00
|
|
|
files.forEach(function(f) {
|
2014-01-24 17:44:58 +00:00
|
|
|
html += "<li><a href='/download/" + f.container + "/" + f.name + "'>" + f.container + "/" + f.name + "</a></li>"
|
2013-07-10 20:08:18 +00:00
|
|
|
});
|
2014-01-24 17:44:58 +00:00
|
|
|
html += "</ul><p><a href='/'>Home</a></p></body></html>";
|
2015-01-16 18:40:18 +00:00
|
|
|
res.status(200).send(html);
|
2014-01-24 17:44:58 +00:00
|
|
|
});
|
2013-07-10 20:08:18 +00:00
|
|
|
});
|
|
|
|
|
2015-01-16 18:40:18 +00:00
|
|
|
app.get('/download/:container/:file', function(req, res, next) {
|
|
|
|
handler.download(req.params.container, req.params.file, res, function(err, result) {
|
2014-01-24 17:44:58 +00:00
|
|
|
if (err) {
|
2015-01-16 18:40:18 +00:00
|
|
|
res.status(500).send(err);
|
2014-01-24 17:44:58 +00:00
|
|
|
}
|
|
|
|
});
|
2013-07-10 20:08:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
app.listen(app.get('port'));
|
|
|
|
console.log('http://127.0.0.1:' + app.get('port'));
|