initial commit
This commit is contained in:
41
app/api/uploadthing/core.ts
Normal file
41
app/api/uploadthing/core.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { clerkClient, auth } from "@clerk/nextjs/server";
|
||||
import { createUploadthing, type FileRouter } from "uploadthing/next";
|
||||
import { UploadThingError } from "uploadthing/server";
|
||||
const f = createUploadthing();
|
||||
|
||||
// FileRouter for your app, can contain multiple FileRoutes
|
||||
export const ourFileRouter = {
|
||||
// Define as many FileRoutes as you like, each with a unique routeSlug
|
||||
imageUploader: f({
|
||||
image: {
|
||||
/**
|
||||
* For full list of options and defaults, see the File Route API reference
|
||||
* @see https://docs.uploadthing.com/file-routes#route-config
|
||||
*/
|
||||
maxFileSize: "4MB",
|
||||
maxFileCount: 1,
|
||||
},
|
||||
})
|
||||
// Set permissions and file types for this FileRoute
|
||||
.middleware(async () => {
|
||||
// This code runs on your server before upload
|
||||
const user = await auth();
|
||||
|
||||
// If you throw, the user will not be able to upload
|
||||
if (!user) throw new UploadThingError("Unauthorized");
|
||||
|
||||
// Whatever is returned here is accessible in onUploadComplete as `metadata`
|
||||
return { userId: user.userId };
|
||||
})
|
||||
.onUploadComplete(async ({ metadata, file }) => {
|
||||
// This code RUNS ON YOUR SERVER after upload
|
||||
console.log("Upload complete for userId:", metadata.userId);
|
||||
|
||||
console.log("file url", file.ufsUrl);
|
||||
|
||||
// !!! Whatever is returned here is sent to the clientside `onClientUploadComplete` callback
|
||||
return { uploadedBy: metadata.userId };
|
||||
}),
|
||||
} satisfies FileRouter;
|
||||
|
||||
export type OurFileRouter = typeof ourFileRouter;
|
||||
11
app/api/uploadthing/route.ts
Normal file
11
app/api/uploadthing/route.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { createRouteHandler } from "uploadthing/next";
|
||||
|
||||
import { ourFileRouter } from "./core";
|
||||
|
||||
// Export routes for Next App Router
|
||||
export const { GET, POST } = createRouteHandler({
|
||||
router: ourFileRouter,
|
||||
|
||||
// Apply an (optional) custom config:
|
||||
// config: { ... },
|
||||
});
|
||||
Reference in New Issue
Block a user