Optimized checkConflict, more type improvs
This commit is contained in:
parent
5d4a4fc485
commit
45ccec72d3
@ -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) {
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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();
|
||||
|
||||
20
frontend/src/types/file.d.ts
vendored
20
frontend/src/types/file.d.ts
vendored
@ -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;
|
||||
}
|
||||
|
||||
19
frontend/src/types/upload.d.ts
vendored
Normal file
19
frontend/src/types/upload.d.ts
vendored
Normal 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;
|
||||
}
|
||||
@ -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,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user