add api route to check for expired videos
This commit is contained in:
parent
141c3e385c
commit
4c64aeeb35
1 changed files with 58 additions and 0 deletions
58
src/pages/api/check-expired-videos.ts
Normal file
58
src/pages/api/check-expired-videos.ts
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { prisma } from "~/server/db";
|
||||
import { DeleteObjectCommand } from "@aws-sdk/client-s3";
|
||||
import { env } from "~/env.mjs";
|
||||
import { s3 } from "~/server/aws/s3";
|
||||
|
||||
export default async function handler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse
|
||||
) {
|
||||
const expiredVideos = await prisma.video.findMany({
|
||||
where: {
|
||||
shareLinkExpiresAt: {
|
||||
lte: new Date(),
|
||||
},
|
||||
delete_after_link_expires: true,
|
||||
sharing: true,
|
||||
},
|
||||
include: {
|
||||
user: true,
|
||||
},
|
||||
});
|
||||
|
||||
const updatedVideos = await prisma.video.updateMany({
|
||||
where: {
|
||||
shareLinkExpiresAt: {
|
||||
lte: new Date(),
|
||||
},
|
||||
delete_after_link_expires: false,
|
||||
sharing: true,
|
||||
},
|
||||
data: {
|
||||
sharing: false,
|
||||
},
|
||||
});
|
||||
|
||||
const expiredVideoIds = expiredVideos.map((x) => x.id);
|
||||
expiredVideos.map(async (video) => {
|
||||
const deleteObjectCommand = new DeleteObjectCommand({
|
||||
Bucket: env.AWS_BUCKET_NAME,
|
||||
Key: video.user.id + "/" + video.id,
|
||||
});
|
||||
|
||||
return await s3.send(deleteObjectCommand);
|
||||
});
|
||||
|
||||
const deletedVideos = await prisma.video.deleteMany({
|
||||
where: {
|
||||
id: {
|
||||
in: expiredVideoIds,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
res
|
||||
.status(200)
|
||||
.json({ name: "John Doe", expiredVideos, updatedVideos, deletedVideos });
|
||||
}
|
||||
Loading…
Reference in a new issue