Skip to content

Commit

Permalink
Merge pull request #21 from vishalkondle45/feature/drive
Browse files Browse the repository at this point in the history
Feature/drive
  • Loading branch information
vishalkondle-dev authored Jul 1, 2024
2 parents 4bd7c0d + 6d32830 commit 3e07fbc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
9 changes: 8 additions & 1 deletion app/(private)/drive/[path]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,14 @@ const DrivePage = () => {
))}
</Table>
<Group mt="md" justify="right">
<Button color="red" onClick={() => setOpenMoveDialog(false)}>
<Button
color="red"
onClick={() => {
setOpenMoveDialog(false);
_setPath([]);
__setPath('');
}}
>
Cancel
</Button>
<Button color="teal" onClick={() => moveFile({ parent: __path, ids: checked })}>
Expand Down
36 changes: 20 additions & 16 deletions app/api/drive/files/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
})
Expand All @@ -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) {
Expand All @@ -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 });
Expand Down
19 changes: 12 additions & 7 deletions app/api/drive/files/upload/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
})
);

Expand Down

0 comments on commit 3e07fbc

Please sign in to comment.