Skip to content

Commit fcecb97

Browse files
committed
Added a function to remove extra slashes from path
1 parent 3ad98f9 commit fcecb97

File tree

2 files changed

+104
-96
lines changed

2 files changed

+104
-96
lines changed

lib/uploadhandler.js

Lines changed: 102 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ module.exports = function (options) {
6060
else cb(err);
6161
}, this));
6262
}, this),
63-
_.bind(function(err) {
63+
_.bind(function(err) {
6464
this.callback({files: files});
6565
}, this));
6666
}, this));
@@ -83,7 +83,7 @@ module.exports = function (options) {
8383
cb(err);
8484
}, this));
8585
}, this),
86-
_.bind(function(err) {
86+
_.bind(function(err) {
8787
this.callback({files: files}, redirect);
8888
}, this));
8989
}
@@ -93,92 +93,92 @@ module.exports = function (options) {
9393

9494
form.uploadDir = options.tmpDir;
9595
form
96-
.on('fileBegin', function (name, file) {
97-
tmpFiles.push(file.path);
98-
var fileInfo = new FileInfo(file);
99-
fileInfo.safeName();
100-
map[path.basename(file.path)] = fileInfo;
101-
files.push(fileInfo);
102-
self.emit('begin', fileInfo);
103-
})
104-
.on('field', function (name, value) {
105-
if (name === 'redirect') {
106-
redirect = value;
107-
}
108-
if ( !self.req.fields )
109-
self.req.fields = {};
110-
self.req.fields[name] = value;
111-
})
112-
.on('file', function (name, file) {
113-
counter++;
114-
var fileInfo = map[path.basename(file.path)];
115-
fs.exists(file.path, function(exists) {
116-
if (exists) {
117-
fileInfo.size = file.size;
118-
if (!fileInfo.validate()) {
119-
fs.unlink(file.path);
120-
finish();
121-
return;
122-
}
96+
.on('fileBegin', function (name, file) {
97+
tmpFiles.push(file.path);
98+
var fileInfo = new FileInfo(file);
99+
fileInfo.safeName();
100+
map[path.basename(file.path)] = fileInfo;
101+
files.push(fileInfo);
102+
self.emit('begin', fileInfo);
103+
})
104+
.on('field', function (name, value) {
105+
if (name === 'redirect') {
106+
redirect = value;
107+
}
108+
if ( !self.req.fields )
109+
self.req.fields = {};
110+
self.req.fields[name] = value;
111+
})
112+
.on('file', function (name, file) {
113+
counter++;
114+
var fileInfo = map[path.basename(file.path)];
115+
fs.exists(file.path, function(exists) {
116+
if (exists) {
117+
fileInfo.size = file.size;
118+
if (!fileInfo.validate()) {
119+
fs.unlink(file.path);
120+
finish();
121+
return;
122+
}
123123

124-
var generatePreviews = function () {
125-
if (options.imageTypes.test(fileInfo.name)) {
126-
_.each(options.imageVersions, function (value, version) {
127-
counter++;
128-
// creating directory recursive
129-
mkdirp(options.uploadDir() + '/' + version + '/', function (err, made) {
130-
var opts = options.imageVersions[version];
131-
imageMagick.resize({
132-
width: opts.width,
133-
height: opts.height,
134-
srcPath: options.uploadDir() + '/' + fileInfo.name,
135-
dstPath: options.uploadDir() + '/' + version + '/' + fileInfo.name,
136-
customArgs: opts.imageArgs || ['-auto-orient']
137-
}, finish);
138-
});
124+
var generatePreviews = function () {
125+
if (options.imageTypes.test(fileInfo.name)) {
126+
_.each(options.imageVersions, function (value, version) {
127+
counter++;
128+
// creating directory recursive
129+
mkdirp(options.uploadDir() + '/' + version + '/', function (err, made) {
130+
var opts = options.imageVersions[version];
131+
imageMagick.resize({
132+
width: opts.width,
133+
height: opts.height,
134+
srcPath: options.uploadDir() + '/' + fileInfo.name,
135+
dstPath: options.uploadDir() + '/' + version + '/' + fileInfo.name,
136+
customArgs: opts.imageArgs || ['-auto-orient']
137+
}, finish);
139138
});
140-
}
139+
});
141140
}
141+
}
142142

143-
mkdirp(options.uploadDir() + '/', function(err, made) {
144-
fs.rename(file.path, options.uploadDir() + '/' + fileInfo.name, function (err) {
145-
if (!err) {
146-
generatePreviews();
143+
mkdirp(options.uploadDir() + '/', function(err, made) {
144+
fs.rename(file.path, options.uploadDir() + '/' + fileInfo.name, function (err) {
145+
if (!err) {
146+
generatePreviews();
147+
finish();
148+
} else {
149+
var is = fs.createReadStream(file.path);
150+
var os = fs.createWriteStream(options.uploadDir() + '/' + fileInfo.name);
151+
is.on('end', function (err) {
152+
if (!err) {
153+
fs.unlink(file.path);
154+
generatePreviews();
155+
}
147156
finish();
148-
} else {
149-
var is = fs.createReadStream(file.path);
150-
var os = fs.createWriteStream(options.uploadDir() + '/' + fileInfo.name);
151-
is.on('end', function (err) {
152-
if (!err) {
153-
fs.unlink(file.path);
154-
generatePreviews();
155-
}
156-
finish();
157-
});
158-
is.pipe(os);
159-
}
160-
});
157+
});
158+
is.pipe(os);
159+
}
161160
});
162-
}
163-
else finish();
164-
});
165-
})
166-
.on('aborted', function () {
167-
_.each(tmpFiles, function (file) {
168-
var fileInfo = map[path.basename(file)];
169-
self.emit('abort', fileInfo);
170-
fs.unlink(file);
171-
});
172-
})
173-
.on('error', function (e) {
174-
self.emit('error', e);
175-
})
176-
.on('progress', function (bytesReceived, bytesExpected) {
177-
if (bytesReceived > options.maxPostSize)
178-
self.req.connection.destroy();
179-
})
180-
.on('end', finish)
181-
.parse(self.req);
161+
});
162+
}
163+
else finish();
164+
});
165+
})
166+
.on('aborted', function () {
167+
_.each(tmpFiles, function (file) {
168+
var fileInfo = map[path.basename(file)];
169+
self.emit('abort', fileInfo);
170+
fs.unlink(file);
171+
});
172+
})
173+
.on('error', function (e) {
174+
self.emit('error', e);
175+
})
176+
.on('progress', function (bytesReceived, bytesExpected) {
177+
if (bytesReceived > options.maxPostSize)
178+
self.req.connection.destroy();
179+
})
180+
.on('end', finish)
181+
.parse(self.req);
182182
};
183183

184184
UploadHandler.prototype.destroy = function () {
@@ -202,36 +202,44 @@ module.exports = function (options) {
202202

203203
UploadHandler.prototype.initUrls = function (fileInfo, cb) {
204204
//http://<<bucket>>.s3-<<region>>.amazonaws.com
205-
var baseUrl = (options.ssl ? 'https:' : 'http:') + '//' + options.s3Bucket() + '.s3-' + options.s3Region() + '.amazonaws.com';
205+
var baseUrl = (options.ssl ? 'https:' : 'http:') + '//' + options.s3Bucket() + '.s3-' + options.s3Region() + '.amazonaws.com/';
206206
fileInfo.setUrl(null, baseUrl + options.s3Url());
207-
fileInfo.setUrl('delete', baseUrl + this.req.s3Url);
208-
async.each(Object.keys(options.imageVersions), function(version, cb) {
209-
fs.exists(options.uploadDir() + '/' + version + '/' + fileInfo.name, function(exists) {
210-
if (exists) fileInfo.setUrl(version, baseUrl + options.s3Url() + '/' + version);
211-
cb(null);
212-
})
213-
},
214-
cb);
207+
fileInfo.setUrl('delete', baseUrl + options.s3Url());
208+
async.each(Object.keys(options.imageVersions), _.bind(function(version, cb) {
209+
//fs.exists(options.uploadDir() + '/' + version + '/' + fileInfo.name, function(exists) {
210+
/*if (exists)*/ fileInfo.setUrl(version, baseUrl + this.removeExtraSlashes(options.s3Url() + '/' + version));
211+
cb(null);
212+
//})
213+
}, this), cb);
215214
};
216215

217216
UploadHandler.prototype.moveToS3 = function(fileInfo) {
218217
var fileName = options.uploadDir() + '/' + fileInfo.name;
219218
fs.readFile(fileName, _.bind(function(err, data) {
220219
if(!err) {
221-
this.uploadToS3(options.s3Url() + '/' + fileInfo.name, data);
220+
this.uploadToS3(this.removeExtraSlashes(options.s3Url() + '/' + fileInfo.name, data));
222221

223222
async.each(Object.keys(options.imageVersions), _.bind(function(version) {
224223
fileName = options.uploadDir() + '/' + version + '/' + fileInfo.name;
225224
fs.readFile(fileName, _.bind(function(err, data) {
226225
if(!err) {
227-
this.uploadToS3(options.s3Url() + '/' + version + '/' + fileInfo.name, data);
226+
this.uploadToS3(this.removeExtraSlashes(options.s3Url() + '/' + version + '/' + fileInfo.name, data));
228227
}
229228
}, this));
230229
}, this));
231230
}
232231
}, this));
233232
};
234-
233+
234+
UploadHandler.prototype.removeExtraSlashes = function(filePath) {
235+
if(filePath.indexOf('//') !== -1) {
236+
filePath = filePath.replace('//', '/');
237+
filePath = this.removeExtraSlashes(filePath);
238+
}
239+
240+
return filePath;
241+
};
242+
235243
UploadHandler.prototype.uploadToS3 = function(fileName, data) {
236244
fs.unlink(fileName, _.bind(function(err){
237245
this.emit('error', err);

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"express",
99
"middleware"
1010
],
11-
"version": "0.0.1",
11+
"version": "0.0.2",
1212
"dependencies": {
1313
"async": "*",
1414
"aws-sdk": "^2.0.17",
@@ -30,6 +30,6 @@
3030
"scripts": {
3131
"test": "echo \"Error: no test specified\" && exit 1"
3232
},
33-
"_id": "jquery-cloud-file-upload-middleware@0.0.1",
33+
"_id": "jquery-cloud-file-upload-middleware@0.0.2",
3434
"license": "MIT"
3535
}

0 commit comments

Comments
 (0)