parent
0ec122226f
commit
24bf069f49
@ -78,53 +78,87 @@ const asyncReadEntries = async (
|
||||
const getFile = async (entry: FileSystemEntry): Promise<File> =>
|
||||
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(
|
||||
entry: FileSystemEntry,
|
||||
parent: number,
|
||||
token: string
|
||||
) {
|
||||
entry: FileSystemEntry
|
||||
): Promise<Tree | TreeFile> {
|
||||
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;
|
||||
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
|
||||
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 = [];
|
||||
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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user