Skip to content
This repository was archived by the owner on Sep 29, 2023. It is now read-only.
Open
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"axios": "^0.9.1",
"cheerio": "^0.20.0",
"file-type": "^3.8.0",
"image-size": "^0.5.0",
"source-map-support": "^0.4.0"
},
"devDependencies": {
Expand Down
10 changes: 10 additions & 0 deletions src/modules/download/downloadIcon.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const axios = require('axios');
const url = require('url');
const fileType = require('file-type');
const imageSize = require('image-size');


function getExtension(downloadUrl) {
return downloadUrl.match(/\.(png|jpg|ico)/)[0];
Expand Down Expand Up @@ -41,7 +43,15 @@ function downloadIcon(iconUrl) {
// add `.` to ext
fileDetails.ext = `.${fileDetails.ext}`;

var dimension;
try {
dimension = imageSize(iconData);
} catch (e) {
dimension = { width: 0, height: 0 };
}

return Object.assign({
dimension: dimension,
source: iconUrl,
name: getSiteDomain(iconUrl),
data: iconData,
Expand Down
37 changes: 30 additions & 7 deletions src/modules/findBestIcon.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
function checkSquareDimension(dimension) {
if (dimension == null || dimension.width <= 0 || dimension.height <= 0) {
return false;
}

return dimension.width === dimension.height;
}

function compareByDimension(a, b) {
var ra = checkSquareDimension(a.dimension);
var rb = checkSquareDimension(b.dimension);

if (rb == true && ra == false) {
return 1;
} else if (rb == false && ra == true) {
return -1;
}

return b.dimension.width - a.dimension.width;
}

function sortIconsByDimension(icons) {
return icons.sort(compareByDimension);
}

function sortIconsBySize(icons) {
return icons.sort((a, b) => {
if (a.size < b.size) {
return 1;
} else {
return -1;
}
return icons.sort(function (a, b) {
return b.size - a.size;
});
}

Expand All @@ -14,7 +35,9 @@ function sortIconsBySize(icons) {
* @param [ext]
*/
function findBestIcon(icons, ext) {
const sorted = sortIconsBySize(icons);
var sortingFunc = ext == '.ico' ? sortIconsBySize : sortIconsByDimension; //image-size doesn't support .ico.
var sorted = sortingFunc(icons);

if (ext) {
for (let icon of sorted) {
if (icon.ext === ext) {
Expand Down