From 24bf069f49e65b518c274e321090897f254e8690 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 6 Sep 2022 15:02:13 +0200 Subject: [PATCH] Whoever created chrome has a few chromies too many Closes #28 --- .../components/UploadDialog/UploadField.vue | 74 ++++++++++++++----- 1 file changed, 54 insertions(+), 20 deletions(-) diff --git a/frontend/src/components/UploadDialog/UploadField.vue b/frontend/src/components/UploadDialog/UploadField.vue index 96432c4..46387d0 100644 --- a/frontend/src/components/UploadDialog/UploadField.vue +++ b/frontend/src/components/UploadDialog/UploadField.vue @@ -78,53 +78,87 @@ const asyncReadEntries = async ( const getFile = async (entry: FileSystemEntry): Promise => new Promise((resolve, reject) => entry.file(resolve, reject)); +interface Tree { + name: string; + children: Promise[]; +} +interface TreeFile { + file: Promise; + fullName: string; +} + async function processDirOrFile( - entry: FileSystemEntry, - parent: number, - token: string -) { + entry: FileSystemEntry +): Promise { if (entry.isDirectory) { - const resp = await FS.create_folder(token, parent, entry.name); - if (isErrorResponse(resp)) return; - if ('exists' in resp && resp.isFile) return; + const tree: Tree = { + name: entry.name, + children: [] + }; const reader = entry.createReader(); let entries = []; do { try { entries = await asyncReadEntries(reader); - entries.forEach((e) => processDirOrFile(e, resp.id, token)); + tree.children.push(...entries.map((e) => processDirOrFile(e))); } catch { break; } } while (entries.length != 0); + return tree; } else + return { + file: getFile(entry), + fullName: entry.fullPath.slice(1) + }; +} + +async function processTree( + tree: Tree | TreeFile, + parent: number, + token: string +) { + if ('file' in tree) { files.value.push({ parent: parent, - fullName: entry.fullPath.slice(1), - file: await getFile(entry) + fullName: tree.fullName, + file: await tree.file }); + } else { + const resp = await FS.create_folder(token, parent, tree.name); + if (isErrorResponse(resp)) return; + if ('exists' in resp && resp.isFile) return; + await Promise.all( + tree.children.map(async (child) => + processTree(await child, resp.id, token) + ) + ); + } } const filesDropped = loadingMsgWrapper(message, async (event: DragEvent) => { stopDrag(); if (!event.dataTransfer) return; + //debugger; const token = await check_token(jwt); if (!token) return; + const items = event.dataTransfer.items; + const entries: FileSystemEntry[] = []; + for (let i = 0; i < items.length; i++) + entries.push(items[i].webkitGetAsEntry() as unknown as FileSystemEntry); + const trees: Array = await Promise.all( + entries.map((e) => processDirOrFile(e)) + ); files.value = []; - for (const file of event.dataTransfer.items) { - const entry = file.webkitGetAsEntry(); - if (entry) - await processDirOrFile( - entry as unknown as FileSystemEntry, - props.node.id, - token - ); - } + await Promise.all(trees.map((t) => processTree(t, props.node.id, token))); uploadFiles(); }); function uploadFiles() { - if (files.value.length == 0) return; + if (files.value.length == 0) { + emit('reloadNode'); + return; + } uploadDialogShow.value = true; }