Optimized checkConflict, more type improvs

This commit is contained in:
Kloon ImKloon 2023-09-12 15:40:49 +02:00
parent 5d4a4fc485
commit 45ccec72d3
No known key found for this signature in database
GPG Key ID: CCF1C86A995C5B6A
6 changed files with 35 additions and 43 deletions

View File

@ -8,7 +8,7 @@ export async function fetch(url: string) {
const res = await fetchURL(`/api/resources${url}`, {}); const res = await fetchURL(`/api/resources${url}`, {});
const data = await res.json(); const data = (await res.json()) as Resource;
data.url = `/files${url}`; data.url = `/files${url}`;
if (data.isDir) { if (data.isDir) {

View File

@ -13,7 +13,7 @@ export default async function search(base: string, query: string) {
let data = await res.json(); let data = await res.json();
data = data.map((item: Item) => { data = data.map((item: UploadItem) => {
item.url = `/files${base}` + url.encodePath(item.path); item.url = `/files${base}` + url.encodePath(item.path);
if (item.dir) { if (item.dir) {

View File

@ -87,7 +87,7 @@ export const useUploadStore = defineStore("upload", {
this.sizes = []; this.sizes = [];
this.progress = []; this.progress = [];
}, },
addJob(item: Item) { addJob(item: UploadItem) {
this.queue.push(item); this.queue.push(item);
this.sizes[this.id] = item.file.size; this.sizes[this.id] = item.file.size;
this.id++; this.id++;
@ -102,7 +102,7 @@ export const useUploadStore = defineStore("upload", {
// Vue.delete(this.uploads, id); // Vue.delete(this.uploads, id);
delete this.uploads[id]; delete this.uploads[id];
}, },
upload(item: Item) { upload(item: UploadItem) {
const uploadsCount = Object.keys(this.uploads).length; const uploadsCount = Object.keys(this.uploads).length;
const isQueueEmpty = this.queue.length == 0; const isQueueEmpty = this.queue.length == 0;
@ -116,7 +116,7 @@ export const useUploadStore = defineStore("upload", {
this.addJob(item); this.addJob(item);
this.processUploads(); this.processUploads();
}, },
finishUpload(item: Item) { finishUpload(item: UploadItem) {
this.setProgress({ id: item.id, loaded: item.file.size > 0 }); this.setProgress({ id: item.id, loaded: item.file.size > 0 });
this.removeJob(item.id); this.removeJob(item.id);
this.processUploads(); this.processUploads();

View File

@ -34,23 +34,3 @@ type ResourceType =
| "text" | "text"
| "blob" | "blob"
| "textImmutable"; | "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;
}

19
frontend/src/types/upload.d.ts vendored Normal file
View File

@ -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;
}

View File

@ -1,14 +1,17 @@
import { useUploadStore } from "@/stores/upload"; import { useUploadStore } from "@/stores/upload";
import url from "@/utils/url"; import url from "@/utils/url";
export function checkConflict(files: Resource[], items: Item[]) { export function checkConflict(
if (typeof items === "undefined" || items === null) { files: ResourceItem[],
items = []; dest: ResourceItem[]
): boolean {
if (typeof dest === "undefined" || dest === null) {
dest = [];
} }
const folder_upload = files[0].fullPath !== undefined; const folder_upload = files[0].fullPath !== undefined;
let conflict = false; const names: string[] = [];
for (let i = 0; i < files.length; i++) { for (let i = 0; i < files.length; i++) {
const file = files[i]; const file = files[i];
let name = file.name; let name = file.name;
@ -20,23 +23,13 @@ export function checkConflict(files: Resource[], items: Item[]) {
} }
} }
const res = items.findIndex(function hasConflict(element) { names.push(name);
// @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;
}
} }
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) => { return new Promise((resolve) => {
let reading = 0; let reading = 0;
const contents: any[] = []; const contents: any[] = [];
@ -134,7 +127,7 @@ export function handleFiles(
path += "/"; path += "/";
} }
const item: Item = { const item: UploadItem = {
id, id,
path, path,
file, file,