From d10c6da763723d336b7480006ad701526d630b21 Mon Sep 17 00:00:00 2001 From: vishal Date: Mon, 1 Jul 2024 00:37:06 +0530 Subject: [PATCH 1/3] fix: delete async await --- app/api/drive/files/route.ts | 36 ++++++++++++++++------------- app/api/drive/files/upload/route.ts | 19 +++++++++------ 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/app/api/drive/files/route.ts b/app/api/drive/files/route.ts index c41c7b0..3499459 100644 --- a/app/api/drive/files/route.ts +++ b/app/api/drive/files/route.ts @@ -66,23 +66,17 @@ export async function PUT(req: NextRequest) { await Promise.all( body?.ids?.map(async (file: string) => { const fl = await File.findById(file); - let link = null; + const Key = encodeURI(body?.parent ? `${body?.parent}/${fl?.name}` : fl?.name || ''); if (fl?.link) { const input = { Bucket: 'dream-by-vishal', - CopySource: `dream-by-vishal/${fl.link}`, - Key: encodeURI(`${body?.parent}/${fl?.name}`), + CopySource: `dream-by-vishal/${encodeURI(fl.link)}`, + Key, }; await s3Client.send(new CopyObjectCommand(input)); - await s3Client.send( - new DeleteObjectCommand({ Bucket: 'dream-by-vishal', Key: encodeURI(fl.link) }) - ); - link = encodeURI(`${body?.parent}/${fl?.name}`); + await s3Client.send(new DeleteObjectCommand({ Bucket: 'dream-by-vishal', Key: fl.link })); } - const input = { - parent: body?.parent, - link, - }; + const input = { parent: body?.parent || null, link: Key }; await fl?.updateOne(input); response = [...response, fl]; }) @@ -98,18 +92,26 @@ const deleteAllChilds = async (fl: any) => { await s3Client.send( new DeleteObjectCommand({ Bucket: 'dream-by-vishal', - Key: encodeURI(`${fl?.parent}/${fl?.name}`), + Key: fl?.link, }) ); + await fl?.deleteOne(); + return true; } - await fl?.deleteOne(); const shouldDelete = await File.find({ parent: fl?._id }); + await fl?.deleteOne(); await Promise.all( shouldDelete.map(async (item) => { await File.findByIdAndDelete(item._id); deleteAllChilds(item); }) ); + return false; +}; + +const findAndDeleteChilds = async (file: any) => { + const fl = await File.findById(file); + return deleteAllChilds(fl); }; export async function DELETE(req: NextRequest) { @@ -121,9 +123,11 @@ export async function DELETE(req: NextRequest) { await startDb(); const ids = JSON.parse(req.nextUrl.searchParams.get('_id') || ''); await Promise.all( - ids.map(async (file: string) => { - const fl = await File.findById(file); - deleteAllChilds(fl); + ids.map((file: string) => { + const some = new Promise((resolve) => { + findAndDeleteChilds(file).then(resolve); + }); + return some; }) ); return NextResponse.json(ids, { status: 200 }); diff --git a/app/api/drive/files/upload/route.ts b/app/api/drive/files/upload/route.ts index c851693..64a6079 100644 --- a/app/api/drive/files/upload/route.ts +++ b/app/api/drive/files/upload/route.ts @@ -35,13 +35,18 @@ export async function POST(req: Request) { const Body = (await file.arrayBuffer()) as Buffer; const Key = encodeURI(parent ? `${parent}/${file.name}` : file.name); await s3Client.send(new PutObjectCommand({ Bucket: 'dream-by-vishal', Key, Body })); - await File.create({ - user: session?.user._id, - parent, - name: file.name, - size: file.size, - link: Key, - }); + const isAlready = await File.findOne({ link: Key }); + if (!isAlready) { + await File.create({ + user: session?.user._id, + parent, + name: file.name, + size: file.size, + link: Key, + }); + } else { + await File.updateOne({ link: Key }, { name: file.name, size: file.size }); + } }) ); From 94e0313467fba2e5c9d5bf4db3cbcb83a29eca82 Mon Sep 17 00:00:00 2001 From: vishal Date: Mon, 1 Jul 2024 07:29:35 +0530 Subject: [PATCH 2/3] fix: path reset on close of move popup --- app/(private)/drive/[path]/page.tsx | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/(private)/drive/[path]/page.tsx b/app/(private)/drive/[path]/page.tsx index 46d952f..143c82b 100644 --- a/app/(private)/drive/[path]/page.tsx +++ b/app/(private)/drive/[path]/page.tsx @@ -212,9 +212,17 @@ const DrivePage = () => { ))} + {JSON.stringify(_path)} -