Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,25 @@ 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
* @param {string} u - url
* @returns {string} filename
*/
function getFilenameFromUrl (u) {
return path.basename(url.parse(u).pathname);
return path.basename(getPathnameFromUrl(u));
}

/**
Expand All @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions test/unit/filename-generator/by-site-structure-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
10 changes: 10 additions & 0 deletions test/unit/filename-generator/by-type-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
40 changes: 25 additions & 15 deletions test/unit/utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +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.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');
Expand All @@ -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.equal('logo/logo-mobile (1).svg');
});
});

describe('#getHashFromUrl', function () {
Expand Down