🐛Fix file uploading of a big directory tree (#208)

This commit is contained in:
Anton Shepilov
2023-09-15 10:48:12 +02:00
committed by GitHub
parent efcb27b405
commit 07794bf654
3 changed files with 10 additions and 5 deletions
@@ -48,12 +48,14 @@ export const useDriveUpload = () => {
const filesPerParentId: { [key: string]: File[] } = {}; const filesPerParentId: { [key: string]: File[] } = {};
// Create all directories // Create all directories
console.debug("Start creating directories ...");
const createDirectories = async (tree: FileTreeObject['tree'], parentId: string) => { const createDirectories = async (tree: FileTreeObject['tree'], parentId: string) => {
for (const directory of Object.keys(tree)) { for (const directory of Object.keys(tree)) {
if (tree[directory] instanceof File) { if (tree[directory] instanceof File) {
if (!filesPerParentId[parentId]) filesPerParentId[parentId] = []; if (!filesPerParentId[parentId]) filesPerParentId[parentId] = [];
filesPerParentId[parentId].push(tree[directory] as File); filesPerParentId[parentId].push(tree[directory] as File);
} else { } else {
console.debug(`Create directory ${directory}`);
const driveItem = await create( const driveItem = await create(
{ {
company_id: context.companyId, company_id: context.companyId,
@@ -63,6 +65,7 @@ export const useDriveUpload = () => {
}, },
{}, {},
); );
console.debug(`Directory ${directory} created`);
if (driveItem?.id) { if (driveItem?.id) {
await createDirectories(tree[directory] as FileTreeObject['tree'], driveItem.id); await createDirectories(tree[directory] as FileTreeObject['tree'], driveItem.id);
} else { } else {
@@ -72,10 +75,11 @@ export const useDriveUpload = () => {
} }
}; };
await createDirectories(tree.tree, context.parentId); await createDirectories(tree.tree, context.parentId);
console.debug("All directories created");
// Upload files into directories // Upload files into directories
for (const parentId of Object.keys(filesPerParentId)) { for (const parentId of Object.keys(filesPerParentId)) {
FileUploadService.upload(filesPerParentId[parentId], { await FileUploadService.upload(filesPerParentId[parentId], {
context: { context: {
companyId: context.companyId, companyId: context.companyId,
parentId: parentId, parentId: parentId,
@@ -56,8 +56,8 @@ class FileUploadService {
this.currentTaskId = uuid(); this.currentTaskId = uuid();
} }
fileList.forEach(async file => { for (const file of fileList) {
if (!file) return; if (!file) continue;
const pendingFile: PendingFileType = { const pendingFile: PendingFileType = {
id: uuid(), id: uuid(),
@@ -140,7 +140,7 @@ class FileUploadService {
options?.callback?.(null, options?.context || {}); options?.callback?.(null, options?.context || {});
this.notify(); this.notify();
}); });
}); }
return this.pendingFiles.filter(f => f.uploadTaskId === this.currentTaskId); return this.pendingFiles.filter(f => f.uploadTaskId === this.currentTaskId);
} }
@@ -258,6 +258,7 @@ class FileUploadService {
}); });
} }
public getDownloadRoute({ companyId, fileId }: { companyId: string; fileId: string }): string { public getDownloadRoute({ companyId, fileId }: { companyId: string; fileId: string }): string {
return FileUploadAPIClient.getDownloadRoute({ return FileUploadAPIClient.getDownloadRoute({
companyId: companyId, companyId: companyId,
@@ -156,7 +156,7 @@ export default memo(
if (dataTransfer) { if (dataTransfer) {
const tree = await getFilesTree(dataTransfer); const tree = await getFilesTree(dataTransfer);
setCreationModalState({ parent_id: '', open: false }); setCreationModalState({ parent_id: '', open: false });
uploadTree(tree, { await uploadTree(tree, {
companyId, companyId,
parentId, parentId,
}); });