diff --git a/app/api/uploadthing/core.ts b/app/api/uploadthing/core.ts new file mode 100644 index 0000000..f4a0b08 --- /dev/null +++ b/app/api/uploadthing/core.ts @@ -0,0 +1,21 @@ +import { auth } from "@clerk/nextjs"; +import { createUploadthing, type FileRouter } from "uploadthing/next"; + +const f = createUploadthing(); + +const handleAuth = () => { + const { userId } = auth(); + if (!userId) throw new Error("Unauthorized"); + return { userId: userId }; +} + +export const ourFileRouter = { + serverImage: f({ image: { maxFileSize: "4MB", maxFileCount: 1 } }) + .middleware(() => handleAuth()) + .onUploadComplete(() => {}), + messageFile: f(["image", "pdf"]) + .middleware(() => handleAuth()) + .onUploadComplete(() => {}) +} satisfies FileRouter; + +export type OurFileRouter = typeof ourFileRouter; \ No newline at end of file diff --git a/app/api/uploadthing/route.ts b/app/api/uploadthing/route.ts new file mode 100644 index 0000000..43ca1cd --- /dev/null +++ b/app/api/uploadthing/route.ts @@ -0,0 +1,8 @@ +import { createNextRouteHandler } from "uploadthing/next"; + +import { ourFileRouter } from "./core"; + +// Export routes for Next App Router +export const { GET, POST } = createNextRouteHandler({ + router: ourFileRouter, +}); \ No newline at end of file diff --git a/components/file-upload.tsx b/components/file-upload.tsx new file mode 100644 index 0000000..70dbf3a --- /dev/null +++ b/components/file-upload.tsx @@ -0,0 +1,54 @@ +"use client"; + +import { X } from "lucide-react"; +import Image from "next/image"; + +import { UploadDropzone } from "@/lib/uploadthing"; + +import "@uploadthing/react/styles.css"; + +interface FileUploadProps { + onChange: (url?: string) => void; + value: string; + endpoint: "messageFile" | "serverImage"; + } + +export const FileUpload = ({ + onChange, + value, + endpoint, +}: FileUploadProps) => { + const fileType = value?.split(".").pop(); + + if (value && fileType !== "pdf") { + return ( +