From 02dfc742c9ce07e2d0b6b9841344cdff9efb08a4 Mon Sep 17 00:00:00 2001 From: s0ph1e Date: Tue, 31 Jan 2017 12:05:16 +0200 Subject: [PATCH 1/2] Decode url-based filenames --- lib/utils.js | 17 ++++++++++++++--- .../by-site-structure-test.js | 10 ++++++++++ test/unit/filename-generator/by-type-test.js | 10 ++++++++++ test/unit/utils-test.js | 10 ++++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index d6a2da7b..2f89279c 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -35,6 +35,17 @@ function getRelativePath (path1, path2) { return getUnixPath(relativePath); } +/** + * Returns decoded pathname from url + * Example: https://example.co/path/logo%20(1).svg => /path/logo (1).svg + * @param u - url + * @returns {string} decoded pathname + */ +function getPathnameFromUrl (u) { + var pathname = url.parse(u).pathname; + return decodeURI(pathname); +} + /** * Returns filename from given url * Example: http://example.com/some/path/file.js => file.js @@ -42,7 +53,7 @@ function getRelativePath (path1, path2) { * @returns {string} filename */ function getFilenameFromUrl (u) { - return path.basename(url.parse(u).pathname); + return path.basename(getPathnameFromUrl(u)); } /** @@ -52,8 +63,8 @@ function getFilenameFromUrl (u) { * @returns {string} path */ function getFilepathFromUrl (u) { - var normalizedUrl = normalizeUrl(u); - return url.parse(normalizedUrl).pathname.substring(1); + var nu = normalizeUrl(u); + return getPathnameFromUrl(nu).substring(1); } function getHashFromUrl (u) { diff --git a/test/unit/filename-generator/by-site-structure-test.js b/test/unit/filename-generator/by-site-structure-test.js index bf32686a..5e0077d3 100644 --- a/test/unit/filename-generator/by-site-structure-test.js +++ b/test/unit/filename-generator/by-site-structure-test.js @@ -70,4 +70,14 @@ describe('FilenameGenerator: bySiteStructure', function() { var filename = _.last(filepath.split('/')); should(filename.length).be.lessThan(255); }); + + it('should return decoded filepath', function() { + var r = new Resource('https://developer.mozilla.org/ru/docs/JavaScript_%D1%88%D0%B5%D0%BB%D0%BB%D1%8B'); + var filename = bySiteStructureFilenameGenerator(r, options); + filename.should.equalFileSystemPath('ru/docs/JavaScript_шеллы'); + + var r2 = new Resource('https://developer.mozilla.org/Hello%20G%C3%BCnter.png'); + var filename2 = bySiteStructureFilenameGenerator(r2, options); + filename2.should.equalFileSystemPath('Hello Günter.png'); + }); }); diff --git a/test/unit/filename-generator/by-type-test.js b/test/unit/filename-generator/by-type-test.js index 03c36c1b..3a2f55cb 100644 --- a/test/unit/filename-generator/by-type-test.js +++ b/test/unit/filename-generator/by-type-test.js @@ -111,4 +111,14 @@ describe('FilenameGenerator: byType', function() { should(f2).not.be.eql(f1); }); + + it('should return decoded url-based filename', function() { + var r = new Resource('https://developer.mozilla.org/ru/docs/JavaScript_%D1%88%D0%B5%D0%BB%D0%BB%D1%8B'); + var filename = byTypeFilenameGenerator(r, {}, []); + filename.should.equalFileSystemPath('JavaScript_шеллы'); + + var r2 = new Resource('https://developer.mozilla.org/Hello%20G%C3%BCnter.png'); + var filename2 = byTypeFilenameGenerator(r2, {}, []); + filename2.should.equalFileSystemPath('Hello Günter.png'); + }); }); diff --git a/test/unit/utils-test.js b/test/unit/utils-test.js index ead60148..6ea3879d 100644 --- a/test/unit/utils-test.js +++ b/test/unit/utils-test.js @@ -60,6 +60,9 @@ describe('Utils', function () { it('should return unconverted filename if there are no ?,#', function () { utils.getFilenameFromUrl('index.html').should.equalFileSystemPath('index.html'); }); + it('should decode escaped chars', function () { + utils.getFilenameFromUrl('https://example.co/logo-mobile%20(1).svg?q=650').should.equalFileSystemPath('logo-mobile (1).svg'); + }); }); describe('#getFilepathFromUrl', function () { @@ -81,6 +84,13 @@ describe('Utils', function () { utils.getFilepathFromUrl('http://example.com/some/path/').should.equal('some/path'); utils.getFilepathFromUrl('http://example.com/some/path/file.css/').should.equal('some/path/file.css'); }); + it('should normalize slashes', function() { + utils.getFilepathFromUrl('http://example.com///some//path').should.equal('some/path'); + utils.getFilepathFromUrl('http://example.com//////////file.css/').should.equal('file.css'); + }); + it('should decode escaped chars', function () { + utils.getFilepathFromUrl('https://example.co/logo/logo-mobile%20(1).svg?q=650').should.equalFileSystemPath('logo/logo-mobile (1).svg'); + }); }); describe('#getHashFromUrl', function () { From 3e0e5369df52fc128b3e5aced3b82e0640e51977 Mon Sep 17 00:00:00 2001 From: s0ph1e Date: Tue, 31 Jan 2017 12:44:34 +0200 Subject: [PATCH 2/2] Update tests --- test/unit/utils-test.js | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/test/unit/utils-test.js b/test/unit/utils-test.js index 6ea3879d..7c64d26e 100644 --- a/test/unit/utils-test.js +++ b/test/unit/utils-test.js @@ -48,31 +48,31 @@ describe('Utils', function () { describe('#getFilenameFromUrl(url)', function () { it('should return last path item as filename & trim all after first ? or #', function () { - utils.getFilenameFromUrl('http://example.com/index.html').should.equalFileSystemPath('index.html'); - utils.getFilenameFromUrl('http://example.com/p/a/t/h/index.html').should.equalFileSystemPath('index.html'); - utils.getFilenameFromUrl('http://example.com/index.html?12').should.equalFileSystemPath('index.html'); - utils.getFilenameFromUrl('http://example.com/index.html#t?12').should.equalFileSystemPath('index.html'); - utils.getFilenameFromUrl('http://example.com/index.html?12#t').should.equalFileSystemPath('index.html'); - utils.getFilenameFromUrl('http://example.com/?12_jdlsk').should.be.empty(); - utils.getFilenameFromUrl('http://example.com/#index.html').should.be.empty(); - utils.getFilenameFromUrl('http://example.com/').should.be.empty(); + utils.getFilenameFromUrl('http://example.com/index.html').should.equal('index.html'); + utils.getFilenameFromUrl('http://example.com/p/a/t/h/index.html').should.equal('index.html'); + utils.getFilenameFromUrl('http://example.com/index.html?12').should.equal('index.html'); + utils.getFilenameFromUrl('http://example.com/index.html#t?12').should.equal('index.html'); + utils.getFilenameFromUrl('http://example.com/index.html?12#t').should.equal('index.html'); + utils.getFilenameFromUrl('http://example.com/?12_jdlsk').should.equal(''); + utils.getFilenameFromUrl('http://example.com/#index.html').should.equal(''); + utils.getFilenameFromUrl('http://example.com/').should.equal(''); }); it('should return unconverted filename if there are no ?,#', function () { - utils.getFilenameFromUrl('index.html').should.equalFileSystemPath('index.html'); + utils.getFilenameFromUrl('index.html').should.equal('index.html'); }); it('should decode escaped chars', function () { - utils.getFilenameFromUrl('https://example.co/logo-mobile%20(1).svg?q=650').should.equalFileSystemPath('logo-mobile (1).svg'); + utils.getFilenameFromUrl('https://example.co/logo-mobile%20(1).svg?q=650').should.equal('logo-mobile (1).svg'); }); }); describe('#getFilepathFromUrl', function () { it('should return empty sting if url has no pathname', function() { - utils.getFilepathFromUrl('http://example.com').should.be.empty(); - utils.getFilepathFromUrl('http://example.com/').should.be.empty(); - utils.getFilepathFromUrl('http://example.com?').should.be.empty(); - utils.getFilepathFromUrl('http://example.com?abc=3').should.be.empty(); - utils.getFilepathFromUrl('http://example.com#').should.be.empty(); - utils.getFilepathFromUrl('http://example.com#test').should.be.empty(); + utils.getFilepathFromUrl('http://example.com').should.equal(''); + utils.getFilepathFromUrl('http://example.com/').should.equal(''); + utils.getFilepathFromUrl('http://example.com?').should.equal(''); + utils.getFilepathFromUrl('http://example.com?abc=3').should.equal(''); + utils.getFilepathFromUrl('http://example.com#').should.equal(''); + utils.getFilepathFromUrl('http://example.com#test').should.equal(''); }); it('should return path if url has pathname', function() { utils.getFilepathFromUrl('http://example.com/some/path').should.equal('some/path'); @@ -89,7 +89,7 @@ describe('Utils', function () { utils.getFilepathFromUrl('http://example.com//////////file.css/').should.equal('file.css'); }); it('should decode escaped chars', function () { - utils.getFilepathFromUrl('https://example.co/logo/logo-mobile%20(1).svg?q=650').should.equalFileSystemPath('logo/logo-mobile (1).svg'); + utils.getFilepathFromUrl('https://example.co/logo/logo-mobile%20(1).svg?q=650').should.equal('logo/logo-mobile (1).svg'); }); });