Fix for SLA-213. The stream from pkgcloud doesn't make callbacks
This commit is contained in:
parent
3fc500a37c
commit
d409a4bd87
|
@ -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',
|
provider: 'filesystem',
|
||||||
root: path.join(__dirname, 'storage')
|
root: path.join(__dirname, 'storage')
|
||||||
});
|
});
|
||||||
|
|
||||||
// Container
|
// Container
|
||||||
|
|
||||||
fs.getContainers(function (err, containers) {
|
local.getContainers(function (err, containers) {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
return;
|
return;
|
||||||
|
@ -81,3 +88,4 @@ fs.getContainers(function (err, containers) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Binary file not shown.
After Width: | Height: | Size: 1.2 MiB |
|
@ -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 = "<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();
|
||||||
|
});
|
||||||
|
|
||||||
|
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'));
|
|
@ -51,32 +51,52 @@ exports.upload = function (provider, req, res, cb) {
|
||||||
|
|
||||||
self.emit('fileBegin', part.name, file);
|
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 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) {
|
part.on('data', function (buffer) {
|
||||||
self.pause();
|
self.pause();
|
||||||
writer.write(buffer, function () {
|
writer.write(buffer, function () {
|
||||||
self.resume();
|
// pkgcloud stream doesn't make callbacks
|
||||||
});
|
});
|
||||||
|
self.resume();
|
||||||
});
|
});
|
||||||
|
|
||||||
part.on('end', function () {
|
part.on('end', function () {
|
||||||
writer.end(function () {
|
|
||||||
self._flushing--;
|
writer.end(); // pkgcloud stream doesn't make callbacks
|
||||||
var values = files[part.name];
|
endFunc();
|
||||||
if(values === undefined) {
|
});
|
||||||
values = [file];
|
*/
|
||||||
files[part.name] = values;
|
|
||||||
} else {
|
part.pipe(writer, { end: false });
|
||||||
values.push(file);
|
part.on("end", function() {
|
||||||
}
|
writer.end();
|
||||||
self.emit('file', part.name, file);
|
endFunc();
|
||||||
self._maybeEnd();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
form.parse(req, function (err, _fields, _files) {
|
form.parse(req, function (err, _fields, _files) {
|
||||||
|
if(err) {
|
||||||
|
console.error(err);
|
||||||
|
}
|
||||||
cb && cb(err, {files: files, fields: fields});
|
cb && cb(err, {files: files, fields: fields});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue