Something went wrong. Try again.
A self-hosted web platform for applications & services with design based on Google's Material 3 Expressive. (Work In Progress)
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236import path from "path-browserify";import trpc from "./trpc";
export type UniformResourceLocator = `local:${string}` | `remote:${string}`;
class FilesystemInterface { createFile(url: UniformResourceLocator) { const parsedUrl = this.urlToPath(url);
if (parsedUrl.type === "invalid") { return false; }
if (parsedUrl.type === "local") { // Do some electron things return true; } else { // Call tRPC to perform the required actions return true; } }
async createDirectory(url: UniformResourceLocator) { const parsedUrl = this.urlToPath(url);
if (parsedUrl.type === "invalid") { return false; }
if (parsedUrl.type === "local") { // Do some electron things return true; } else { const createDirectoryResponse = await trpc.create.directory.mutate({ path: parsedUrl.path });
return { status: createDirectoryResponse.status, }; } }
async readDirectory(url: UniformResourceLocator) { const parsedUrl = this.urlToPath(url);
if (parsedUrl.type === "invalid") { return { status: "invalid_path" as const }; }
if (parsedUrl.type === "local") { // @ts-ignore const readDirectoryResponse = await window.electronAPI.fs.readdir(parsedUrl.path);
if (readDirectoryResponse.status === "ok") { return { status: readDirectoryResponse.status, items: readDirectoryResponse.items.map((newItem: string) => { return `${parsedUrl.type}:${path.join(parsedUrl.path, newItem)}` as UniformResourceLocator; }), }; } else { return { status: readDirectoryResponse.status }; } } else { const readDirectoryResponse = await trpc.readDirectory.query({ path: parsedUrl.path });
if (readDirectoryResponse.status === "ok") { return { status: readDirectoryResponse.status, items: readDirectoryResponse.items.map((newItem) => { return `${parsedUrl.type}:${path.join(parsedUrl.path, newItem)}` as UniformResourceLocator; }), }; } else { return { status: readDirectoryResponse.status }; } } }
getViewEntryBatchSize(url: UniformResourceLocator) { const parsedUrl = this.urlToPath(url);
if (parsedUrl.type === "invalid") { return 0; }
if (parsedUrl.type === "local") { return 4; } else { return 10; } }
async getViewEntry(url: UniformResourceLocator, thumbnailSize?: number) { const parsedUrl = this.urlToPath(url);
if (parsedUrl.type === "invalid") { return { status: "invalid_path" as const }; }
if (parsedUrl.type === "local") { // @ts-ignore const getEntryResponse = await window.electronAPI.files.get_entry(parsedUrl.path, thumbnailSize);
if (getEntryResponse.status === "ok") { let thumbnail: string | undefined;
if (getEntryResponse.data.thumbnail) { // @ts-ignore const blob = new Blob([getEntryResponse.data.thumbnail]);
thumbnail = URL.createObjectURL(blob); }
return { status: getEntryResponse.status, data: { ...getEntryResponse.data, path: `${parsedUrl.type}:${getEntryResponse.data.path}`, thumbnail: thumbnail, }, }; } else { return { status: getEntryResponse.status }; } } else { const getEntryResponse = await trpc.view.getEntry.query({ path: parsedUrl.path, thumbnailSize: thumbnailSize });
if (getEntryResponse.status === "ok") { return { status: getEntryResponse.status, data: { ...getEntryResponse.data, path: `${parsedUrl.type}:${getEntryResponse.data.path}`, }, }; } else { return { status: getEntryResponse.status }; } } }
async getQuota(location: "local" | "remote") { if (location === "local") return { maximum: -1, currentUsage: -1 };
const quota = await trpc.quota.query();
return quota; }
async openInDefaultApplication(url: UniformResourceLocator) { const parsedUrl = this.urlToPath(url);
if (parsedUrl.type === "invalid") { return { status: "invalid_path" as const }; }
if (parsedUrl.type === "local") { // @ts-ignore const openInDefaultApplicationResponse = await window.electronAPI.files.open_in_default_application(parsedUrl.path);
if (openInDefaultApplicationResponse.status === "ok") { return { status: openInDefaultApplicationResponse.status }; } else { return { status: openInDefaultApplicationResponse.status }; } } else { alert("Cannot open remote files as no applications exist to open them yet."); } }
async getPreviewDialogMetadata(url: UniformResourceLocator) { const parsedUrl = this.urlToPath(url);
if (parsedUrl.type === "invalid") { return { status: "invalid_path" as const }; }
if (parsedUrl.type === "local") { alert("Cannot open the preview dialog for local files currently."); return { status: "invalid_operation" as const }; } else { const getEntryResponse = await trpc.previewDialog.get.query({ path: parsedUrl.path });
if (getEntryResponse.status === "ok") { return getEntryResponse; } else { return { status: getEntryResponse.status }; } } }
joinUrls(...urls: UniformResourceLocator[]) { const firstUrlType = this.urlToPath(urls[0]).type;
const paths: string[] = [];
for (const url of urls) { const parsedUrl = this.urlToPath(url);
if (parsedUrl.type === "invalid") { throw new Error(`Cannot join invalid fs URLs, \n${urls.join("\n")}`); }
if (parsedUrl.type !== firstUrlType) { throw new Error(`Cannot join fs URLs which are of a different type, \n${urls.join("\n")}`); }
paths.push(parsedUrl.path); }
return `${firstUrlType}:${path.join(...paths)}`; }
urlToPath(url: UniformResourceLocator): { type: "remote" | "local"; path: string } | { type: "invalid" } { if (url.startsWith("local:")) { return { type: "local", path: url.substring(6), }; } else if (url.startsWith("remote:")) { return { type: "remote", path: url.substring(7), }; }
return { type: "invalid", }; }}
const filesystemInterface = new FilesystemInterface();
export default filesystemInterface;