-
-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix image upload on articles (#1126)
* Fix image upload on articles * Adds safe actions for actions * Uses actions instead of trpc
- Loading branch information
1 parent
9803fd1
commit a47e8d2
Showing
6 changed files
with
139 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"use server"; | ||
|
||
import * as Sentry from "@sentry/nextjs"; | ||
import { getPresignedUrl } from "@/server/common/getPresignedUrl"; | ||
import { authActionClient } from "@/server/lib/safeAction"; | ||
|
||
import { z } from "zod"; | ||
|
||
const schema = z.object({ | ||
type: z.string(), | ||
size: z.number(), | ||
uploadType: z.enum(["uploads", "user"]).default("uploads"), | ||
}); | ||
|
||
export const getUploadUrl = authActionClient | ||
.schema(schema) | ||
.action(async ({ parsedInput, ctx }) => { | ||
const { type, size, uploadType } = parsedInput; | ||
const extension = type.split("/")[1]; | ||
const acceptedFormats = ["jpg", "jpeg", "gif", "png", "webp"]; | ||
|
||
if (!acceptedFormats.includes(extension)) { | ||
throw new Error( | ||
`Invalid file. Accepted file formats: ${acceptedFormats.join(", ")}.`, | ||
); | ||
} | ||
|
||
if (size > 1048576 * 10) { | ||
throw new Error("Maximum file size 10mb"); | ||
} | ||
|
||
try { | ||
const response = await getPresignedUrl(type, size, { | ||
kind: uploadType, | ||
userId: ctx.user.id, | ||
}); | ||
|
||
return response; | ||
} catch (error) { | ||
Sentry.captureException(error); | ||
console.error("Error getting presigned URL:", error); | ||
throw new Error("Failed to upload image."); | ||
} | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import "server-only"; | ||
|
||
import { createSafeActionClient } from "next-safe-action"; | ||
import { getServerAuthSession } from "@/server/auth"; | ||
export const actionClient = createSafeActionClient(); | ||
|
||
export const authActionClient = actionClient.use(async ({ next }) => { | ||
const session = await getServerAuthSession(); | ||
|
||
if (!session || !session.user) { | ||
throw new Error("Session invalid."); | ||
} | ||
|
||
return next({ ctx: { user: session.user } }); | ||
}); |