2023-06-03 05:27:08 +03:00
|
|
|
// class to load 7zip wasm module
|
|
|
|
// and extract files from archive
|
|
|
|
|
2023-06-13 01:58:19 +03:00
|
|
|
import * as Comlink from "comlink";
|
|
|
|
// @ts-expect-error typescript can't find it when query it with ?worker
|
|
|
|
import SevenZipWorker from "./worker/7zip-manager?worker";
|
|
|
|
import { SevenZipManager, iFile } from "./worker/7zip-manager";
|
2023-06-03 05:27:08 +03:00
|
|
|
|
|
|
|
export class FilesManager {
|
|
|
|
consoleOutputBuffer: string[] = [];
|
2023-06-03 06:53:03 +03:00
|
|
|
path: Ref<string> = useSelectedPath();
|
2023-06-13 01:58:19 +03:00
|
|
|
remoteSevenZipManager?: Comlink.Remote<SevenZipManager>;
|
2023-06-03 05:27:08 +03:00
|
|
|
|
2023-06-13 01:58:19 +03:00
|
|
|
constructor(private filesList: Ref<iFile[]>) {
|
2023-06-03 05:27:08 +03:00
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
2023-06-13 01:58:19 +03:00
|
|
|
this.remoteSevenZipManager = await new (Comlink.wrap(new SevenZipWorker()) as any);
|
2023-06-03 05:27:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async loadArchive(file: File) {
|
2023-06-13 01:58:19 +03:00
|
|
|
if (!this.remoteSevenZipManager) return;
|
|
|
|
this.filesList.value = await this.remoteSevenZipManager.loadArchive(file) || [];
|
2023-06-03 05:27:08 +03:00
|
|
|
}
|
|
|
|
|
2023-06-13 01:58:19 +03:00
|
|
|
getFile(path: string, innerList: iFile[] | undefined = undefined): any {
|
2023-06-03 05:53:52 +03:00
|
|
|
if (path == "/") {
|
|
|
|
return {
|
2023-07-21 17:16:59 +03:00
|
|
|
content: this.filesList.value.sort((a: iFile, b: iFile) => {
|
2023-06-03 06:38:49 +03:00
|
|
|
// sort by folder and from a to z
|
|
|
|
if (a.isFolder && !b.isFolder) return -1;
|
|
|
|
if (!a.isFolder && b.isFolder) return 1;
|
|
|
|
if (a.name < b.name) return -1;
|
|
|
|
if (a.name > b.name) return 1;
|
|
|
|
return 0;
|
|
|
|
}),
|
2023-06-06 03:59:48 +03:00
|
|
|
isFolder: this.filesList.value.length ? true : false,
|
2023-06-03 05:53:52 +03:00
|
|
|
};
|
2023-06-03 05:27:08 +03:00
|
|
|
}
|
2023-06-03 05:53:52 +03:00
|
|
|
|
|
|
|
for (const file of (innerList || this.filesList.value)) {
|
|
|
|
if (file.path == path) {
|
2023-07-21 17:16:59 +03:00
|
|
|
file.content = file.content?.sort((a: iFile, b: iFile) => {
|
2023-06-03 06:38:49 +03:00
|
|
|
// sort by folder and from a to z
|
|
|
|
if (a.isFolder && !b.isFolder) return -1;
|
|
|
|
if (!a.isFolder && b.isFolder) return 1;
|
|
|
|
if (a.name < b.name) return -1;
|
|
|
|
if (a.name > b.name) return 1;
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
|
2023-06-03 05:53:52 +03:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file.isFolder && path.includes(file.path)) {
|
2023-06-03 06:59:17 +03:00
|
|
|
const recursiveFile = this.getFile(path, file.content);
|
2023-06-03 05:53:52 +03:00
|
|
|
if (recursiveFile) {
|
|
|
|
return recursiveFile;
|
|
|
|
}
|
|
|
|
}
|
2023-06-03 05:27:08 +03:00
|
|
|
}
|
2023-06-03 05:53:52 +03:00
|
|
|
|
|
|
|
return undefined;
|
2023-06-03 05:27:08 +03:00
|
|
|
}
|
2023-07-21 17:16:59 +03:00
|
|
|
|
|
|
|
async getFileBlobUrl(path: string) {
|
|
|
|
if (!this.remoteSevenZipManager) return;
|
|
|
|
return await this.remoteSevenZipManager.generateBlobUrl(JSON.stringify(this.getFile(path)) as any);
|
|
|
|
}
|
2023-06-03 05:27:08 +03:00
|
|
|
}
|