From 4a4172661cdf2ff8177e07a19f5f2befe3b21de7 Mon Sep 17 00:00:00 2001 From: s0ph1e Date: Tue, 8 Nov 2016 16:17:41 +0200 Subject: [PATCH 1/2] Fix incorrect filepath from url, fixes #140 --- lib/utils.js | 13 ++++++++++--- test/unit/utils-test.js | 4 ++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index d9800de5..33ac4171 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -49,15 +49,22 @@ function getFilenameFromUrl (u) { * @returns {string} path */ function getFilepathFromUrl (u) { - return url.parse(u).pathname.substring(1); + var normalizedUrl = normalizeUrl(u); + return url.parse(normalizedUrl).pathname.substring(1); } function getHashFromUrl (u) { return url.parse(u).hash || ''; } -function getFilenameExtension (filename) { - return (typeof filename === 'string') ? path.extname(filename) : null; +/** + * Returns extension for given filepath + * Example: some/path/file.js => .js + * @param {string} filepath + * @returns {string|null} - extension + */ +function getFilenameExtension (filepath) { + return (typeof filepath === 'string') ? path.extname(filepath) : null; } function shortenFilename (filename) { diff --git a/test/unit/utils-test.js b/test/unit/utils-test.js index fd138d63..02c0380f 100644 --- a/test/unit/utils-test.js +++ b/test/unit/utils-test.js @@ -77,6 +77,10 @@ describe('Common utils', function () { it('should return path including filename if url has pathname', function() { utils.getFilepathFromUrl('http://example.com/some/path/file.js').should.equal('some/path/file.js'); }); + it('should not contain trailing slash', 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'); + }); }); describe('#getHashFromUrl', function () { From 42934bee87f688978fe522e018feea74f6ab8e43 Mon Sep 17 00:00:00 2001 From: s0ph1e Date: Wed, 9 Nov 2016 18:52:14 +0200 Subject: [PATCH 2/2] Add default filenames for html resources with incorrect extension --- lib/config/resource-extensions-by-type.js | 10 ++++++++-- lib/filename-generator/by-site-structure.js | 11 +++++++++-- lib/filename-generator/by-type.js | 6 +++--- .../unit/filename-generator/by-site-structure-test.js | 8 ++++++++ 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/lib/config/resource-extensions-by-type.js b/lib/config/resource-extensions-by-type.js index 76c8f6ff..e6a98672 100644 --- a/lib/config/resource-extensions-by-type.js +++ b/lib/config/resource-extensions-by-type.js @@ -2,7 +2,13 @@ var types = require('./resource-types'); var defaultExtensions = {}; -defaultExtensions[types.html] = '.html'; -defaultExtensions[types.css] = '.css'; +defaultExtensions[types.html] = { + defaultExtension: '.html', + possibleExtensions: [ '.html', '.htm' ] +}; +defaultExtensions[types.css] = { + defaultExtension: '.css', + possibleExtensions: [ '.css' ] +}; module.exports = defaultExtensions; diff --git a/lib/filename-generator/by-site-structure.js b/lib/filename-generator/by-site-structure.js index 091a53a7..1ae5b5cf 100644 --- a/lib/filename-generator/by-site-structure.js +++ b/lib/filename-generator/by-site-structure.js @@ -1,6 +1,8 @@ var _ = require('lodash'); var path = require('path'); var utils = require('../utils'); +var resourceTypes = require('../config/resource-types'); +var resourceTypeExtensions = require('../config/resource-extensions-by-type'); module.exports = function generateFilename (resource, options) { var resourceUrl = resource.getUrl(); @@ -8,8 +10,13 @@ module.exports = function generateFilename (resource, options) { var extension = utils.getFilenameExtension(filePath); // If we have HTML from 'http://example.com/path' => set 'path/index.html' as filepath - if (resource.isHtml() && !extension) { - filePath = path.join(filePath, options.defaultFilename); + if (resource.isHtml()) { + var htmlExtensions = resourceTypeExtensions[resourceTypes.html].possibleExtensions; + var resourceHasHtmlExtension = _.includes(htmlExtensions, extension); + // add index.html only if filepath has ext != html '/path/test.com' => '/path/test.com/index.html' + if (!resourceHasHtmlExtension) { + filePath = path.join(filePath, options.defaultFilename); + } } return sanitizeFilepath(filePath); diff --git a/lib/filename-generator/by-type.js b/lib/filename-generator/by-type.js index e14a34f8..83a5971b 100644 --- a/lib/filename-generator/by-type.js +++ b/lib/filename-generator/by-type.js @@ -1,7 +1,7 @@ var _ = require('lodash'); var path = require('path'); var utils = require('../utils.js'); -var defaultExtensions = require('../config/resource-extensions-by-type'); +var typeExtensions = require('../config/resource-extensions-by-type'); module.exports = function generateFilename (resource, options, occupiedFileNames) { var occupiedNames = getSubDirectoryNames(options).concat(occupiedFileNames); @@ -32,8 +32,8 @@ function getFilenameForResource (resource, options) { var resourceType = resource.getType(); var extension = utils.getFilenameExtension(filename); - if (!extension && defaultExtensions[resourceType]) { - extension = defaultExtensions[resourceType]; + if (!extension && typeExtensions[resourceType]) { + extension = typeExtensions[resourceType].defaultExtension; filename += extension; } diff --git a/test/unit/filename-generator/by-site-structure-test.js b/test/unit/filename-generator/by-site-structure-test.js index 6a1c4f8a..6a04aea1 100644 --- a/test/unit/filename-generator/by-site-structure-test.js +++ b/test/unit/filename-generator/by-site-structure-test.js @@ -35,6 +35,14 @@ describe('byStructureFilenameGenerator', function() { bySiteStructureFilenameGenerator(r3, options).should.equalFileSystemPath('index.html'); }); + it('should add the defaultFilename to the path, for html resources with wrong extension', function(){ + var isHtmlMock = sinon.stub().returns(true); + + var r1 = new Resource('http://example.com/some/path/test.com'); + r1.isHtml = isHtmlMock; + bySiteStructureFilenameGenerator(r1, options).should.equalFileSystemPath('some/path/test.com/index.html'); + }); + it('should normalize to safe relative paths, without ..', function(){ var r = new Resource('http://example.com/some/path/../../../../images/a.png'); bySiteStructureFilenameGenerator(r, options).should.equalFileSystemPath('images/a.png');