Skip to content

Commit ffb0c0a

Browse files
author
Sebastian.Just
committed
Avoid concurrency issues while the same file-version is created for multiple requests resulting in an overwrite of the files by the last request.
1 parent bf894bf commit ffb0c0a

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ Other options and their default values:
253253
targetDir: uploadDir,
254254
targetUrl: uploadUrl,
255255
ssl: false,
256+
prefixUuid: false, // Prefix every created file with a UUID. Important if there are multiple node processes working on the same folder (iisnode for example) serving requests
256257
hostname: null, // in case your reverse proxy doesn't set Host header
257258
// eg 'google.com'
258259
maxPostSize: 11000000000, // 11 GB

lib/fileinfo.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,17 @@ module.exports = function (options) {
2626
// Prevent directory traversal and creating hidden system files:
2727
this.name = require('path').basename(this.name).replace(/^\.+/, '');
2828
// Prevent overwriting existing files:
29-
while (fs.existsSync(options.baseDir() + '/' + this.name)) {
30-
this.name = this.name.replace(/(?:(?: \(([\d]+)\))?(\.[^.]+))?$/, function (s, index, ext) {
31-
return ' (' + ((parseInt(index, 10) || 0) + 1) + ')' + (ext || '');
32-
});
29+
if (options.prefixUuid) {
30+
this.name = require('uuid').v1() + '_' + this.name;
31+
} else {
32+
while (fs.existsSync(options.baseDir() + '/' + this.name)) {
33+
this.name = this.name.replace(/(?:(?: \(([\d]+)\))?(\.[^.]+))?$/, function (s, index, ext) {
34+
return ' (' + ((parseInt(index, 10) || 0) + 1) + ')' + (ext || '');
35+
});
36+
}
3337
}
38+
//Touch the file to be sure. Synchronous to have this done before the emit of 'begin' happens
39+
fs.closeSync(fs.openSync(options.baseDir() + '/' + this.name, 'w'));
3440
};
3541

3642
FileInfo.prototype.setUrl = function (type, baseUrl) {

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"imagemagick": ">=0.1.2",
1515
"lodash": ">= 0.9.2",
1616
"mkdirp": ">= 0.3.4",
17+
"uuid": ">=2.0.1",
1718
"async": "*"
1819
},
1920
"engines": {

0 commit comments

Comments
 (0)