-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: Add profile image upload and display support #1214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,25 @@ | ||
| "use server"; | ||
|
|
||
| import { db } from "@cap/database"; | ||
| import { getCurrentUser } from "@cap/database/auth/session"; | ||
| import { users } from "@cap/database/schema"; | ||
| import { eq } from "drizzle-orm"; | ||
| import { revalidatePath } from "next/cache"; | ||
|
|
||
| export async function removeProfileImage() { | ||
| const user = await getCurrentUser(); | ||
|
|
||
| if (!user) { | ||
| throw new Error("Unauthorized"); | ||
| } | ||
|
|
||
| await db() | ||
| .update(users) | ||
| .set({ image: null }) | ||
| .where(eq(users.id, user.id)); | ||
|
|
||
| revalidatePath("/dashboard/settings/account"); | ||
| revalidatePath("/dashboard", "layout"); | ||
|
|
||
| return { success: true } as const; | ||
| } |
This file contains hidden or 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,94 @@ | ||
| "use server"; | ||
|
|
||
| import { db } from "@cap/database"; | ||
| import { getCurrentUser } from "@cap/database/auth/session"; | ||
| import { users } from "@cap/database/schema"; | ||
| import { serverEnv } from "@cap/env"; | ||
| import { S3Buckets } from "@cap/web-backend"; | ||
| import { eq } from "drizzle-orm"; | ||
| import { Effect, Option } from "effect"; | ||
| import { revalidatePath } from "next/cache"; | ||
| import { sanitizeFile } from "@/lib/sanitizeFile"; | ||
| import { runPromise } from "@/lib/server"; | ||
| import { randomUUID } from "node:crypto"; | ||
|
|
||
| const MAX_FILE_SIZE_BYTES = 3 * 1024 * 1024; // 3MB | ||
| const ALLOWED_IMAGE_TYPES = new Map<string, string>([ | ||
| ["image/png", "png"], | ||
| ["image/jpeg", "jpg"], | ||
| ["image/jpg", "jpg"], | ||
| ]); | ||
|
|
||
| export async function uploadProfileImage(formData: FormData) { | ||
| const user = await getCurrentUser(); | ||
|
|
||
| if (!user) { | ||
| throw new Error("Unauthorized"); | ||
| } | ||
|
|
||
| const file = formData.get("image") as File | null; | ||
|
|
||
| if (!file) { | ||
| throw new Error("No file provided"); | ||
| } | ||
|
|
||
| const normalizedType = file.type.toLowerCase(); | ||
| const fileExtension = ALLOWED_IMAGE_TYPES.get(normalizedType); | ||
|
|
||
| if (!fileExtension) { | ||
| throw new Error("Only PNG or JPEG images are supported"); | ||
| } | ||
|
|
||
| if (file.size > MAX_FILE_SIZE_BYTES) { | ||
| throw new Error("File size must be 3MB or less"); | ||
| } | ||
|
|
||
| const fileKey = `users/${user.id}/profile-${Date.now()}-${randomUUID()}.${fileExtension}`; | ||
|
|
||
| try { | ||
| const sanitizedFile = await sanitizeFile(file); | ||
| let imageUrl: string | undefined; | ||
|
|
||
| await Effect.gen(function* () { | ||
| const [bucket] = yield* S3Buckets.getBucketAccess(Option.none()); | ||
|
|
||
| const bodyBytes = yield* Effect.promise(async () => { | ||
| const buf = await sanitizedFile.arrayBuffer(); | ||
| return new Uint8Array(buf); | ||
| }); | ||
|
|
||
| yield* bucket.putObject(fileKey, bodyBytes, { | ||
| contentType: file.type, | ||
| }); | ||
|
|
||
| if (serverEnv().CAP_AWS_BUCKET_URL) { | ||
| imageUrl = `${serverEnv().CAP_AWS_BUCKET_URL}/${fileKey}`; | ||
| } else if (serverEnv().CAP_AWS_ENDPOINT) { | ||
| imageUrl = `${serverEnv().CAP_AWS_ENDPOINT}/${bucket.bucketName}/${fileKey}`; | ||
| } else { | ||
| imageUrl = `https://${bucket.bucketName}.s3.${ | ||
| serverEnv().CAP_AWS_REGION || "us-east-1" | ||
| }.amazonaws.com/${fileKey}`; | ||
| } | ||
| }).pipe(runPromise); | ||
|
|
||
| if (typeof imageUrl !== "string" || imageUrl.length === 0) { | ||
| throw new Error("Failed to resolve uploaded profile image URL"); | ||
| } | ||
|
|
||
| const finalImageUrl = imageUrl; | ||
|
|
||
| await db() | ||
| .update(users) | ||
| .set({ image: finalImageUrl }) | ||
| .where(eq(users.id, user.id)); | ||
|
|
||
| revalidatePath("/dashboard/settings/account"); | ||
| revalidatePath("/dashboard", "layout"); | ||
|
|
||
| return { success: true, imageUrl: finalImageUrl } as const; | ||
| } catch (error) { | ||
| console.error("Error uploading profile image:", error); | ||
| throw new Error(error instanceof Error ? error.message : "Upload failed"); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.