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
30 changes: 19 additions & 11 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ function isUrl (path) {
}

function getUrl (currentUrl, path) {
var pathObject = url.parse(path);
const pathObject = url.parse(path);
if (isUrl(path) && !pathObject.protocol) {
var urlObject = url.parse(currentUrl);
const urlObject = url.parse(currentUrl);
pathObject.protocol = urlObject.protocol;
path = url.format(pathObject);
}
Expand All @@ -33,8 +33,8 @@ function getUnixPath (filepath) {
}

function getRelativePath (path1, path2) {
var dirname = path.dirname(path1);
var relativePath = path.relative(dirname, path2);
const dirname = path.dirname(path1);
const relativePath = path.relative(dirname, path2);
return getUnixPath(relativePath);
}

Expand All @@ -45,8 +45,12 @@ function getRelativePath (path1, path2) {
* @returns {string} decoded pathname
*/
function getPathnameFromUrl (u) {
var pathname = url.parse(u).pathname;
return decodeURI(pathname);
const pathname = url.parse(u).pathname;
try {
return decodeURI(pathname);
} catch (e) {
return pathname;
}
}

/**
Expand Down Expand Up @@ -96,8 +100,8 @@ function getFilenameExtension (filepath) {

function shortenFilename (filename) {
if (filename.length >= MAX_FILENAME_LENGTH) {
var shortFilename = filename.substring(0, 20) + getFilenameExtension(filename);
logger.debug('shorten filename: ' + filename + ' -> ' + shortFilename);
const shortFilename = filename.substring(0, 20) + getFilenameExtension(filename);
logger.debug(`[utils] shorten filename: ${filename} -> ${shortFilename}`);
return shortFilename;
}
return filename;
Expand All @@ -110,15 +114,19 @@ function waitAllFulfilled (promises) {
}

function normalizeUrl (u, opts) {
return normalize(u, extend({removeTrailingSlash: false}, opts));
try {
return normalize(u, extend({removeTrailingSlash: false}, opts));
} catch (e) {
return u;
}
}

function urlsEqual (url1, url2) {
return normalizeUrl(url1) === normalizeUrl(url2);
}

function isUriSchemaSupported (path) {
var protocol = url.parse(path).protocol;
const protocol = url.parse(path).protocol;
return !protocol || protocol && isUrl(path);
}

Expand All @@ -127,7 +135,7 @@ function getTypeByMime (mimeType) {
}

function getTypeByFilename (filename) {
var ext = getFilenameExtension(filename);
const ext = getFilenameExtension(filename);
return typeByExt[ext];
}

Expand Down
12 changes: 11 additions & 1 deletion test/unit/utils/utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ describe('Utils', function () {
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');
});
it('should return path as is if url is malformed', () => {
utils.getFilepathFromUrl('https://example.co/%%IMAGE%%/logo.png').should.equal('%%IMAGE%%/logo.png');
});
});

describe('#getHashFromUrl', function () {
Expand Down Expand Up @@ -225,5 +228,12 @@ describe('Utils', function () {
it('should return false for /path and /path/', function() {
should(utils.urlsEqual('http://example.com/path', 'http://example.com/path/')).be.eql(false);
});
})
});

describe('#normalizeUrl', () => {
it('should return original url if it is malformed', () => {
const malformedUrl = 'http://example.com/%%IMAGEURL%%/bar1q2blitz.png';
should(utils.normalizeUrl(malformedUrl)).be.eql(malformedUrl);
});
});
});