diff --git a/example/app.js b/example/app.js index e011ea6..6ea7482 100644 --- a/example/app.js +++ b/example/app.js @@ -60,14 +60,21 @@ s3.getContainers(function (err, containers) { }); }); -var fs = StorageService({ + +var fs = require('fs'); +var path = require('path'); +var stream = s3.uploadStream('con1','test.jpg'); +var input = fs.createReadStream(path.join(__dirname, 'test.jpg')).pipe(stream); + + +var local = StorageService({ provider: 'filesystem', root: path.join(__dirname, 'storage') }); // Container -fs.getContainers(function (err, containers) { +local.getContainers(function (err, containers) { if (err) { console.error(err); return; @@ -81,3 +88,4 @@ fs.getContainers(function (err, containers) { }); }); }); + diff --git a/example/test.jpg b/example/test.jpg new file mode 100644 index 0000000..04f1cdb Binary files /dev/null and b/example/test.jpg differ diff --git a/example/upload-amazon.js b/example/upload-amazon.js new file mode 100644 index 0000000..6f9d354 --- /dev/null +++ b/example/upload-amazon.js @@ -0,0 +1,79 @@ +var StorageService = require('../'); + +var express = require('express'); +var app = express(); + +app.configure(function () { + app.set('port', process.env.PORT || 3001); + 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 StorageService( +{ + provider: 'amazon', + key: 'your-amazon-key', + keyId: 'your-amazon-key-id' +}); + +app.get('/', function (req, res, next) { + res.setHeader('Content-Type', 'text/html'); + var form = "

Storage Service Demo

" + + "List all containers

" + + "Upload to container con1:

" + + "

" + + "File to upload:
" + + "Notes about the file:
" + + "
" + + ""; + res.send(form); + 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 = "

Containers

Home

"; + res.send(200, html); + }); +}); + +app.get('/download/:container', function (req, res, next) { + handler.getFiles(req.params.container, function (err, files) { + var html = "

Files in container " + req.params.container + "

Home

"; + 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')); diff --git a/lib/storage-handler.js b/lib/storage-handler.js index e6f350b..2e91eac 100644 --- a/lib/storage-handler.js +++ b/lib/storage-handler.js @@ -51,32 +51,52 @@ exports.upload = function (provider, req, res, cb) { self.emit('fileBegin', part.name, file); + var headers = {}; + if('content-type' in part.headers) { + headers['content-type'] = part.headers['content-type']; + } var writer = provider.upload({container: container, remote: part.filename}); + var endFunc = function () { + self._flushing--; + var values = files[part.name]; + if(values === undefined) { + values = [file]; + files[part.name] = values; + } else { + values.push(file); + } + self.emit('file', part.name, file); + self._maybeEnd(); + }; + + /* part.on('data', function (buffer) { self.pause(); writer.write(buffer, function () { - self.resume(); + // pkgcloud stream doesn't make callbacks }); + self.resume(); }); part.on('end', function () { - writer.end(function () { - self._flushing--; - var values = files[part.name]; - if(values === undefined) { - values = [file]; - files[part.name] = values; - } else { - values.push(file); - } - self.emit('file', part.name, file); - self._maybeEnd(); - }); + + writer.end(); // pkgcloud stream doesn't make callbacks + endFunc(); + }); + */ + + part.pipe(writer, { end: false }); + part.on("end", function() { + writer.end(); + endFunc(); }); }; form.parse(req, function (err, _fields, _files) { + if(err) { + console.error(err); + } cb && cb(err, {files: files, fields: fields}); }); }