-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore[VERCEL_CRON-JOBS]: Added cron jobs for removing unused data
Data will be automatically removed from the server if its unused
- Loading branch information
1 parent
f858c28
commit 829fc70
Showing
5 changed files
with
70 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { prisma } from "@zephyr/db"; | ||
import { UTApi } from "uploadthing/server"; | ||
|
||
export async function GET(req: Request) { | ||
try { | ||
const authHeader = req.headers.get("Authorization"); | ||
|
||
if (authHeader !== `Bearer ${process.env.CRON_SECRET}`) { | ||
return Response.json( | ||
{ message: "Invalid authorization header" }, | ||
{ status: 401 } | ||
); | ||
} | ||
|
||
const unusedMedia = await prisma.media.findMany({ | ||
where: { | ||
postId: null, | ||
...(process.env.NODE_ENV === "production" | ||
? { | ||
createdAt: { | ||
lte: new Date(Date.now() - 1000 * 60 * 60 * 24) | ||
} | ||
} | ||
: {}) | ||
}, | ||
select: { | ||
id: true, | ||
url: true | ||
} | ||
}); | ||
|
||
new UTApi().deleteFiles( | ||
unusedMedia.map( | ||
(m) => | ||
m.url.split(`/a/${process.env.NEXT_PUBLIC_UPLOADTHING_APP_ID}/`)[1] | ||
) | ||
); | ||
|
||
await prisma.media.deleteMany({ | ||
where: { | ||
id: { | ||
in: unusedMedia.map((m) => m.id) | ||
} | ||
} | ||
}); | ||
|
||
return new Response(); | ||
} catch (error) { | ||
console.error(error); | ||
return Response.json({ error: "Internal server error" }, { status: 500 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"crons": [ | ||
{ | ||
"path": "/api/clear-uploads", | ||
"schedule": "0 2 * * *" | ||
} | ||
] | ||
} |