import { createUploadthing, type FileRouter } from "uploadthing/next";
import { UploadThingError } from "uploadthing/server";
const f = createUploadthing();
const auth = async (_req: Request) => ({ id: "demo_user" }); // Demo auth function
export const ourFileRouter = {
// Image uploader with size and count restrictions
imageUploader: f({
image: {
maxFileSize: "4MB",
maxFileCount: 4,
},
})
.middleware(async ({ req }) => {
const user = await auth(req);
if (!user) throw new UploadThingError("Unauthorized");
return { userId: user.id };
})
.onUploadComplete(async ({ metadata, file }) => {
console.log("Upload complete for userId:", metadata.userId);
console.log("file url", file.url);
return { uploadedBy: metadata.userId };
}),
// PDF document uploader
pdfUploader: f({
pdf: {
maxFileSize: "16MB",
maxFileCount: 1,
},
})
.middleware(async ({ req }) => {
const user = await auth(req);
if (!user) throw new UploadThingError("Unauthorized");
return { userId: user.id };
})
.onUploadComplete(async ({ metadata, file }) => {
console.log("PDF upload complete for userId:", metadata.userId);
console.log("file url", file.url);
return { uploadedBy: metadata.userId };
}),
// Media uploader (images, videos, audio)
mediaUploader: f({
image: { maxFileSize: "4MB" },
video: { maxFileSize: "16MB" },
audio: { maxFileSize: "8MB" },
})
.middleware(async ({ req }) => {
const user = await auth(req);
if (!user) throw new UploadThingError("Unauthorized");
return { userId: user.id };
})
.onUploadComplete(async ({ metadata, file }) => {
console.log("Media upload complete for userId:", metadata.userId);
console.log("file url", file.url);
return { uploadedBy: metadata.userId };
}),
// General file uploader
fileUploader: f({
image: { maxFileSize: "4MB" },
video: { maxFileSize: "16MB" },
audio: { maxFileSize: "8MB" },
pdf: { maxFileSize: "16MB" },
text: { maxFileSize: "1MB" },
blob: { maxFileSize: "32MB" },
})
.middleware(async ({ req }) => {
const user = await auth(req);
if (!user) throw new UploadThingError("Unauthorized");
return { userId: user.id, timestamp: Date.now() };
})
.onUploadComplete(async ({ metadata, file }) => {
console.log("File upload complete for userId:", metadata.userId);
console.log("file url", file.url);
console.log("upload timestamp:", metadata.timestamp);
return { uploadedBy: metadata.userId, timestamp: metadata.timestamp };
}),
} satisfies FileRouter;
export type OurFileRouter = typeof ourFileRouter;