@@ -4,7 +4,7 @@ import { db } from "@cap/database";
44import { getCurrentUser } from "@cap/database/auth/session" ;
55import { spaces } from "@cap/database/schema" ;
66import { serverEnv } from "@cap/env" ;
7- import { S3BucketAccess , S3Buckets } from "@cap/web-backend" ;
7+ import { S3Buckets } from "@cap/web-backend" ;
88import { eq } from "drizzle-orm" ;
99import { Effect , Option } from "effect" ;
1010import { revalidatePath } from "next/cache" ;
@@ -54,10 +54,9 @@ export async function uploadSpaceIcon(formData: FormData, spaceId: string) {
5454 space . organizationId
5555 } /spaces/${ spaceId } /icon-${ Date . now ( ) } .${ fileExtension } `;
5656
57- const [ S3ProviderLayer ] = await Effect . gen ( function * ( ) {
58- const s3Buckets = yield * S3Buckets ;
59- return yield * s3Buckets . getProviderForBucket ( Option . none ( ) ) ;
60- } ) . pipe ( runPromise ) ;
57+ const [ bucket ] = await S3Buckets . getBucketAccess ( Option . none ( ) ) . pipe (
58+ runPromise ,
59+ ) ;
6160
6261 try {
6362 // Remove previous icon if exists
@@ -66,10 +65,7 @@ export async function uploadSpaceIcon(formData: FormData, spaceId: string) {
6665 const key = space . iconUrl . match ( / o r g a n i z a t i o n s \/ .+ / ) ?. [ 0 ] ;
6766 if ( key ) {
6867 try {
69- await Effect . gen ( function * ( ) {
70- const bucket = yield * S3BucketAccess ;
71- return yield * bucket . deleteObject ( key ) ;
72- } ) . pipe ( Effect . provide ( S3ProviderLayer ) , runPromise ) ;
68+ await bucket . deleteObject ( key ) . pipe ( runPromise ) ;
7369 } catch ( e ) {
7470 // Log and continue
7571 console . warn ( "Failed to delete old space icon from S3" , e ) ;
@@ -79,26 +75,26 @@ export async function uploadSpaceIcon(formData: FormData, spaceId: string) {
7975
8076 const sanitizedFile = await sanitizeFile ( file ) ;
8177
82- let iconUrl ;
83- await Effect . gen ( function * ( ) {
84- const bucket = yield * S3BucketAccess ;
85- yield * bucket . putObject (
78+ await bucket
79+ . putObject (
8680 fileKey ,
8781 Effect . promise ( ( ) => sanitizedFile . bytes ( ) ) ,
8882 { contentType : file . type } ,
89- ) ;
90-
91- // Construct the icon URL
92- if ( serverEnv ( ) . CAP_AWS_BUCKET_URL ) {
93- iconUrl = `${ serverEnv ( ) . CAP_AWS_BUCKET_URL } /${ fileKey } ` ;
94- } else if ( serverEnv ( ) . CAP_AWS_ENDPOINT ) {
95- iconUrl = `${ serverEnv ( ) . CAP_AWS_ENDPOINT } /${ bucket . bucketName } /${ fileKey } ` ;
96- } else {
97- iconUrl = `https://${ bucket . bucketName } .s3.${
98- serverEnv ( ) . CAP_AWS_REGION || "us-east-1"
99- } .amazonaws.com/${ fileKey } `;
100- }
101- } ) . pipe ( Effect . provide ( S3ProviderLayer ) , runPromise ) ;
83+ )
84+ . pipe ( runPromise ) ;
85+
86+ let iconUrl : string | undefined ;
87+
88+ // Construct the icon URL
89+ if ( serverEnv ( ) . CAP_AWS_BUCKET_URL ) {
90+ iconUrl = `${ serverEnv ( ) . CAP_AWS_BUCKET_URL } /${ fileKey } ` ;
91+ } else if ( serverEnv ( ) . CAP_AWS_ENDPOINT ) {
92+ iconUrl = `${ serverEnv ( ) . CAP_AWS_ENDPOINT } /${ bucket . bucketName } /${ fileKey } ` ;
93+ } else {
94+ iconUrl = `https://${ bucket . bucketName } .s3.${
95+ serverEnv ( ) . CAP_AWS_REGION || "us-east-1"
96+ } .amazonaws.com/${ fileKey } `;
97+ }
10298
10399 // Update space with new icon URL
104100 await db ( ) . update ( spaces ) . set ( { iconUrl } ) . where ( eq ( spaces . id , spaceId ) ) ;
0 commit comments