-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.ts
More file actions
39 lines (29 loc) · 1.36 KB
/
core.ts
File metadata and controls
39 lines (29 loc) · 1.36 KB
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
34
35
36
37
38
39
/* eslint-disable @typescript-eslint/only-throw-error */
import { auth } from "@clerk/nextjs/server";
import { createUploadthing, type FileRouter } from "uploadthing/next";
import { UploadThingError } from "uploadthing/server";
import { z } from "zod";
import * as mutations from "~/server/db/mutations";
import * as queries from "~/server/db/queries";
const f = createUploadthing();
export const ourFileRouter = {
driveUploader: f({ blob: { maxFileSize: "4MB", maxFileCount: 1 } })
.input(z.object({ folderId: z.number() }))
.middleware(async ({ input }) => {
const user = await auth();
if (!user.userId) throw new UploadThingError("Unauthorized");
const folder = await queries.getFolderById(input.folderId);
if (!folder) throw new UploadThingError("Folder not found");
if (folder.ownerId !== user.userId)
throw new UploadThingError("Unauthorized");
return { userId: user.userId, parent: folder.id };
})
.onUploadComplete(async ({ metadata, file }) => {
console.log("Upload complete for userId:", metadata.userId);
console.log("file url", file.ufsUrl);
const uploadedFile = { ...file, parent: metadata.parent };
await mutations.createFile(uploadedFile, metadata.userId);
return { uploadedBy: metadata.userId };
}),
} satisfies FileRouter;
export type OurFileRouter = typeof ourFileRouter;