2013-06-26 21:43:53 +00:00
|
|
|
var IncomingForm = require('formidable');
|
|
|
|
var StringDecoder = require('string_decoder').StringDecoder;
|
|
|
|
|
2013-07-01 17:54:53 +00:00
|
|
|
/**
|
2013-07-02 15:57:53 +00:00
|
|
|
* Handle multipart/form-data upload to the storage service
|
|
|
|
* @param provider The storage service provider
|
2014-01-10 19:34:37 +00:00
|
|
|
* @param {Request} req The HTTP request
|
|
|
|
* @param {Response} res The HTTP response
|
|
|
|
* @param {String} container The container name
|
|
|
|
* @param {Function} cb The callback
|
2013-07-01 17:54:53 +00:00
|
|
|
*/
|
2014-01-10 19:34:37 +00:00
|
|
|
exports.upload = function (provider, req, res, container, cb) {
|
2013-06-26 21:43:53 +00:00
|
|
|
var form = new IncomingForm(this.options);
|
2014-01-10 19:34:37 +00:00
|
|
|
container = container || req.params.container;
|
2013-07-02 00:04:05 +00:00
|
|
|
var fields = {}, files = {};
|
2013-06-26 21:43:53 +00:00
|
|
|
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 () {
|
2013-07-02 00:04:05 +00:00
|
|
|
var values = fields[part.name];
|
|
|
|
if(values === undefined) {
|
|
|
|
values = [value];
|
|
|
|
fields[part.name] = values;
|
|
|
|
} else {
|
|
|
|
values.push(value);
|
|
|
|
}
|
2013-07-02 14:54:58 +00:00
|
|
|
self.emit('field', part.name, value);
|
2013-06-26 21:43:53 +00:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._flushing++;
|
|
|
|
|
2013-06-27 15:31:03 +00:00
|
|
|
var file = {
|
|
|
|
container: container,
|
|
|
|
name: part.filename,
|
|
|
|
type: part.mime
|
|
|
|
};
|
|
|
|
|
|
|
|
self.emit('fileBegin', part.name, file);
|
|
|
|
|
2013-07-10 20:08:18 +00:00
|
|
|
var headers = {};
|
|
|
|
if('content-type' in part.headers) {
|
|
|
|
headers['content-type'] = part.headers['content-type'];
|
|
|
|
}
|
2013-07-02 15:57:53 +00:00
|
|
|
var writer = provider.upload({container: container, remote: part.filename});
|
2013-06-26 21:43:53 +00:00
|
|
|
|
2013-07-10 20:08:18 +00:00
|
|
|
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();
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2013-06-26 21:43:53 +00:00
|
|
|
part.on('data', function (buffer) {
|
|
|
|
self.pause();
|
|
|
|
writer.write(buffer, function () {
|
2013-07-10 20:08:18 +00:00
|
|
|
// pkgcloud stream doesn't make callbacks
|
2013-06-26 21:43:53 +00:00
|
|
|
});
|
2013-07-10 20:08:18 +00:00
|
|
|
self.resume();
|
2013-06-26 21:43:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
part.on('end', function () {
|
2013-07-10 20:08:18 +00:00
|
|
|
|
|
|
|
writer.end(); // pkgcloud stream doesn't make callbacks
|
|
|
|
endFunc();
|
|
|
|
});
|
|
|
|
*/
|
|
|
|
|
|
|
|
part.pipe(writer, { end: false });
|
|
|
|
part.on("end", function() {
|
|
|
|
writer.end();
|
|
|
|
endFunc();
|
2013-06-26 21:43:53 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-07-02 00:04:05 +00:00
|
|
|
form.parse(req, function (err, _fields, _files) {
|
2013-07-10 20:08:18 +00:00
|
|
|
if(err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
2013-06-26 21:43:53 +00:00
|
|
|
cb && cb(err, {files: files, fields: fields});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-07-02 15:57:53 +00:00
|
|
|
/**
|
|
|
|
* Handle download from a container/file
|
|
|
|
* @param provider The storage service provider
|
2014-01-10 19:34:37 +00:00
|
|
|
* @param {Request} req The HTTP request
|
|
|
|
* @param {Response} res The HTTP response
|
|
|
|
* @param {String} container The container name
|
|
|
|
* @param {String} file The file name
|
|
|
|
* @param {Function} cb The callback
|
2013-07-02 15:57:53 +00:00
|
|
|
*/
|
2014-01-10 19:34:37 +00:00
|
|
|
exports.download = function(provider, req, res, container, file, cb) {
|
2013-07-02 15:57:53 +00:00
|
|
|
var reader = provider.download({
|
2014-01-14 18:26:09 +00:00
|
|
|
container: container || req && req.params.container,
|
|
|
|
remote: file || req && req.params.file
|
2013-06-26 21:43:53 +00:00
|
|
|
});
|
|
|
|
reader.pipe(res);
|
|
|
|
reader.on('error', function(err) {
|
2014-01-14 18:26:09 +00:00
|
|
|
res.send(500, { error: err });
|
2013-06-26 21:43:53 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|