Add an express demo for metadata apis, upload/download
This commit is contained in:
parent
9b0fe46fbd
commit
d6b89fe2ef
|
@ -0,0 +1,67 @@
|
|||
var Uploader = require('../lib/storage-handler.js');
|
||||
|
||||
var express = require('express');
|
||||
var app = express();
|
||||
|
||||
app.configure(function () {
|
||||
app.set('port', process.env.PORT || 3000);
|
||||
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);
|
||||
});
|
||||
|
||||
var handler = new Uploader({provider: 'filesystem', root: '/tmp/storage'});
|
||||
|
||||
app.get('/', function (req, res, next) {
|
||||
res.setHeader('Content-Type', 'text/html');
|
||||
var form = "<html><body><form method='POST' enctype='multipart/form-data' action='/upload/c1'>"
|
||||
+ " File to upload: <input type=file name=upfile><br>"
|
||||
+ " Notes about the file: <input type=text name=note><br>"
|
||||
+ " <input type=submit value=Upload></form></body></html>";
|
||||
res.send(form);
|
||||
res.end();
|
||||
});
|
||||
|
||||
app.post('/upload/:container', function (req, res, next) {
|
||||
handler.processUpload(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.client.getContainers(function (err, containers) {
|
||||
var html = "<body><h1>Containers</h1><ul>";
|
||||
containers.forEach(function (f) {
|
||||
html += "<li><a href='" + f.name + "'>" + f.name + "</a></li>"
|
||||
});
|
||||
res.send(200, html);
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/download/:container', function (req, res, next) {
|
||||
handler.client.getFiles(req.params.container, function (err, files) {
|
||||
var html = "<body><h1>Files</h1><ul>";
|
||||
files.forEach(function (f) {
|
||||
html += "<li><a href='" + f.container + "/" + f.name + "'>" + f.container + "/" + f.name + "</a></li>"
|
||||
});
|
||||
res.send(200, html);
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/download/:container/:file', function (req, res, next) {
|
||||
handler.processDownload(req, res, function (err, result) {
|
||||
if (err) {
|
||||
res.send(500, err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
app.listen(app.get('port'));
|
|
@ -0,0 +1,87 @@
|
|||
var factory = require('./index');
|
||||
|
||||
var IncomingForm = require('formidable');
|
||||
var StringDecoder = require('string_decoder').StringDecoder;
|
||||
|
||||
module.exports = Uploader;
|
||||
|
||||
function Uploader(options) {
|
||||
if (!(this instanceof Uploader)) {
|
||||
return new Uploader(options);
|
||||
}
|
||||
this.client = factory.createClient(options);
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
Uploader.prototype.processUpload = function (req, res, cb) {
|
||||
var client = this.client;
|
||||
var form = new IncomingForm(this.options);
|
||||
var container = req.params.container;
|
||||
var fields = {};
|
||||
var files = {};
|
||||
form.handlePart = function (part) {
|
||||
var self = this;
|
||||
|
||||
if (part.filename === undefined) {
|
||||
var value = ''
|
||||
, decoder = new StringDecoder(this.encoding);
|
||||
|
||||
part.on('data', function (buffer) {
|
||||
self._fieldsSize += buffer.length;
|
||||
if (self._fieldsSize > self.maxFieldsSize) {
|
||||
self._error(new Error('maxFieldsSize exceeded, received ' + self._fieldsSize + ' bytes of field data'));
|
||||
return;
|
||||
}
|
||||
value += decoder.write(buffer);
|
||||
});
|
||||
|
||||
part.on('end', function () {
|
||||
fields[part.name] = value;
|
||||
self.emit('field', part.name, value);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
this._flushing++;
|
||||
|
||||
var writer = client.upload({container: container, remote: part.filename});
|
||||
|
||||
part.on('data', function (buffer) {
|
||||
self.pause();
|
||||
writer.write(buffer, function () {
|
||||
self.resume();
|
||||
});
|
||||
});
|
||||
|
||||
part.on('end', function () {
|
||||
writer.end(function () {
|
||||
self._flushing--;
|
||||
files[part.name] = part.filename;
|
||||
self._maybeEnd();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
form.parse(req, function (err, result) {
|
||||
console.log(result);
|
||||
cb && cb(err, {files: files, fields: fields});
|
||||
});
|
||||
}
|
||||
|
||||
Uploader.prototype.processDownload = function(req, res, cb) {
|
||||
var reader = this.client.download({
|
||||
container: req.params.container,
|
||||
remote: req.params.file
|
||||
});
|
||||
reader.pipe(res);
|
||||
reader.on('error', function(err) {
|
||||
cb && cb(err);
|
||||
});
|
||||
reader.on('end', function(err, result) {
|
||||
cb && cb(err, result);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
@ -11,6 +11,8 @@
|
|||
"async": "latest"
|
||||
},
|
||||
"devDependencies": {
|
||||
"express": "latest",
|
||||
"formidable": "latest",
|
||||
"mocha": "latest",
|
||||
"supertest": "latest"
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue