From 9725f0a4124afc065316f43303cf3b3434bc90dc Mon Sep 17 00:00:00 2001 From: "David Humphrey (:humph) david.humphrey@senecacollege.ca" Date: Fri, 28 Mar 2014 10:56:04 -0400 Subject: [PATCH] Improve automatic filename extraction for wget --- src/shell/shell.js | 10 +++++++++- tests/spec/shell/wget.spec.js | 3 +-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/shell/shell.js b/src/shell/shell.js index 3a1f7e0..af9e928 100644 --- a/src/shell/shell.js +++ b/src/shell/shell.js @@ -441,7 +441,15 @@ define(function(require) { 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); function onerror() { diff --git a/tests/spec/shell/wget.spec.js b/tests/spec/shell/wget.spec.js index 3608c8c..c2ab974 100644 --- a/tests/spec/shell/wget.spec.js +++ b/tests/spec/shell/wget.spec.js @@ -40,8 +40,7 @@ define(["Filer", "util"], function(Filer, util) { shell.wget(url, function(err, path) { if(err) throw err; - // The filename should be something like /file-13424512341 - expect(path).to.match(/^\/file\-\d+$/); + expect(path).to.equal('/test-file.txt'); fs.readFile(path, 'utf8', function(err, data) { if(err) throw err;