mirror of
https://github.com/Llewellynvdm/nativefier.git
synced 2024-09-28 04:19:01 +00:00
Add check for correct icon format
This commit is contained in:
parent
6eaf335ba5
commit
e1f2e662e9
@ -1,5 +1,7 @@
|
|||||||
import os from 'os';
|
import os from 'os';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
import hasBinary from 'hasbin';
|
||||||
|
import path from 'path';
|
||||||
|
|
||||||
function isOSX() {
|
function isOSX() {
|
||||||
return os.platform() === 'darwin';
|
return os.platform() === 'darwin';
|
||||||
@ -15,12 +17,87 @@ function downloadFile(fileUrl) {
|
|||||||
responseType: 'arraybuffer'
|
responseType: 'arraybuffer'
|
||||||
})
|
})
|
||||||
.then(function(response) {
|
.then(function(response) {
|
||||||
return response.data;
|
if (!response.data) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
data: response.data,
|
||||||
|
ext: path.extname(fileUrl)
|
||||||
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function allowedIconFormats(platform) {
|
||||||
|
const hasIdentify = hasBinary.sync('identify');
|
||||||
|
const hasConvert = hasBinary.sync('convert');
|
||||||
|
const hasIconUtil = hasBinary.sync('iconutil');
|
||||||
|
|
||||||
|
const pngToIcns = hasConvert && hasIconUtil;
|
||||||
|
const pngToIco = hasConvert;
|
||||||
|
const icoToIcns = pngToIcns && hasIdentify;
|
||||||
|
const icoToPng = hasConvert;
|
||||||
|
|
||||||
|
// todo scripts for the following
|
||||||
|
const icnsToPng = false;
|
||||||
|
const icnsToIco = false;
|
||||||
|
|
||||||
|
const formats = [];
|
||||||
|
|
||||||
|
// todo shell scripting is not supported on windows, temporary override
|
||||||
|
if (isWindows()) {
|
||||||
|
switch (platform) {
|
||||||
|
case 'darwin':
|
||||||
|
formats.push('.icns');
|
||||||
|
break;
|
||||||
|
case 'linux':
|
||||||
|
formats.push('.png');
|
||||||
|
break;
|
||||||
|
case 'win32':
|
||||||
|
formats.push('ico');
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw `function allowedIconFormats error: Unknown platform ${platform}`;
|
||||||
|
}
|
||||||
|
return formats;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (platform) {
|
||||||
|
case 'darwin':
|
||||||
|
formats.push('.icns');
|
||||||
|
if (pngToIcns) {
|
||||||
|
formats.push('.png');
|
||||||
|
}
|
||||||
|
if (icoToIcns) {
|
||||||
|
formats.push('.ico');
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'linux':
|
||||||
|
formats.push('.png');
|
||||||
|
if (icoToPng) {
|
||||||
|
formats.push('.ico');
|
||||||
|
}
|
||||||
|
if (icnsToPng) {
|
||||||
|
formats.push('icns');
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'win32':
|
||||||
|
formats.push('.ico');
|
||||||
|
if (pngToIco) {
|
||||||
|
formats.push('.png');
|
||||||
|
}
|
||||||
|
if (icnsToIco) {
|
||||||
|
formats.push('icns');
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw `function allowedIconFormats error: Unknown platform ${platform}`;
|
||||||
|
}
|
||||||
|
return formats;
|
||||||
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
isOSX,
|
isOSX,
|
||||||
isWindows,
|
isWindows,
|
||||||
downloadFile
|
downloadFile,
|
||||||
|
allowedIconFormats
|
||||||
};
|
};
|
||||||
|
@ -3,27 +3,38 @@ import path from 'path';
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import tmp from 'tmp';
|
import tmp from 'tmp';
|
||||||
import gitCloud from 'gitcloud';
|
import gitCloud from 'gitcloud';
|
||||||
|
import helpers from './../helpers/helpers';
|
||||||
|
|
||||||
|
const {downloadFile, allowedIconFormats} = helpers;
|
||||||
tmp.setGracefulCleanup();
|
tmp.setGracefulCleanup();
|
||||||
|
|
||||||
import helpers from './../helpers/helpers';
|
|
||||||
const {downloadFile} = helpers;
|
|
||||||
|
|
||||||
function inferIconFromStore(targetUrl, platform) {
|
function inferIconFromStore(targetUrl, platform) {
|
||||||
if (platform === 'win32') {
|
const allowedFormats = allowedIconFormats(platform);
|
||||||
return new Promise((resolve, reject) => reject('Skipping icon retrieval from store on windows'));
|
|
||||||
}
|
|
||||||
|
|
||||||
return gitCloud('http://jiahaog.com/nativefier-icons/')
|
return gitCloud('http://jiahaog.com/nativefier-icons/')
|
||||||
.then(fileIndex => {
|
.then(fileIndex => {
|
||||||
const matchingUrls = fileIndex
|
const matchingIcons = fileIndex
|
||||||
.filter(item => {
|
.filter(item => {
|
||||||
|
// todo might have problems with matching length, e.g. `book` vs `facebook`
|
||||||
return targetUrl
|
return targetUrl
|
||||||
.toLowerCase()
|
.toLowerCase()
|
||||||
.includes(item.name);
|
.includes(item.name);
|
||||||
})
|
})
|
||||||
.map(item => item.url);
|
.map(item => {
|
||||||
|
item.ext = path.extname(item.url);
|
||||||
|
return item;
|
||||||
|
});
|
||||||
|
|
||||||
|
let matchingUrl;
|
||||||
|
for (let format of allowedFormats) {
|
||||||
|
for (let icon of matchingIcons) {
|
||||||
|
if (icon.ext !== format) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
matchingUrl = icon.url;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const matchingUrl = matchingUrls[0];
|
|
||||||
if (!matchingUrl) {
|
if (!matchingUrl) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -49,11 +60,14 @@ function inferFromPage(targetUrl, platform, outDir) {
|
|||||||
preferredExt = 'ico';
|
preferredExt = 'ico';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// todo might want to pass list of preferences instead
|
||||||
return pageIcon(targetUrl, {ext: preferredExt})
|
return pageIcon(targetUrl, {ext: preferredExt})
|
||||||
.then(icon => {
|
.then(icon => {
|
||||||
if (!icon) {
|
if (!icon) {
|
||||||
throw 'Icon not found';
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// note that ext from page icon does not contain a '.'
|
||||||
const outfilePath = path.join(outDir, `/icon.${icon.ext}`);
|
const outfilePath = path.join(outDir, `/icon.${icon.ext}`);
|
||||||
return writeFilePromise(outfilePath, icon.data);
|
return writeFilePromise(outfilePath, icon.data);
|
||||||
});
|
});
|
||||||
@ -67,18 +81,13 @@ function inferFromPage(targetUrl, platform, outDir) {
|
|||||||
function inferIconFromUrlToPath(targetUrl, platform, outDir) {
|
function inferIconFromUrlToPath(targetUrl, platform, outDir) {
|
||||||
|
|
||||||
return inferIconFromStore(targetUrl, platform)
|
return inferIconFromStore(targetUrl, platform)
|
||||||
.then(iconData => {
|
.then(icon => {
|
||||||
|
if (!icon) {
|
||||||
if (!iconData) {
|
return inferFromPage(targetUrl, platform, outDir);
|
||||||
throw 'Unable to find icon from store';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const outfilePath = path.join(outDir, `/icon.png`);
|
const outfilePath = path.join(outDir, `/icon${icon.ext}`);
|
||||||
return writeFilePromise(outfilePath, iconData);
|
return writeFilePromise(outfilePath, icon.data);
|
||||||
}).catch(error => {
|
|
||||||
console.warn('Unable to find icon on store', error);
|
|
||||||
console.warn('Falling back to inferring from url');
|
|
||||||
return inferFromPage(targetUrl, platform, outDir);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user