forked from DustinBrett/daedalOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreloadIcons.js
More file actions
127 lines (103 loc) · 3.43 KB
/
preloadIcons.js
File metadata and controls
127 lines (103 loc) · 3.43 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const {
mkdirSync,
readdirSync,
readFileSync,
writeFileSync,
existsSync,
statSync,
} = require("fs");
const { extname, join } = require("path");
const { parse } = require("ini");
const PUBLIC_DIR = "public";
const HOME = "/Users/Public";
const DESKTOP_PATH = `${HOME}/Desktop`;
const START_MENU_PATH = `${HOME}/Start Menu`;
const ICON_PATH = "/System/Icons";
const SHORTCUT_ICON = `${ICON_PATH}/shortcut.webp`;
const NEW_FOLDER_ICON = `${ICON_PATH}/new_folder.webp`;
const USER_ICON_PATH = `${HOME}/Icons`;
const ICON_CACHE = `${USER_ICON_PATH}/Cache`;
const YT_ICON_CACHE = `${ICON_CACHE}/YouTube`;
const ICON_CACHE_EXTENSION = ".cache";
const VLC_SUBICON = "/System/Icons/16x16/vlc.webp";
const isYouTubeUrl = (url) =>
url.includes("youtube.com/") || url.includes("youtu.be/");
const getYouTubeUrlId = (url) => {
try {
const { pathname, searchParams } = new URL(url);
return searchParams.get("v") || pathname.split("/").pop() || "";
} catch {
// URL parsing failed
}
return "";
};
const getPublicDirectoryIcons = (directory) => [
...new Set(
readdirSync(join(PUBLIC_DIR, directory)).reduce((icons, file) => {
if (extname(file).toLowerCase() === ".url") {
const {
InternetShortcut: {
BaseURL: pid = "",
IconFile: icon = "",
URL: url = "",
},
} = parse(readFileSync(join(PUBLIC_DIR, directory, file)).toString());
const isVideo = pid === "VideoPlayer";
if (isVideo && url) icons.push(encodeURI(VLC_SUBICON));
if (icon) icons.push(encodeURI(icon));
else {
if (isVideo && isYouTubeUrl(url)) {
const iconFileName = `/${getYouTubeUrlId(
url
)}${ICON_CACHE_EXTENSION}`;
if (
existsSync(join(PUBLIC_DIR, YT_ICON_CACHE, `${iconFileName}`))
) {
icons.push(encodeURI(`${YT_ICON_CACHE}${iconFileName}`));
}
} else {
const iconPath = url || `${directory}/${file}`;
const iconCacheFileName = `${iconPath}${ICON_CACHE_EXTENSION}`;
if (
extname(iconPath) &&
existsSync(join(PUBLIC_DIR, ICON_CACHE, `${iconCacheFileName}`))
) {
icons.push(encodeURI(`${ICON_CACHE}${iconCacheFileName}`));
}
}
}
}
return icons;
}, [])
),
];
const getIniIcons = () => {
const iniIcons = {};
const rootPath = join(PUBLIC_DIR, HOME);
const readDirectory = (directory) =>
readdirSync(directory).forEach((entry) => {
const currentPath = join(directory, entry);
if (statSync(currentPath).isDirectory()) readDirectory(currentPath);
else if (entry === "desktop.ini") {
const {
ShellClassInfo: { IconFile = "" },
} = parse(readFileSync(currentPath).toString());
iniIcons[directory.replace(PUBLIC_DIR, "").replace(/\\/g, "/")] =
IconFile;
}
});
readDirectory(rootPath);
return iniIcons;
};
if (!existsSync(join(PUBLIC_DIR, ".index"))) {
mkdirSync(join(PUBLIC_DIR, ".index"));
}
writeFileSync(
"./public/.index/desktopIcons.json",
JSON.stringify([SHORTCUT_ICON, ...getPublicDirectoryIcons(DESKTOP_PATH)])
);
writeFileSync(
"./public/.index/startMenuIcons.json",
JSON.stringify([NEW_FOLDER_ICON, ...getPublicDirectoryIcons(START_MENU_PATH)])
);
writeFileSync("./public/.index/iniIcons.json", JSON.stringify(getIniIcons()));