Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concurrent file uploads resulting in the same file version #59

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ Other options and their default values:
targetDir: uploadDir,
targetUrl: uploadUrl,
ssl: false,
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
hostname: null, // in case your reverse proxy doesn't set Host header
// eg 'google.com'
maxPostSize: 11000000000, // 11 GB
Expand Down
19 changes: 15 additions & 4 deletions lib/fileinfo.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var fs = require('fs'),
mkdirp = require('mkdirp'),
_ = require('lodash');

module.exports = function (options) {
Expand Down Expand Up @@ -26,11 +27,21 @@ module.exports = function (options) {
// Prevent directory traversal and creating hidden system files:
this.name = require('path').basename(this.name).replace(/^\.+/, '');
// Prevent overwriting existing files:
while (fs.existsSync(options.baseDir() + '/' + this.name)) {
this.name = this.name.replace(/(?:(?: \(([\d]+)\))?(\.[^.]+))?$/, function (s, index, ext) {
return ' (' + ((parseInt(index, 10) || 0) + 1) + ')' + (ext || '');
});
if (options.prefixUuid) {
this.name = require('uuid').v1() + '_' + this.name;
} else {
while (fs.existsSync(options.baseDir() + '/' + this.name)) {
this.name = this.name.replace(/(?:(?: \(([\d]+)\))?(\.[^.]+))?$/, function (s, index, ext) {
return ' (' + ((parseInt(index, 10) || 0) + 1) + ')' + (ext || '');
});
}
}
//Touch the file to be sure. Synchronous to have this done before the emit of 'begin' happens
var touchFile = options.baseDir() + '/' + this.name;
mkdirp(options.baseDir(), function (err) {
if (err) {}
else {fs.closeSync(fs.openSync(touchFile, 'w'));}
});
};

FileInfo.prototype.setUrl = function (type, baseUrl) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"imagemagick": ">=0.1.2",
"lodash": ">= 0.9.2",
"mkdirp": ">= 0.3.4",
"uuid": ">=2.0.1",
"async": "*"
},
"engines": {
Expand Down