Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 25 additions & 6 deletions lib/fileinfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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) {
Expand Down