File size: 1,300 Bytes
1fa2c88 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import { writeFile, readFile } from 'node:fs/promises';
import path from 'path';
import config from './config.js';
let ICON_LOCATION = path.join(import.meta.dirname, '../static/img/icon.png');
export async function download(){
const res = await fetch(config.addonIcon, {method: 'GET'});
if(!res.ok){
throw new Error('Network response was not ok');
}
let extension = null;
if(res.headers.has('content-type')){
const matches = res.headers.get('content-type').match(/image\/([a-z0-9]+)/i);
if(matches && matches.length > 1)extension = matches[1];
}
if(!extension && res.headers.has('content-disposition')){
const matches = res.headers.get('content-disposition').match(/filename\*?=['"]?(?:UTF-\d['"]*)?([^;\r\n"']*)['"]?;?/i);
if(matches && matches.length > 1)extension = matches[1].split('.').pop();
}
if(!extension){
throw new Error(`No valid image found: ${res.headers.get('content-type')} / ${res.headers.get('content-disposition')}`);
}
const location = `${config.dataFolder}/icon.${extension}`;
const buffer = await res.arrayBuffer();
await writeFile(location, new Uint8Array(buffer));
console.log(`Icon downloaded: ${location}`);
ICON_LOCATION = location;
return location;
}
export async function getLocation(){
return ICON_LOCATION;
} |