2016-03-08 12:29:04 +00:00
|
|
|
import pageIcon from 'page-icon';
|
2016-01-28 13:13:57 +00:00
|
|
|
import path from 'path';
|
|
|
|
import fs from 'fs';
|
|
|
|
import tmp from 'tmp';
|
|
|
|
tmp.setGracefulCleanup();
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {string} targetUrl
|
2016-03-09 06:50:25 +00:00
|
|
|
* @param {string} platform
|
2016-01-28 13:13:57 +00:00
|
|
|
* @param {string} outDir
|
|
|
|
* @param {inferIconCallback} callback
|
|
|
|
*/
|
2016-03-09 06:50:25 +00:00
|
|
|
function inferIconFromUrlToPath(targetUrl, platform, outDir, callback) {
|
|
|
|
let preferredExt = 'png';
|
|
|
|
if (platform === 'win32') {
|
|
|
|
preferredExt = 'ico';
|
|
|
|
}
|
|
|
|
|
|
|
|
pageIcon(targetUrl, {ext: preferredExt})
|
2016-03-08 12:29:04 +00:00
|
|
|
.then(icon => {
|
2016-03-08 15:57:19 +00:00
|
|
|
const outfilePath = path.join(outDir, `/icon.${icon.ext}`);
|
2016-03-08 12:29:04 +00:00
|
|
|
fs.writeFile(outfilePath, icon.data, error => {
|
2016-01-28 13:13:57 +00:00
|
|
|
callback(error, outfilePath);
|
|
|
|
});
|
2016-03-08 12:29:04 +00:00
|
|
|
})
|
|
|
|
.catch(callback);
|
2016-01-28 13:13:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @callback inferIconCallback
|
|
|
|
* @param error
|
|
|
|
* @param {string} [iconPath]
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} targetUrl
|
2016-03-09 06:50:25 +00:00
|
|
|
* @param {string} platform
|
2016-01-28 13:13:57 +00:00
|
|
|
* @param {inferIconCallback} callback
|
|
|
|
*/
|
2016-03-09 06:50:25 +00:00
|
|
|
function inferIcon(targetUrl, platform, callback) {
|
2016-01-28 13:13:57 +00:00
|
|
|
const tmpObj = tmp.dirSync({unsafeCleanup: true});
|
|
|
|
const tmpPath = tmpObj.name;
|
2016-03-09 06:50:25 +00:00
|
|
|
inferIconFromUrlToPath(targetUrl, platform, tmpPath, callback);
|
2016-01-28 13:13:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default inferIcon;
|