import {Inject, Injectable} from '@angular/core'; import {BehaviorSubject} from "rxjs"; import {File} from "../../models/File"; import {invoke} from "@tauri-apps/api/tauri"; import {DOCUMENT} from "@angular/common"; @Injectable({ providedIn: 'root' }) export class FileService { displayedFiles = new BehaviorSubject([]); constructor(@Inject(DOCUMENT) private document: Document) { } public async getFiles() { let all_files = await invoke("get_all_files"); this.displayedFiles.next(all_files.slice(0, 50)); } public async readFile(hash: string, mime_type: string): Promise { const data = await invoke("read_file_by_hash", {hash}); const blob = new Blob([new Uint8Array(data)], {type: mime_type}); return new Promise((res, rej) => { const reader = new FileReader(); reader.onload = (e) => { const url = e.target?.result if (url === null) { res(undefined); } else { res(url as string) } }; reader.readAsDataURL(blob); }) } }