FileSystem pulls the file and pulls from localStorage cache to checksum both versions

This commit is contained in:
2026-05-24 19:33:02 +02:00
parent 65c776d1a2
commit 68458be71f
4 changed files with 180 additions and 19 deletions
+34 -8
View File
@@ -6,15 +6,39 @@ export class FileSystem {
private _fs: Map<number, TreeNode>;
private _root: number;
private constructor() {
private constructor(fs: Map<number, TreeNode>) {
this._fs = fs;
this._root = this._fs.values().next().value!.inode;
if (!this._root) throw new Error('First entry in the filesystem map empty!');
}
public static async Init() {
const fsJson: JSON = await this._fetchFileSystem('/temp/path');
const checksum: any = MD5.hash(JSON.stringify(fsJson));
let fsJson: JSON = await this._fetchFileSystem('/src/lib/assets/fs/fs.json');
const fsChecksum = MD5.hash(JSON.stringify(fsJson));
const cache: string | null = localStorage.getItem('fs-json');
if (cache) {
const fsCacheChecksum = MD5.hash(cache);
console.log(fsChecksum);
console.log(fsCacheChecksum);
if (fsChecksum !== fsCacheChecksum) {
//calculate version control difference to preserve user made changes (do a merge)
}
fsJson = JSON.parse(cache);
}
localStorage.setItem('fs-json', JSON.stringify(fsJson));
const fs: Map<number, TreeNode> = this._jsonToNodeTable(fsJson);
// This produces a different checksum!
// Find a way to produce the same hash from Map and a fetched json file or cached stringified json
console.log(MD5.hash(JSON.stringify(Array.from(fs))));
FileSystem.#instance = new FileSystem(fs);
}
private static async _fetchFileSystem(path: string): Promise<JSON> {
@@ -38,10 +62,12 @@ export class FileSystem {
}
}
private async fetchFsJson(sig: string): Promise<JSON> {
const signature: string | null = localStorage.getItem('signature');
private _fsToJsonString(): string {}
if (signature !== sig || signature === null || localStorage.getItem('fs') === null) {
private static async fetchFsJson(): Promise<JSON> {
const checksum: string | null = localStorage.getItem('fs-checksum');
if (checksum !== sig || signature === null || localStorage.getItem('fs') === null) {
const fs: JSON = await this._fetchFileSystem('/src/lib/assets/fs/fs.json');
localStorage.setItem('signature', sig);
localStorage.setItem('fs', JSON.stringify(fs));
@@ -59,7 +85,7 @@ export class FileSystem {
}
}
private _jsonToNodeTable(data: JSON): Map<number, TreeNode> {
private static _jsonToNodeTable(data: JSON): Map<number, TreeNode> {
const FsTable: Map<number, TreeNode> = new Map<number, TreeNode>();
const entryList = Object.entries(data);
@@ -288,7 +314,7 @@ export class FileSystem {
public static get instance(): FileSystem {
if (!FileSystem.#instance) {
FileSystem.#instance = new FileSystem();
throw new Error(`FileSystem was not initialized properly!`);
}
return FileSystem.instance;