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 28989fb..a941f6b 100644 --- a/lib/fileinfo.js +++ b/lib/fileinfo.js @@ -12,13 +12,17 @@ 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.getRequest = function() { + return req; + } } FileInfo.prototype.safeName = function() { @@ -28,12 +32,27 @@ 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; + var req = this.getRequest(); + + 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; + + while (_existsSync(this.options.uploadDir + '/' + lastFileName)) { + fileName = lastFileName; + lastFileName = fileName.replace(nameCountRegexp, nameCountFunc); } + + if (!req.headers['content-range']||firstIndex) { + fileName = lastFileName; + }; + + this.name = fileName; }; FileInfo.prototype.initUrls = function(req, sss) {