Improve automatic filename extraction for wget

This commit is contained in:
David Humphrey (:humph) david.humphrey@senecacollege.ca 2014-03-28 10:56:04 -04:00
parent 3e11408f44
commit 9725f0a412
2 changed files with 10 additions and 3 deletions

View File

@ -441,7 +441,15 @@ define(function(require) {
return; return;
} }
var path = options.filename || ('file-' + Date.now()); var path = options.filename || (function() {
// Strip any query string or hash portions of the url first, then
// Grab whatever is after the last / (assuming there is one) and
// remove any non-filename type chars.
var filename = url.replace(/\?.*$/, '');
filename = filename.replace(/\#.*$/, '');
filename = filename.split('/').pop();
return filename.replace(/[^a-zA-Z0-9-_\.\~]/, '');
}());
path = Path.resolve(fs.cwd, path); path = Path.resolve(fs.cwd, path);
function onerror() { function onerror() {

View File

@ -40,8 +40,7 @@ define(["Filer", "util"], function(Filer, util) {
shell.wget(url, function(err, path) { shell.wget(url, function(err, path) {
if(err) throw err; if(err) throw err;
// The filename should be something like /file-13424512341 expect(path).to.equal('/test-file.txt');
expect(path).to.match(/^\/file\-\d+$/);
fs.readFile(path, 'utf8', function(err, data) { fs.readFile(path, 'utf8', function(err, data) {
if(err) throw err; if(err) throw err;