Skip to content

Commit 00af35c

Browse files
committed
uploading files works
1 parent 7122a32 commit 00af35c

9 files changed

Lines changed: 241 additions & 13 deletions

File tree

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"@libsql/client": "^0.9.0",
2525
"@radix-ui/react-slot": "^1.1.2",
2626
"@t3-oss/env-nextjs": "^0.10.1",
27+
"@uploadthing/react": "^7.2.1",
2728
"class-variance-authority": "^0.7.1",
2829
"clsx": "^2.1.1",
2930
"drizzle-orm": "^0.39.3",
@@ -35,6 +36,7 @@
3536
"react-dom": "^18.3.1",
3637
"tailwind-merge": "^3.0.1",
3738
"tailwindcss-animate": "^1.0.7",
39+
"uploadthing": "^7.5.1",
3840
"zod": "^3.23.3"
3941
},
4042
"devDependencies": {

pnpm-lock.yaml

Lines changed: 130 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/api/uploadthing/core.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { auth } from "@clerk/nextjs/server";
2+
import { createUploadthing, type FileRouter } from "uploadthing/next";
3+
import { UploadThingError } from "uploadthing/server";
4+
import { MUTATIONS } from "~/server/db/queries";
5+
6+
const f = createUploadthing();
7+
8+
9+
// FileRouter for your app, can contain multiple FileRoutes
10+
export const ourFileRouter = {
11+
// Define as many FileRoutes as you like, each with a unique routeSlug
12+
imageUploader: f({
13+
image: {
14+
/**
15+
* For full list of options and defaults, see the File Route API reference
16+
* @see https://docs.uploadthing.com/file-routes#route-config
17+
*/
18+
maxFileSize: "4MB",
19+
maxFileCount: 1,
20+
},
21+
})
22+
// Set permissions and file types for this FileRoute
23+
.middleware(async () => {
24+
// This code runs on your server before upload
25+
const user = await auth();
26+
27+
// If you throw, the user will not be able to upload
28+
// eslint-disable-next-line @typescript-eslint/only-throw-error
29+
if (!user.userId) throw new UploadThingError("Unauthorized");
30+
31+
// Whatever is returned here is accessible in onUploadComplete as `metadata`
32+
return { userId: user.userId };
33+
})
34+
.onUploadComplete(async ({ metadata, file }) => {
35+
// This code RUNS ON YOUR SERVER after upload
36+
console.log("Upload complete for userId:", metadata.userId);
37+
38+
console.log("file url", file.ufsUrl);
39+
40+
await MUTATIONS.createFile({
41+
file: {
42+
name: file.name,
43+
size: file.size,
44+
url: file.ufsUrl,
45+
},
46+
userId: metadata.userId
47+
})
48+
49+
// !!! Whatever is returned here is sent to the clientside `onClientUploadComplete` callback
50+
return { uploadedBy: metadata.userId };
51+
}),
52+
} satisfies FileRouter;
53+
54+
export type OurFileRouter = typeof ourFileRouter;

src/app/api/uploadthing/route.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { createRouteHandler } from "uploadthing/next";
2+
3+
import { ourFileRouter } from "./core";
4+
5+
// Export routes for Next App Router
6+
export const { GET, POST } = createRouteHandler({
7+
router: ourFileRouter,
8+
9+
// Apply an (optional) custom config:
10+
// config: { ... },
11+
});

0 commit comments

Comments
 (0)