Initialize VUE project with WS connection, main pages, and various small features
This commit is contained in:
87
cista-front/src/repositories/Document.ts
Normal file
87
cista-front/src/repositories/Document.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
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;
|
||||
};
|
||||
|
||||
export type FolderDocument = BaseDocument & {
|
||||
size: number;
|
||||
modified: string;
|
||||
type: 'folder';
|
||||
};
|
||||
|
||||
export type FileDocument = BaseDocument & {
|
||||
type: 'file';
|
||||
ext: string;
|
||||
data: string;
|
||||
};
|
||||
|
||||
export type Document = FolderDocument | FileDocument;
|
||||
|
||||
|
||||
export const url_document_watch_ws = import.meta.env.VITE_URL_DOCUMENT_WATCH_WS
|
||||
export const url_document_upload_ws = import.meta.env.VITE_URL_DOCUMENT_UPLOAD_WS
|
||||
export const url_document_get = import.meta.env.VITE_URL_DOCUMENT_GET
|
||||
|
||||
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<FileDocument>{
|
||||
const file = await Client.get(path)
|
||||
const name = path.substring(1 , path.length)
|
||||
return {
|
||||
name,
|
||||
data: file.data,
|
||||
type: 'file',
|
||||
ext: getFileExtension(name)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user