-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactions.ts
More file actions
33 lines (24 loc) · 951 Bytes
/
actions.ts
File metadata and controls
33 lines (24 loc) · 951 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"use server";
import { auth } from "@clerk/nextjs/server";
import { and, eq } from "drizzle-orm";
import { cookies } from "next/headers";
import { UTApi } from "uploadthing/server";
import { db } from "~/server/db";
import { files_table as fileSchema } from "~/server/db/schema";
const utApi = new UTApi();
export async function deleteFile(fileId: number) {
const session = await auth();
if (!session.userId) return { error: "Unauthorized" };
const [file] = await db
.select()
.from(fileSchema)
.where(
and(eq(fileSchema.id, fileId), eq(fileSchema.ownerId, session.userId)),
);
if (!file) return { error: "File not found" };
await utApi.deleteFiles([file.url.replace("https://utfs.io/f/", "")]); // TODO: add fileKey to db schema
await db.delete(fileSchema).where(eq(fileSchema.id, fileId));
const c = await cookies();
c.set("force-refresh", JSON.stringify(Math.random()));
return { success: true };
}