Spaces:
Sleeping
Sleeping
File size: 1,817 Bytes
7aec436 |
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
import assetCache from './cache';
import request from '../../common/request';
import {readAsURL, readAsArrayBuffer} from '../../common/readers';
import defaultIcon from '../images/default-icon.png';
class WebAdapter {
getCachedAsset (asset) {
return assetCache.get(asset)
}
async cacheAsset (asset, result) {
await assetCache.set(asset, result);
}
getAppIcon (file) {
if (!file) {
return request({
url: defaultIcon,
type: 'arraybuffer'
});
}
// Convert to PNG
if (file.type === 'image/png') {
return readAsArrayBuffer(file);
}
return new Promise((resolve, reject) => {
const url = URL.createObjectURL(file);
const image = new Image();
image.onload = () => {
image.onload = null;
image.onerror = null;
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
if (!ctx) {
reject(new Error('Cannot get rendering context for icon conversion'));
return;
}
canvas.width = image.width;
canvas.height = image.height;
ctx.drawImage(image, 0, 0);
canvas.toBlob((blob) => {
URL.revokeObjectURL(url);
resolve(readAsArrayBuffer(blob));
});
};
image.onerror = () => {
image.onload = null;
image.onerror = null;
reject(new Error('Cannot load icon'));
};
image.src = url;
});
}
readAsURL (file, debugInfo) {
return readAsURL(file)
.catch((err) => {
throw new Error(`${debugInfo}: ${err}`);
});
}
fetchExtensionScript (url) {
return request({
type: 'text',
url: url
});
}
}
export default WebAdapter;
|