|
| 1 | +"use server"; |
| 2 | + |
| 3 | +import { db } from "@cap/database"; |
| 4 | +import { organizations, users } from "@cap/database/schema"; |
| 5 | +import { S3Buckets } from "@cap/web-backend"; |
| 6 | +import { OrganisationId, UserId } from "@cap/web-domain"; |
| 7 | +import { eq } from "drizzle-orm"; |
| 8 | +import { Effect, Option } from "effect"; |
| 9 | +import { revalidatePath } from "next/cache"; |
| 10 | +import { runPromise } from "@/lib/server"; |
| 11 | + |
| 12 | +export async function uploadImage( |
| 13 | + file: File, |
| 14 | + type: "user" | "organization", |
| 15 | + entityId: string, |
| 16 | + oldImageUrlOrKey?: string | null, |
| 17 | +) { |
| 18 | + try { |
| 19 | + const s3Key = await Effect.gen(function* () { |
| 20 | + const [bucket] = yield* S3Buckets.getBucketAccess(Option.none()); |
| 21 | + |
| 22 | + // Delete old image if it exists |
| 23 | + if (oldImageUrlOrKey) { |
| 24 | + try { |
| 25 | + // Extract the S3 key - handle both old URL format and new key format |
| 26 | + let oldS3Key = oldImageUrlOrKey; |
| 27 | + if ( |
| 28 | + oldImageUrlOrKey.startsWith("http://") || |
| 29 | + oldImageUrlOrKey.startsWith("https://") |
| 30 | + ) { |
| 31 | + const url = new URL(oldImageUrlOrKey); |
| 32 | + oldS3Key = url.pathname.substring(1); |
| 33 | + } |
| 34 | + |
| 35 | + // Only delete if it looks like the correct type of image key |
| 36 | + const expectedPrefix = type === "user" ? "users/" : "organizations/"; |
| 37 | + if (oldS3Key.startsWith(expectedPrefix)) { |
| 38 | + yield* bucket.deleteObject(oldS3Key); |
| 39 | + } |
| 40 | + } catch (error) { |
| 41 | + console.error(`Error deleting old ${type} image from S3:`, error); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + // Generate new S3 key |
| 46 | + const timestamp = Date.now(); |
| 47 | + const fileExtension = file.name.split(".").pop() || "jpg"; |
| 48 | + const s3Key = `${type}s/${entityId}/${timestamp}.${fileExtension}`; |
| 49 | + |
| 50 | + // Upload new image |
| 51 | + const arrayBuffer = yield* Effect.promise(() => file.arrayBuffer()); |
| 52 | + const buffer = Buffer.from(arrayBuffer); |
| 53 | + yield* bucket.putObject(s3Key, buffer, { |
| 54 | + contentType: file.type, |
| 55 | + }); |
| 56 | + |
| 57 | + return s3Key; |
| 58 | + }).pipe(runPromise); |
| 59 | + |
| 60 | + // Update database |
| 61 | + if (type === "user") { |
| 62 | + await db() |
| 63 | + .update(users) |
| 64 | + .set({ image: s3Key }) |
| 65 | + .where(eq(users.id, UserId.make(entityId))); |
| 66 | + } else { |
| 67 | + await db() |
| 68 | + .update(organizations) |
| 69 | + .set({ iconUrl: s3Key }) |
| 70 | + .where(eq(organizations.id, OrganisationId.make(entityId))); |
| 71 | + } |
| 72 | + |
| 73 | + revalidatePath("/dashboard/settings/account"); |
| 74 | + if (type === "organization") { |
| 75 | + revalidatePath("/dashboard/settings/organization"); |
| 76 | + } |
| 77 | + |
| 78 | + return { success: true, image: s3Key } as const; |
| 79 | + } catch (error) { |
| 80 | + console.error(`Error uploading ${type} image:`, error); |
| 81 | + throw new Error(error instanceof Error ? error.message : "Upload failed"); |
| 82 | + } |
| 83 | +} |
0 commit comments