Skip to content

Commit

Permalink
🐛Fix file uploading of a big directory tree (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
shepilov authored Sep 15, 2023
1 parent efcb27b commit 07794bf
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ export const useDriveUpload = () => {
const filesPerParentId: { [key: string]: File[] } = {};

// Create all directories
console.debug("Start creating directories ...");
const createDirectories = async (tree: FileTreeObject['tree'], parentId: string) => {
for (const directory of Object.keys(tree)) {
if (tree[directory] instanceof File) {
if (!filesPerParentId[parentId]) filesPerParentId[parentId] = [];
filesPerParentId[parentId].push(tree[directory] as File);
} else {
console.debug(`Create directory ${directory}`);
const driveItem = await create(
{
company_id: context.companyId,
Expand All @@ -63,6 +65,7 @@ export const useDriveUpload = () => {
},
{},
);
console.debug(`Directory ${directory} created`);
if (driveItem?.id) {
await createDirectories(tree[directory] as FileTreeObject['tree'], driveItem.id);
} else {
Expand All @@ -72,10 +75,11 @@ export const useDriveUpload = () => {
}
};
await createDirectories(tree.tree, context.parentId);
console.debug("All directories created");

// Upload files into directories
for (const parentId of Object.keys(filesPerParentId)) {
FileUploadService.upload(filesPerParentId[parentId], {
await FileUploadService.upload(filesPerParentId[parentId], {
context: {
companyId: context.companyId,
parentId: parentId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ class FileUploadService {
this.currentTaskId = uuid();
}

fileList.forEach(async file => {
if (!file) return;
for (const file of fileList) {
if (!file) continue;

const pendingFile: PendingFileType = {
id: uuid(),
Expand Down Expand Up @@ -140,7 +140,7 @@ class FileUploadService {
options?.callback?.(null, options?.context || {});
this.notify();
});
});
}

return this.pendingFiles.filter(f => f.uploadTaskId === this.currentTaskId);
}
Expand Down Expand Up @@ -258,6 +258,7 @@ class FileUploadService {
});
}


public getDownloadRoute({ companyId, fileId }: { companyId: string; fileId: string }): string {
return FileUploadAPIClient.getDownloadRoute({
companyId: companyId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export default memo(
if (dataTransfer) {
const tree = await getFilesTree(dataTransfer);
setCreationModalState({ parent_id: '', open: false });
uploadTree(tree, {
await uploadTree(tree, {
companyId,
parentId,
});
Expand Down

0 comments on commit 07794bf

Please sign in to comment.