Skip to content

Commit 63863fb

Browse files
committed
More dynamic fs's
1 parent 660957d commit 63863fb

File tree

4 files changed

+76
-60
lines changed

4 files changed

+76
-60
lines changed

contexts/fileSystem/useFileSystemContextState.ts

Lines changed: 63 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ import useAsyncFs from "contexts/fileSystem/useAsyncFs";
2929
import { useProcesses } from "contexts/process";
3030
import type { UpdateFiles } from "contexts/session/types";
3131
import { basename, dirname, isAbsolute, join } from "path";
32-
import * as BrowserFS from "public/System/BrowserFS/browserfs.min.js";
3332
import { useCallback, useEffect, useRef, useState } from "react";
3433
import {
3534
CLIPBOARD_FILE_EXTENSIONS,
3635
DEFAULT_MAPPED_NAME,
36+
DESKTOP_PATH,
3737
PROCESS_DELIMITER,
3838
TRANSITIONS_IN_MILLISECONDS,
3939
} from "utils/constants";
@@ -88,10 +88,6 @@ type FileSystemContextState = AsyncFS & {
8888

8989
const SYSTEM_DIRECTORIES = new Set(["/OPFS"]);
9090

91-
const {
92-
FileSystem: { Emscripten, FileSystemAccess },
93-
} = BrowserFS as IFileSystemAccess & typeof IBrowserFS;
94-
9591
const useFileSystemContextState = (): FileSystemContextState => {
9692
const asyncFs = useAsyncFs();
9793
const {
@@ -225,21 +221,28 @@ const useFileSystemContextState = (): FileSystemContextState => {
225221
const mountEmscriptenFs = useCallback(
226222
async (FS: EmscriptenFS, fsName?: string) =>
227223
new Promise<string>((resolve, reject) => {
228-
Emscripten?.Create({ FS }, (error, newFs) => {
229-
const emscriptenFS = newFs as unknown as ExtendedEmscriptenFileSystem;
224+
import("public/System/BrowserFS/extrafs.min.js").then((ExtraFS) => {
225+
const {
226+
FileSystem: { Emscripten },
227+
} = ExtraFS as typeof IBrowserFS;
230228

231-
if (error || !newFs || !emscriptenFS._FS?.DB_NAME) {
232-
reject();
233-
return;
234-
}
229+
Emscripten?.Create({ FS }, (error, newFs) => {
230+
const emscriptenFS =
231+
newFs as unknown as ExtendedEmscriptenFileSystem;
232+
233+
if (error || !newFs || !emscriptenFS._FS?.DB_NAME) {
234+
reject();
235+
return;
236+
}
235237

236-
const dbName =
237-
fsName ||
238-
`${emscriptenFS._FS?.DB_NAME().replace(/\/+$/, "")}${emscriptenFS
239-
._FS?.DB_STORE_NAME}`;
238+
const dbName =
239+
fsName ||
240+
`${emscriptenFS._FS?.DB_NAME().replace(/\/+$/, "")}${emscriptenFS
241+
._FS?.DB_STORE_NAME}`;
240242

241-
rootFs?.mount?.(join("/", dbName), newFs);
242-
resolve(dbName);
243+
rootFs?.mount?.(join("/", dbName), newFs);
244+
resolve(dbName);
245+
});
243246
});
244247
}),
245248
[rootFs]
@@ -265,24 +268,30 @@ const useFileSystemContextState = (): FileSystemContextState => {
265268

266269
return new Promise((resolve, reject) => {
267270
if (handle instanceof FileSystemDirectoryHandle) {
268-
FileSystemAccess?.Create({ handle }, (error, newFs) => {
269-
if (error || !newFs) {
270-
reject();
271-
return;
272-
}
271+
import("public/System/BrowserFS/extrafs.min.js").then((ExtraFS) => {
272+
const {
273+
FileSystem: { FileSystemAccess },
274+
} = ExtraFS as IFileSystemAccess;
275+
276+
FileSystemAccess?.Create({ handle }, (error, newFs) => {
277+
if (error || !newFs) {
278+
reject();
279+
return;
280+
}
273281

274-
const systemDirectory = SYSTEM_DIRECTORIES.has(directory);
275-
const mappedName =
276-
removeInvalidFilenameCharacters(handle.name).trim() ||
277-
(systemDirectory ? "" : DEFAULT_MAPPED_NAME);
282+
const systemDirectory = SYSTEM_DIRECTORIES.has(directory);
283+
const mappedName =
284+
removeInvalidFilenameCharacters(handle.name).trim() ||
285+
(systemDirectory ? "" : DEFAULT_MAPPED_NAME);
278286

279-
rootFs?.mount?.(join(directory, mappedName), newFs);
280-
resolve(systemDirectory ? directory : mappedName);
287+
rootFs?.mount?.(join(directory, mappedName), newFs);
288+
resolve(systemDirectory ? directory : mappedName);
281289

282-
import("contexts/fileSystem/functions").then(
283-
({ addFileSystemHandle }) =>
284-
addFileSystemHandle(directory, handle, mappedName)
285-
);
290+
import("contexts/fileSystem/functions").then(
291+
({ addFileSystemHandle }) =>
292+
addFileSystemHandle(directory, handle, mappedName)
293+
);
294+
});
286295
});
287296
} else {
288297
reject();
@@ -304,10 +313,10 @@ const useFileSystemContextState = (): FileSystemContextState => {
304313
}
305314
};
306315

307-
import("public/System/BrowserFS/iso_zip_fs.min.js").then((IsoZipFS) => {
316+
import("public/System/BrowserFS/extrafs.min.js").then((ExtraFS) => {
308317
const {
309318
FileSystem: { IsoFS, ZipFS },
310-
} = IsoZipFS as typeof IBrowserFS;
319+
} = ExtraFS as typeof IBrowserFS;
311320

312321
if (getExtension(url) === ".iso") {
313322
IsoFS?.Create({ data: fileData }, createFs);
@@ -507,27 +516,34 @@ const useFileSystemContextState = (): FileSystemContextState => {
507516
const restoreFsHandles = async (): Promise<void> => {
508517
restoredFsHandles.current = true;
509518

510-
Object.entries(await getFileSystemHandles()).forEach(
511-
async ([handleDirectory, handle]) => {
512-
if (!(await exists(handleDirectory))) {
513-
try {
514-
mapFs(
515-
SYSTEM_DIRECTORIES.has(handleDirectory)
519+
let mappedOntoDesktop = false;
520+
521+
await Promise.all(
522+
Object.entries(await getFileSystemHandles()).map(
523+
async ([handleDirectory, handle]) => {
524+
if (!(await exists(handleDirectory))) {
525+
try {
526+
const mapDirectory = SYSTEM_DIRECTORIES.has(handleDirectory)
516527
? handleDirectory
517-
: dirname(handleDirectory),
518-
handle
519-
);
520-
} catch {
521-
// Ignore failure
528+
: dirname(handleDirectory);
529+
530+
await mapFs(mapDirectory, handle);
531+
532+
if (mapDirectory === DESKTOP_PATH) mappedOntoDesktop = true;
533+
} catch {
534+
// Ignore failure
535+
}
522536
}
523537
}
524-
}
538+
)
525539
);
540+
541+
if (mappedOntoDesktop) updateFolder(DESKTOP_PATH);
526542
};
527543

528544
restoreFsHandles();
529545
}
530-
}, [exists, mapFs, rootFs]);
546+
}, [exists, mapFs, rootFs, updateFolder]);
531547

532548
return {
533549
addFile,

public/System/BrowserFS/browserfs.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/System/BrowserFS/extrafs.min.js

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/System/BrowserFS/iso_zip_fs.min.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)