2021-07-03 21:46:40 -07:00
|
|
|
import type { FSModule } from "browserfs/dist/node/core/FS";
|
2021-07-03 21:41:57 -07:00
|
|
|
import extensions from "components/system/Files/FileEntry/extensions";
|
2021-07-03 21:46:40 -07:00
|
|
|
import type { FileInfo } from "components/system/Files/FileEntry/useFileInfo";
|
|
|
|
|
import ini from "ini";
|
|
|
|
|
import { parseBuffer } from "music-metadata-browser";
|
|
|
|
|
import { IMAGE_FILE_EXTENSIONS, SHORTCUT_EXTENSION } from "utils/constants";
|
|
|
|
|
import { bufferToUrl } from "utils/functions";
|
2021-04-03 21:51:11 -07:00
|
|
|
|
2021-07-03 21:46:40 -07:00
|
|
|
const getIconByFileExtension = (extension: string): string =>
|
2021-06-26 23:39:01 -07:00
|
|
|
`/icons/${extensions[extension]?.icon || "unknown"}.png`;
|
2021-06-26 22:01:45 -07:00
|
|
|
|
2021-07-03 21:46:40 -07:00
|
|
|
const getProcessByFileExtension = (extension: string): string => {
|
2021-06-26 23:39:01 -07:00
|
|
|
const [defaultProcess = ""] = extensions[extension]?.process || [];
|
2021-06-26 22:01:45 -07:00
|
|
|
|
|
|
|
|
return defaultProcess;
|
2021-04-04 00:14:27 -07:00
|
|
|
};
|
2021-07-03 21:46:40 -07:00
|
|
|
|
2021-07-03 21:52:14 -07:00
|
|
|
const getShortcutInfo = (contents: Buffer) => {
|
|
|
|
|
const {
|
|
|
|
|
InternetShortcut: { BaseURL: pid = "", IconFile: icon = "", URL: url = "" },
|
|
|
|
|
} = ini.parse(contents.toString());
|
|
|
|
|
|
|
|
|
|
return { icon, pid, url };
|
|
|
|
|
};
|
|
|
|
|
|
2021-07-03 21:46:40 -07:00
|
|
|
export const getInfoWithoutExtension = (
|
|
|
|
|
fs: FSModule,
|
|
|
|
|
path: string,
|
|
|
|
|
callback: React.Dispatch<React.SetStateAction<FileInfo>>
|
|
|
|
|
): void =>
|
|
|
|
|
fs.stat(path, (_error, stats) => {
|
|
|
|
|
const isDirectory = stats ? stats.isDirectory() : false;
|
|
|
|
|
|
|
|
|
|
callback({
|
|
|
|
|
icon: `/icons/${isDirectory ? "folder.png" : "unknown.png"}`,
|
|
|
|
|
pid: isDirectory ? "FileExplorer" : "",
|
|
|
|
|
url: path,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const getInfoWithExtension = (
|
|
|
|
|
fs: FSModule,
|
|
|
|
|
path: string,
|
|
|
|
|
extension: string,
|
|
|
|
|
callback: React.Dispatch<React.SetStateAction<FileInfo>>
|
|
|
|
|
): void => {
|
|
|
|
|
const getInfoByFileExtension = (icon?: string) =>
|
|
|
|
|
callback({
|
|
|
|
|
icon: icon || getIconByFileExtension(extension),
|
|
|
|
|
pid: getProcessByFileExtension(extension),
|
|
|
|
|
url: path,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (extension === SHORTCUT_EXTENSION) {
|
|
|
|
|
fs.readFile(path, (error, contents = Buffer.from("")) => {
|
|
|
|
|
if (error) {
|
|
|
|
|
getInfoByFileExtension();
|
|
|
|
|
} else {
|
2021-07-03 21:52:14 -07:00
|
|
|
callback(getShortcutInfo(contents));
|
2021-07-03 21:46:40 -07:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else if (IMAGE_FILE_EXTENSIONS.has(extension)) {
|
|
|
|
|
fs.readFile(path, (error, contents = Buffer.from("")) =>
|
|
|
|
|
getInfoByFileExtension(error ? "/icons/photo.png" : bufferToUrl(contents))
|
|
|
|
|
);
|
|
|
|
|
} else if (extension === ".mp3") {
|
|
|
|
|
fs.readFile(path, (error, contents = Buffer.from("")) =>
|
|
|
|
|
parseBuffer(contents).then(
|
|
|
|
|
({ common: { picture: [picture] = [] } = {} }) =>
|
|
|
|
|
getInfoByFileExtension(
|
|
|
|
|
!error && picture ? bufferToUrl(picture.data) : "/icons/music.png"
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
getInfoByFileExtension();
|
|
|
|
|
}
|
|
|
|
|
};
|