Skip to content

Commit 2d7a193

Browse files
cslees0ph1e
authored andcommitted
Add domain directory in bySiteStructure filenameGenerator (#199) (#213)
1 parent ab7c7f5 commit 2d7a193

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

lib/filename-generator/by-site-structure.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ var resourceTypeExtensions = require('../config/resource-ext-by-type');
66

77
module.exports = function generateFilename (resource, options) {
88
var resourceUrl = resource.getUrl();
9+
var host = utils.getHostFromUrl(resourceUrl);
910
var filePath = utils.getFilepathFromUrl(resourceUrl);
1011
var extension = utils.getFilenameExtension(filePath);
1112

13+
filePath = path.join(host.replace(':', '_'), filePath);
14+
1215
// If we have HTML from 'http://example.com/path' => set 'path/index.html' as filepath
1316
if (resource.isHtml()) {
1417
var htmlExtensions = resourceTypeExtensions[resourceTypes.html];

lib/utils/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ function getHashFromUrl (u) {
7171
return url.parse(u).hash || '';
7272
}
7373

74+
/**
75+
* Returns host with port from given url
76+
* Example: http://example.com:8080/some/path/file.js => example.com:8080
77+
* @param {string} u - url
78+
* @returns {string} host with port
79+
*/
80+
function getHostFromUrl (u) {
81+
return url.parse(u).host;
82+
}
83+
7484
/**
7585
* Returns extension for given filepath
7686
* Example: some/path/file.js => .js
@@ -135,6 +145,7 @@ module.exports = {
135145
getFilepathFromUrl,
136146
getFilenameExtension,
137147
getHashFromUrl,
148+
getHostFromUrl,
138149
shortenFilename,
139150
waitAllFulfilled,
140151
normalizeUrl,

test/unit/filename-generator/by-site-structure-test.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,49 +10,54 @@ var options = { defaultFilename: 'index.html' };
1010
describe('FilenameGenerator: bySiteStructure', function() {
1111
it('should return the normalized relative path of the resource url', function(){
1212
var r1 = new Resource('http://example.com/some/path/a.png');
13-
bySiteStructureFilenameGenerator(r1, options).should.equalFileSystemPath('some/path/a.png');
13+
bySiteStructureFilenameGenerator(r1, options).should.equalFileSystemPath('example.com/some/path/a.png');
1414

1515
var r2 = new Resource('http://example.com/a.png');
16-
bySiteStructureFilenameGenerator(r2, options).should.equalFileSystemPath('a.png');
16+
bySiteStructureFilenameGenerator(r2, options).should.equalFileSystemPath('example.com/a.png');
1717

1818
var r3 = new Resource('http://example.com/some/path/../images/a.png');
19-
bySiteStructureFilenameGenerator(r3, options).should.equalFileSystemPath('some/images/a.png');
19+
bySiteStructureFilenameGenerator(r3, options).should.equalFileSystemPath('example.com/some/images/a.png');
20+
});
21+
22+
it('should replace the colon, for url with port number', function(){
23+
var r1 = new Resource('http://example.com:8080/some/path/a.png');
24+
bySiteStructureFilenameGenerator(r1, options).should.equalFileSystemPath('example.com_8080/some/path/a.png');
2025
});
2126

2227
it('should add the defaultFilename to the path, for html resources without extension', function(){
2328
var isHtmlMock = sinon.stub().returns(true);
2429

2530
var r1 = new Resource('http://example.com/some/path/');
2631
r1.isHtml = isHtmlMock;
27-
bySiteStructureFilenameGenerator(r1, options).should.equalFileSystemPath('some/path/index.html');
32+
bySiteStructureFilenameGenerator(r1, options).should.equalFileSystemPath('example.com/some/path/index.html');
2833

2934
var r2 = new Resource('http://example.com/some/path');
3035
r2.isHtml = isHtmlMock;
31-
bySiteStructureFilenameGenerator(r2, options).should.equalFileSystemPath('some/path/index.html');
36+
bySiteStructureFilenameGenerator(r2, options).should.equalFileSystemPath('example.com/some/path/index.html');
3237

3338
var r3 = new Resource('http://example.com');
3439
r3.isHtml = isHtmlMock;
35-
bySiteStructureFilenameGenerator(r3, options).should.equalFileSystemPath('index.html');
40+
bySiteStructureFilenameGenerator(r3, options).should.equalFileSystemPath('example.com/index.html');
3641
});
3742

3843
it('should add the defaultFilename to the path, for html resources with wrong extension', function(){
3944
var isHtmlMock = sinon.stub().returns(true);
4045

4146
var r1 = new Resource('http://example.com/some/path/test.com');
4247
r1.isHtml = isHtmlMock;
43-
bySiteStructureFilenameGenerator(r1, options).should.equalFileSystemPath('some/path/test.com/index.html');
48+
bySiteStructureFilenameGenerator(r1, options).should.equalFileSystemPath('example.com/some/path/test.com/index.html');
4449
});
4550

4651
it('should normalize to safe relative paths, without ..', function(){
4752
var r = new Resource('http://example.com/some/path/../../../../images/a.png');
48-
bySiteStructureFilenameGenerator(r, options).should.equalFileSystemPath('images/a.png');
53+
bySiteStructureFilenameGenerator(r, options).should.equalFileSystemPath('example.com/images/a.png');
4954
});
5055

5156
it('should not replace thrice dot in filenames', function() {
5257
// if it replaces them we receive 'some/path/../../../../etc/passwd'
5358
// path.resolve('some/path/../../../../etc/passwd'); = '/etc/passwd' => which is not safe
5459
var r = new Resource('http://example.com/some/path/.../.../.../.../etc/passwd');
55-
bySiteStructureFilenameGenerator(r, options).should.equalFileSystemPath('some/path/.../.../.../.../etc/passwd');
60+
bySiteStructureFilenameGenerator(r, options).should.equalFileSystemPath('example.com/some/path/.../.../.../.../etc/passwd');
5661
});
5762

5863
it('should shorten filename', function() {
@@ -74,10 +79,10 @@ describe('FilenameGenerator: bySiteStructure', function() {
7479
it('should return decoded filepath', function() {
7580
var r = new Resource('https://developer.mozilla.org/ru/docs/JavaScript_%D1%88%D0%B5%D0%BB%D0%BB%D1%8B');
7681
var filename = bySiteStructureFilenameGenerator(r, options);
77-
filename.should.equalFileSystemPath('ru/docs/JavaScript_шеллы');
82+
filename.should.equalFileSystemPath('developer.mozilla.org/ru/docs/JavaScript_шеллы');
7883

7984
var r2 = new Resource('https://developer.mozilla.org/Hello%20G%C3%BCnter.png');
8085
var filename2 = bySiteStructureFilenameGenerator(r2, options);
81-
filename2.should.equalFileSystemPath('Hello Günter.png');
86+
filename2.should.equalFileSystemPath('developer.mozilla.org/Hello Günter.png');
8287
});
8388
});

0 commit comments

Comments
 (0)