2013-07-01 17:54:53 +00:00
|
|
|
var factory = require('./factory');
|
2013-06-26 21:43:53 +00:00
|
|
|
|
|
|
|
var IncomingForm = require('formidable');
|
|
|
|
var StringDecoder = require('string_decoder').StringDecoder;
|
|
|
|
|
|
|
|
module.exports = Uploader;
|
|
|
|
|
2013-07-01 17:54:53 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
* @param options The client instance or options to create a client
|
|
|
|
* @returns {Uploader}
|
|
|
|
* @constructor
|
|
|
|
*/
|
2013-06-26 21:43:53 +00:00
|
|
|
function Uploader(options) {
|
|
|
|
if (!(this instanceof Uploader)) {
|
|
|
|
return new Uploader(options);
|
|
|
|
}
|
2013-07-01 17:54:53 +00:00
|
|
|
if('function' === typeof options) {
|
|
|
|
this.client = options;
|
|
|
|
} else {
|
|
|
|
this.client = factory.createClient(options);
|
|
|
|
}
|
2013-06-26 21:43:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Uploader.prototype.processUpload = function (req, res, cb) {
|
|
|
|
var client = this.client;
|
|
|
|
var form = new IncomingForm(this.options);
|
|
|
|
var 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);
|
|
|
|
}
|
|
|
|
self.emit('field', part.name + '[' + (values.length -1) + ']', 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-06-26 21:43:53 +00:00
|
|
|
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--;
|
2013-07-02 00:04:05 +00:00
|
|
|
var values = files[part.name];
|
|
|
|
if(values === undefined) {
|
|
|
|
values = [file];
|
|
|
|
files[part.name] = values;
|
|
|
|
} else {
|
|
|
|
values.push(file);
|
|
|
|
}
|
|
|
|
self.emit('file', part.name + '[' + (values.length -1) + ']', file);
|
2013-06-26 21:43:53 +00:00
|
|
|
self._maybeEnd();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-07-02 00:04:05 +00:00
|
|
|
form.parse(req, function (err, _fields, _files) {
|
2013-06-26 21:43:53 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|