Skip to content

Commit b7f7a3a

Browse files
committed
Fix json, skip dotfiles, no spaces in filenames
1 parent 84ca6fc commit b7f7a3a

File tree

4 files changed

+31
-28
lines changed

4 files changed

+31
-28
lines changed

lib/filehandler.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@ module.exports = function (middleware, options) {
77
});
88
var UploadHandler = require('./uploadhandler')(options);
99
var handler = new UploadHandler(req, res, function (result, redirect) {
10+
files = {files: result};
1011
if (redirect) {
11-
files = {files: result};
1212
res.redirect(redirect.replace(/%s/, encodeURIComponent(JSON.stringify(files))));
1313
} else {
1414
res.set({
1515
'Content-Type': (req.headers.accept || '').indexOf('application/json') !== -1
1616
? 'application/json'
1717
: 'text/plain'
1818
});
19-
files = {files: result};
20-
res.json(200, { files: files });
19+
res.json(200, files);
2120
}
2221
});
2322

lib/fileinfo.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ module.exports = function (options) {
2727
this.name = require('path').basename(this.name).replace(/^\.+/, '');
2828
// Prevent overwriting existing files:
2929
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 || '');
30+
this.name = this.name.replace(/(?:(?:\-([\d]+))?(\.[^.]+))?$/, function (s, index, ext) {
31+
return '-' + ((parseInt(index, 10) || 0) + 1) + (ext || '');
3232
});
3333
}
3434
};

lib/filemanager.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,21 @@ module.exports = function (middleware, options) {
3737

3838
fs.readdir(options.uploadDir(), _.bind(function (err, list) {
3939
_.each(list, function (name) {
40-
var stats = fs.statSync(options.uploadDir() + '/' + name);
41-
if (stats.isFile()) {
42-
files[name] = {
43-
path: options.uploadDir() + '/' + name
44-
};
45-
_.each(options.imageVersions, function (value, version) {
46-
counter++;
47-
fs.exists(options.uploadDir() + '/' + version + '/' + name, function (exists) {
48-
if (exists)
49-
files[name][version] = options.uploadDir() + '/' + version + '/' + name;
50-
finish();
40+
if (name.indexOf('.') != 0) {
41+
var stats = fs.statSync(options.uploadDir() + '/' + name);
42+
if (stats.isFile()) {
43+
files[name] = {
44+
path: options.uploadDir() + '/' + name
45+
};
46+
_.each(options.imageVersions, function (value, version) {
47+
counter++;
48+
fs.exists(options.uploadDir() + '/' + version + '/' + name, function (exists) {
49+
if (exists)
50+
files[name][version] = options.uploadDir() + '/' + version + '/' + name;
51+
finish();
52+
});
5153
});
52-
});
54+
}
5355
}
5456
}, this);
5557
finish();

lib/uploadhandler.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,25 @@ module.exports = function (options) {
3535
var files = [];
3636
fs.readdir(options.uploadDir(), _.bind(function (err, list) {
3737
_.each(list, function (name) {
38-
var stats = fs.statSync(options.uploadDir() + '/' + name),
39-
fileInfo;
40-
if (stats.isFile()) {
41-
fileInfo = new FileInfo({
42-
name: name,
43-
size: stats.size
44-
});
45-
this.initUrls(fileInfo);
46-
files.push(fileInfo);
38+
if (name.indexOf('.') != 0) {
39+
var stats = fs.statSync(options.uploadDir() + '/' + name),
40+
fileInfo;
41+
if (stats.isFile()) {
42+
fileInfo = new FileInfo({
43+
name: name,
44+
size: stats.size
45+
});
46+
this.initUrls(fileInfo);
47+
files.push(fileInfo);
48+
}
4749
}
4850
}, this);
4951
this.callback(files);
5052
}, this));
5153
};
5254

5355
UploadHandler.prototype.post = function () {
54-
56+
5557
var self = this,
5658
form = new formidable.IncomingForm(),
5759
tmpFiles = [],
@@ -159,7 +161,7 @@ module.exports = function (options) {
159161
UploadHandler.prototype.destroy = function () {
160162
var self = this,
161163
fileName = path.basename(decodeURIComponent(this.req.url));
162-
164+
163165
fs.unlink(options.uploadDir() + '/' + fileName, function (ex) {
164166
_.each(options.imageVersions, function (value, version) {
165167
fs.unlink(options.uploadDir() + '/' + version + '/' + fileName);

0 commit comments

Comments
 (0)