forked from zxch3n/get-app-icon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
34 lines (31 loc) · 1.05 KB
/
index.ts
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
// @ts-ignore
import macIcon from "@todesktop/file-icon-prebuild";
let winIcon: any;
export async function extractIcon(path: string): Promise<string> {
if (process.platform === "darwin") {
const buffer: Buffer = await macIcon.buffer(path);
return "data:image/png;base64," + buffer.toString("base64");
} else if (process.platform === "win32") {
if (!winIcon) {
// @ts-ignore
winIcon = await import("icon-extractor");
}
return new Promise((resolve, reject) => {
let resolved = false;
winIcon.emitter.on("icon", function (data: any) {
if (data.Context === path) {
resolved = true;
resolve("data:image/png;base64," + data.Base64ImageData);
}
});
winIcon.getIcon(path, path);
setTimeout(() => {
if (!resolved) {
reject();
}
}, 3000);
});
} else {
throw new Error("Not implemented");
}
}