import type { FileStructure, DocumentStore } from '@/stores/documents' import { useDocumentStore } from '@/stores/documents' import { getFileExtension } from '@/utils' import Client from '@/repositories/Client' type BaseDocument = { name: string; key?: number | string; }; export type FolderDocument = BaseDocument & { type: 'folder' | 'folder-file'; size: number; sizedisp: string; mtime: number; modified: string; }; export type FileDocument = BaseDocument & { type: 'file'; ext: string; data: string; }; export type Document = FolderDocument | FileDocument; export const url_document_watch_ws = '/api/watch' export const url_document_upload_ws = '/api/upload' export const url_document_get ='/files' export class DocumentHandler { constructor( private store: DocumentStore = useDocumentStore() ) { this.handleWebSocketMessage = this.handleWebSocketMessage.bind(this); } handleWebSocketMessage(event: MessageEvent) { const msg = JSON.parse(event.data); switch (true) { case !!msg.root: this.handleRootMessage(msg); break; case !!msg.update: this.handleUpdateMessage(msg); break; default: } } private handleRootMessage({ root }: { root: FileStructure }) { if (this.store && this.store.root) this.store.root = root; } private handleUpdateMessage(updateData: { update: FileStructure[] }) { const root = updateData.update[0] if(root) this.store.root = root } } export class DocumentUploadHandler { constructor( private store: DocumentStore = useDocumentStore() ) { this.handleWebSocketMessage = this.handleWebSocketMessage.bind(this); } handleWebSocketMessage(event: MessageEvent) { const msg = JSON.parse(event.data); switch (true) { case !!msg.written: this.handleWrittenMessage(msg); break; default: } } private handleWrittenMessage(msg : { written : number}) { // if (this.store && this.store.root) this.store.root = root; console.log('Written message', msg.written) } } export async function fetchFile(path: string): Promise{ const file = await Client.get(url_document_get + path) const name = path.substring(1 , path.length) return { name, data: file.data, type: 'file', ext: getFileExtension(name) } }