From 493b40b4c4dc900fd60c5f9c06bcdc7b4c9fd273 Mon Sep 17 00:00:00 2001 From: Vladimir Yazykov Date: Sun, 29 Mar 2015 12:09:34 +0300 Subject: [PATCH 1/2] multiple file upload --- lib/fileinfo.js | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/fileinfo.js b/lib/fileinfo.js index 28989fb..59d952a 100644 --- a/lib/fileinfo.js +++ b/lib/fileinfo.js @@ -12,13 +12,14 @@ module.exports = FileInfo; module.exports.checkFolder = checkExists; -function FileInfo(file, opts) { +function FileInfo(file, opts, req) { this.name = file.name; this.size = file.size; this.type = file.type; this.modified = file.lastMod; this.deleteType = 'DELETE'; this.options = opts; + this.req = req; } FileInfo.prototype.safeName = function() { @@ -28,12 +29,26 @@ FileInfo.prototype.safeName = function() { return ' (' + ((parseInt(index, 10) || 0) + 1) + ')' + (ext || ''); } - // Prevent directory traversal and creating hidden system files: - this.name = path.basename(this.name).replace(/^\.+/, ''); - // Prevent overwriting existing files: - while (_existsSync(this.options.uploadDir + '/' + this.name)) { - this.name = this.name.replace(nameCountRegexp, nameCountFunc); + var lastFileName = this.name; + var firstIndex = false; + + if (this.req.headers['content-range']) { + lastFileName = decodeURIComponent(this.req.headers['content-disposition'].replace(/(^[^"]+")|("$)/,'').replace('"','')); + firstIndex = this.req.headers['content-range'].replace('bytes ','').split(/[^\d]+/)[0] == 0; + }; + + var fileName = lastFileName; + + while (_existsSync(this.options.uploadDir + '/' + lastFileName)) { + fileName = lastFileName; + lastFileName = fileName.replace(nameCountRegexp, nameCountFunc); } + + if (!this.req.headers['content-range']||firstIndex) { + fileName = lastFileName; + }; + + this.name = fileName; }; FileInfo.prototype.initUrls = function(req, sss) { From ed72fb3355f889a80976f1245488f693407d164c Mon Sep 17 00:00:00 2001 From: Vladimir Yazykov Date: Sun, 29 Mar 2015 13:15:05 +0300 Subject: [PATCH 2/2] Fixed multiple upload bug --- index.js | 4 +++- lib/fileinfo.js | 14 +++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 49276ca..3063891 100644 --- a/index.js +++ b/index.js @@ -200,7 +200,9 @@ function uploadService(opts) { } // part ways here if (options.storage.type == 'local') { - fs.renameSync(file.path, options.uploadDir + '/' + fileInfo.name); + var buffer = fs.readFileSync(file.path); + fs.appendFileSync(options.uploadDir + '/' + fileInfo.name,buffer); + if (options.copyImgAsThumb && options.imageTypes.test(fileInfo.name)) { Object.keys(options.imageVersions).forEach(function(version) { counter += 1; diff --git a/lib/fileinfo.js b/lib/fileinfo.js index 59d952a..a941f6b 100644 --- a/lib/fileinfo.js +++ b/lib/fileinfo.js @@ -19,7 +19,10 @@ function FileInfo(file, opts, req) { this.modified = file.lastMod; this.deleteType = 'DELETE'; this.options = opts; - this.req = req; + + this.getRequest = function() { + return req; + } } FileInfo.prototype.safeName = function() { @@ -31,10 +34,11 @@ FileInfo.prototype.safeName = function() { var lastFileName = this.name; var firstIndex = false; + var req = this.getRequest(); - if (this.req.headers['content-range']) { - lastFileName = decodeURIComponent(this.req.headers['content-disposition'].replace(/(^[^"]+")|("$)/,'').replace('"','')); - firstIndex = this.req.headers['content-range'].replace('bytes ','').split(/[^\d]+/)[0] == 0; + if (req.headers['content-range']) { + lastFileName = decodeURIComponent(req.headers['content-disposition'].replace(/(^[^"]+")|("$)/,'').replace('"','')); + firstIndex = req.headers['content-range'].replace('bytes ','').split(/[^\d]+/)[0] == 0; }; var fileName = lastFileName; @@ -44,7 +48,7 @@ FileInfo.prototype.safeName = function() { lastFileName = fileName.replace(nameCountRegexp, nameCountFunc); } - if (!this.req.headers['content-range']||firstIndex) { + if (!req.headers['content-range']||firstIndex) { fileName = lastFileName; };