From 45ccec72d3bc0164c05c063fbc31907fd5997078 Mon Sep 17 00:00:00 2001 From: Kloon ImKloon Date: Tue, 12 Sep 2023 15:40:49 +0200 Subject: [PATCH] Optimized checkConflict, more type improvs --- frontend/src/api/files.ts | 2 +- frontend/src/api/search.ts | 2 +- frontend/src/stores/upload.ts | 6 +++--- frontend/src/types/file.d.ts | 20 -------------------- frontend/src/types/upload.d.ts | 19 +++++++++++++++++++ frontend/src/utils/upload.ts | 29 +++++++++++------------------ 6 files changed, 35 insertions(+), 43 deletions(-) create mode 100644 frontend/src/types/upload.d.ts diff --git a/frontend/src/api/files.ts b/frontend/src/api/files.ts index c1ffca08..b0aec161 100644 --- a/frontend/src/api/files.ts +++ b/frontend/src/api/files.ts @@ -8,7 +8,7 @@ export async function fetch(url: string) { const res = await fetchURL(`/api/resources${url}`, {}); - const data = await res.json(); + const data = (await res.json()) as Resource; data.url = `/files${url}`; if (data.isDir) { diff --git a/frontend/src/api/search.ts b/frontend/src/api/search.ts index acbb122b..871f0aed 100644 --- a/frontend/src/api/search.ts +++ b/frontend/src/api/search.ts @@ -13,7 +13,7 @@ export default async function search(base: string, query: string) { let data = await res.json(); - data = data.map((item: Item) => { + data = data.map((item: UploadItem) => { item.url = `/files${base}` + url.encodePath(item.path); if (item.dir) { diff --git a/frontend/src/stores/upload.ts b/frontend/src/stores/upload.ts index a2364b35..52ae0466 100644 --- a/frontend/src/stores/upload.ts +++ b/frontend/src/stores/upload.ts @@ -87,7 +87,7 @@ export const useUploadStore = defineStore("upload", { this.sizes = []; this.progress = []; }, - addJob(item: Item) { + addJob(item: UploadItem) { this.queue.push(item); this.sizes[this.id] = item.file.size; this.id++; @@ -102,7 +102,7 @@ export const useUploadStore = defineStore("upload", { // Vue.delete(this.uploads, id); delete this.uploads[id]; }, - upload(item: Item) { + upload(item: UploadItem) { const uploadsCount = Object.keys(this.uploads).length; const isQueueEmpty = this.queue.length == 0; @@ -116,7 +116,7 @@ export const useUploadStore = defineStore("upload", { this.addJob(item); this.processUploads(); }, - finishUpload(item: Item) { + finishUpload(item: UploadItem) { this.setProgress({ id: item.id, loaded: item.file.size > 0 }); this.removeJob(item.id); this.processUploads(); diff --git a/frontend/src/types/file.d.ts b/frontend/src/types/file.d.ts index 7a66c50d..557f2260 100644 --- a/frontend/src/types/file.d.ts +++ b/frontend/src/types/file.d.ts @@ -34,23 +34,3 @@ type ResourceType = | "text" | "blob" | "textImmutable"; - -interface Uploads { - [key: string]: Upload; -} - -interface Upload { - id: number; - file: Resource; - type: string; -} - -interface Item { - id: number; - url?: string; - path: string; - file: Resource; - dir?: boolean; - overwrite?: boolean; - type?: ResourceType; -} diff --git a/frontend/src/types/upload.d.ts b/frontend/src/types/upload.d.ts new file mode 100644 index 00000000..b72ec624 --- /dev/null +++ b/frontend/src/types/upload.d.ts @@ -0,0 +1,19 @@ +interface Uploads { + [key: string]: Upload; +} + +interface Upload { + id: number; + file: Resource; + type: string; +} + +interface UploadItem { + id: number; + url?: string; + path: string; + file: Resource; + dir?: boolean; + overwrite?: boolean; + type?: ResourceType; +} diff --git a/frontend/src/utils/upload.ts b/frontend/src/utils/upload.ts index 7d1eddac..a222e071 100644 --- a/frontend/src/utils/upload.ts +++ b/frontend/src/utils/upload.ts @@ -1,14 +1,17 @@ import { useUploadStore } from "@/stores/upload"; import url from "@/utils/url"; -export function checkConflict(files: Resource[], items: Item[]) { - if (typeof items === "undefined" || items === null) { - items = []; +export function checkConflict( + files: ResourceItem[], + dest: ResourceItem[] +): boolean { + if (typeof dest === "undefined" || dest === null) { + dest = []; } const folder_upload = files[0].fullPath !== undefined; - let conflict = false; + const names: string[] = []; for (let i = 0; i < files.length; i++) { const file = files[i]; let name = file.name; @@ -20,23 +23,13 @@ export function checkConflict(files: Resource[], items: Item[]) { } } - const res = items.findIndex(function hasConflict(element) { - // @ts-ignore - console.log({ left: element.name, right: this }); - // @ts-ignore Don't know what this does - return element.name === this; - }, name); - - if (res >= 0) { - conflict = true; - break; - } + names.push(name); } - return conflict; + return dest.some((d) => names.includes(d.name)); } -export function scanFiles(dt: { [key: string]: any; item: Item }) { +export function scanFiles(dt: { [key: string]: any; item: ResourceItem }) { return new Promise((resolve) => { let reading = 0; const contents: any[] = []; @@ -134,7 +127,7 @@ export function handleFiles( path += "/"; } - const item: Item = { + const item: UploadItem = { id, path, file,