maxChunkSize splitting file into multiple files
Opened this issue · 1 comments
samgranger commented
If I use the maxChunkSize option, the uploaded file get split into multiple files - how can I get them to join automatically?
I could just turn off maxChunkSize but I'm uploading big files (a few GB).
iraniamir commented
Hi there,
i wrote this ability for my server , you can use something like that 👍
first add this class to uploadHandler.js
module.exports = class CRegExp extends RegExp { [Symbol.split](str, limit) { var result = RegExp.prototype[Symbol.split].call(this, str, limit); return result.map(x => x); } }
and next you should add blow var in UploadHandler.prototype.post = function() after finish var
contentRange = _.bind(function() { var CRange = this.req.headers['content-range']; if (typeof CRange !== 'undefined' && CRange !== null && CRange !== '') { var exp = new CRegExp(/[^0-9]+/), contentSplit = CRange.split(exp); return [contentSplit[2], contentSplit[3]]; } else { return [false, false]; } }, this);
and some changes in saving file process, this is my code
UploadHandler.prototype.post = function() { var self = this, form = new formidable.IncomingForm(), tmpFiles = [], files = [], map = {}, counter = 1, redirect, finish = _.bind(function(callb) { if (!--counter) { async.each(files, _.bind(function(fileInfo, cb) { if (!callb) { this.checkValidate(fileInfo, _.bind(function(err) { this.emit('end', fileInfo); cb(err); }, this)); } else { cb(null); } }, this), _.bind(function(err) { this.callback({ files: files }, redirect); }, this)); } }, this), contentRange = _.bind(function() { var CRange = this.req.headers['content-range']; if (typeof CRange !== 'undefined' && CRange !== null && CRange !== '') { var exp = new CRegExp(/[^0-9]+/), contentSplit = CRange.split(exp); return [contentSplit[2], contentSplit[3]]; } else { return [false, false]; } }, this); this.noCache(); form.uploadDir = options.tmpDir; form .on('fileBegin', function(name, file) { tmpFiles.push(file.path); var fileInfo = new FileInfo(file); fileInfo.safeName(); map[path.basename(file.path)] = fileInfo; files.push(fileInfo); self.emit('begin', fileInfo); }) .on('field', function(name, value) { if (name === 'redirect') { redirect = value; } if (!self.req.fields) self.req.fields = {}; self.req.fields[name] = value; }) .on('file', function(name, file) { counter++; var fileInfo = map[path.basename(file.path)]; fs.exists(file.path, function(exists) { if (exists) { fileInfo.path = path.join(options.uploadDir(), fileInfo.name); fileInfo.tmp = md5(file.path); fs.stat(fileInfo.path, function(err, stats) { if (!err && stats.isFile()) { if (contentRange()[1] && stats.size < contentRange()[1]) { var w = fs.createWriteStream(fileInfo.path, { flags: 'a' }); var r = fs.createReadStream(file.path); w.on('close', function() { finish(((parseInt(contentRange()[0]) + 1) !== parseInt(contentRange()[1])) ? true : ''); }); r.pipe(w); } } else { mkdirp(options.uploadDir(), function(err, made) { fs.rename(file.path, fileInfo.path, function(err) { if (!err) { finish((contentRange()[1]) ? true : ''); } else { var is = fs.createReadStream(file.path); var os = fs.createWriteStream(fileInfo.path); is.on('end', function(err) { if (!err) { fs.unlink(file.path); } finish((contentRange()[1]) ? true : ''); }); is.pipe(os); } }); }); } }); } else finish(); }); }) .on('aborted', function() { _.each(tmpFiles, function(file) { var fileInfo = map[path.basename(file)]; self.emit('abort', fileInfo); fs.unlink(file); }); }) .on('error', function(e) { self.emit('error', e); }) .on('progress', function(bytesReceived, bytesExpected) { if (bytesReceived > options.maxPostSize) self.req.connection.destroy(); }) .on('end', finish) .parse(self.req); };
probably you would change something to use it because i change lots of thing in my node module . Good luke