Whoever created chrome has a few chromies too many

Closes #28
This commit is contained in:
Mutzi 2022-09-06 15:02:13 +02:00
parent 0ec122226f
commit 24bf069f49

View File

@ -78,53 +78,87 @@ const asyncReadEntries = async (
const getFile = async (entry: FileSystemEntry): Promise<File> => const getFile = async (entry: FileSystemEntry): Promise<File> =>
new Promise((resolve, reject) => entry.file(resolve, reject)); new Promise((resolve, reject) => entry.file(resolve, reject));
interface Tree {
name: string;
children: Promise<Tree | TreeFile>[];
}
interface TreeFile {
file: Promise<File>;
fullName: string;
}
async function processDirOrFile( async function processDirOrFile(
entry: FileSystemEntry, entry: FileSystemEntry
parent: number, ): Promise<Tree | TreeFile> {
token: string
) {
if (entry.isDirectory) { if (entry.isDirectory) {
const resp = await FS.create_folder(token, parent, entry.name); const tree: Tree = {
if (isErrorResponse(resp)) return; name: entry.name,
if ('exists' in resp && resp.isFile) return; children: []
};
const reader = entry.createReader(); const reader = entry.createReader();
let entries = []; let entries = [];
do { do {
try { try {
entries = await asyncReadEntries(reader); entries = await asyncReadEntries(reader);
entries.forEach((e) => processDirOrFile(e, resp.id, token)); tree.children.push(...entries.map((e) => processDirOrFile(e)));
} catch { } catch {
break; break;
} }
} while (entries.length != 0); } while (entries.length != 0);
return tree;
} else } 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({ files.value.push({
parent: parent, parent: parent,
fullName: entry.fullPath.slice(1), fullName: tree.fullName,
file: await getFile(entry) 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) => { const filesDropped = loadingMsgWrapper(message, async (event: DragEvent) => {
stopDrag(); stopDrag();
if (!event.dataTransfer) return; if (!event.dataTransfer) return;
//debugger;
const token = await check_token(jwt); const token = await check_token(jwt);
if (!token) return; 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<Tree | TreeFile> = await Promise.all(
entries.map((e) => processDirOrFile(e))
);
files.value = []; files.value = [];
for (const file of event.dataTransfer.items) { await Promise.all(trees.map((t) => processTree(t, props.node.id, token)));
const entry = file.webkitGetAsEntry();
if (entry)
await processDirOrFile(
entry as unknown as FileSystemEntry,
props.node.id,
token
);
}
uploadFiles(); uploadFiles();
}); });
function uploadFiles() { function uploadFiles() {
if (files.value.length == 0) return; if (files.value.length == 0) {
emit('reloadNode');
return;
}
uploadDialogShow.value = true; uploadDialogShow.value = true;
} }