1 | import FileSystem from "./fs/index.js";
|
2 | import * as cidLog from "./common/cid-log.js";
|
3 | import * as debug from "./common/debug.js";
|
4 | import * as dataRoot from "./data-root.js";
|
5 | import * as ucan from "./ucan/internal.js";
|
6 | import { Branch } from "./path.js";
|
7 | import { authenticatedUsername } from "./common/index.js";
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | export async function loadFileSystem(permissions, username, rootKey) {
|
20 | let cid, fs;
|
21 |
|
22 | username = username || (await authenticatedUsername() || undefined);
|
23 | if (!username)
|
24 | throw new Error("User hasn't authenticated yet");
|
25 |
|
26 | await ucan.store([]);
|
27 |
|
28 | const dataCid = navigator.onLine ? await dataRoot.lookup(username) : null;
|
29 | const [logIdx, logLength] = dataCid ? await cidLog.index(dataCid) : [-1, 0];
|
30 | if (!navigator.onLine) {
|
31 |
|
32 | cid = await cidLog.newest();
|
33 | }
|
34 | else if (!dataCid) {
|
35 |
|
36 | cid = await cidLog.newest();
|
37 | if (cid)
|
38 | debug.log("📓 No DNSLink, using local CID:", cid);
|
39 | else
|
40 | debug.log("📓 Creating a new file system");
|
41 | }
|
42 | else if (logIdx === 0) {
|
43 |
|
44 | cid = dataCid;
|
45 | debug.log("📓 DNSLink is up to date:", cid);
|
46 | }
|
47 | else if (logIdx > 0) {
|
48 |
|
49 | cid = await cidLog.newest();
|
50 | const idxLog = logIdx === 1 ? "1 newer local entry" : logIdx + " newer local entries";
|
51 | debug.log("📓 DNSLink is outdated (" + idxLog + "), using local CID:", cid);
|
52 | }
|
53 | else {
|
54 |
|
55 | cid = dataCid;
|
56 | await cidLog.add(cid);
|
57 | debug.log("📓 DNSLink is newer:", cid);
|
58 | }
|
59 |
|
60 | const p = permissions || undefined;
|
61 | fs = cid ? await FileSystem.fromCID(cid, { permissions: p }) : null;
|
62 | if (fs)
|
63 | return fs;
|
64 |
|
65 | if (!rootKey)
|
66 | throw new Error("Can't make new filesystem without a root AES key");
|
67 | fs = await FileSystem.empty({ permissions: p, rootKey });
|
68 | await addSampleData(fs);
|
69 |
|
70 | return fs;
|
71 | }
|
72 |
|
73 | async function addSampleData(fs) {
|
74 | await fs.mkdir({ directory: [Branch.Private, "Apps"] });
|
75 | await fs.mkdir({ directory: [Branch.Private, "Audio"] });
|
76 | await fs.mkdir({ directory: [Branch.Private, "Documents"] });
|
77 | await fs.mkdir({ directory: [Branch.Private, "Photos"] });
|
78 | await fs.mkdir({ directory: [Branch.Private, "Video"] });
|
79 | await fs.publish();
|
80 | }
|
81 |
|
\ | No newline at end of file |