1- import {
2- CloudFrontClient ,
3- CreateInvalidationCommand ,
4- } from "@aws-sdk/client-cloudfront" ;
51import type { PresignedPost } from "@aws-sdk/s3-presigned-post" ;
62import { db , updateIfDefined } from "@cap/database" ;
73import * as Db from "@cap/database/schema" ;
8- import { serverEnv } from "@cap/env" ;
9- import { AwsCredentials , S3Buckets } from "@cap/web-backend" ;
4+ import { S3Buckets } from "@cap/web-backend" ;
105import { Video } from "@cap/web-domain" ;
116import { zValidator } from "@hono/zod-validator" ;
127import { and , eq } from "drizzle-orm" ;
@@ -125,53 +120,6 @@ app.post(
125120 . from ( Db . s3Buckets )
126121 . where ( eq ( Db . s3Buckets . ownerId , user . id ) ) ;
127122
128- const s3Config = customBucket
129- ? {
130- endpoint : customBucket . endpoint || undefined ,
131- region : customBucket . region ,
132- accessKeyId : customBucket . accessKeyId ,
133- secretAccessKey : customBucket . secretAccessKey ,
134- }
135- : null ;
136-
137- if (
138- ! customBucket ||
139- ! s3Config ||
140- customBucket . bucketName !== serverEnv ( ) . CAP_AWS_BUCKET
141- ) {
142- const distributionId = serverEnv ( ) . CAP_CLOUDFRONT_DISTRIBUTION_ID ;
143- if ( distributionId ) {
144- console . log ( "Creating CloudFront invalidation for" , fileKey ) ;
145-
146- const cloudfront = new CloudFrontClient ( {
147- region : serverEnv ( ) . CAP_AWS_REGION || "us-east-1" ,
148- credentials : await runPromise (
149- Effect . map ( AwsCredentials , ( c ) => c . credentials ) ,
150- ) ,
151- } ) ;
152-
153- const pathToInvalidate = `/${ fileKey } ` ;
154-
155- try {
156- const invalidation = await cloudfront . send (
157- new CreateInvalidationCommand ( {
158- DistributionId : distributionId ,
159- InvalidationBatch : {
160- CallerReference : `${ Date . now ( ) } ` ,
161- Paths : {
162- Quantity : 1 ,
163- Items : [ pathToInvalidate ] ,
164- } ,
165- } ,
166- } ) ,
167- ) ;
168- console . log ( "CloudFront invalidation created:" , invalidation ) ;
169- } catch ( error ) {
170- console . error ( "Failed to create CloudFront invalidation:" , error ) ;
171- }
172- }
173- }
174-
175123 const contentType = fileKey . endsWith ( ".aac" )
176124 ? "audio/aac"
177125 : fileKey . endsWith ( ".webm" )
@@ -184,9 +132,7 @@ app.post(
184132 ? "application/x-mpegURL"
185133 : "video/mp2t" ;
186134
187- let data : PresignedPost ;
188-
189- await Effect . gen ( function * ( ) {
135+ const data = await Effect . gen ( function * ( ) {
190136 const [ bucket ] = yield * S3Buckets . getBucketAccess (
191137 Option . fromNullable ( customBucket ?. id ) ,
192138 ) ;
@@ -200,31 +146,30 @@ app.post(
200146 : "" ,
201147 } ;
202148
203- data = yield * bucket . getPresignedPostUrl ( fileKey , {
149+ return yield * bucket . getPresignedPostUrl ( fileKey , {
204150 Fields,
205151 Expires : 1800 ,
206152 } ) ;
207- } else if ( method === "put" ) {
208- const presignedUrl = yield * bucket . getPresignedPutUrl (
209- fileKey ,
210- {
211- ContentType : contentType ,
212- Metadata : {
213- userid : user . id ,
214- duration : durationInSecs ? durationInSecs . toString ( ) : "" ,
215- } ,
153+ }
154+
155+ const presignedUrl = yield * bucket . getPresignedPutUrl (
156+ fileKey ,
157+ {
158+ ContentType : contentType ,
159+ Metadata : {
160+ userid : user . id ,
161+ duration : durationInSecs ? durationInSecs . toString ( ) : "" ,
216162 } ,
217- { expiresIn : 1800 } ,
218- ) ;
163+ } ,
164+ { expiresIn : 1800 } ,
165+ ) ;
219166
220- data = { url : presignedUrl , fields : { } } ;
221- }
167+ return { url : presignedUrl , fields : { } } satisfies PresignedPost ;
222168 } ) . pipe ( runPromise ) ;
223169
224170 console . log ( "Presigned URL created successfully" ) ;
225171
226- // After successful presigned URL creation, trigger revalidation
227- const videoIdFromKey = fileKey . split ( "/" ) [ 1 ] ; // Assuming fileKey format is userId/videoId/...
172+ const videoIdFromKey = fileKey . split ( "/" ) [ 1 ] ;
228173
229174 const videoIdToUse = "videoId" in body ? body . videoId : videoIdFromKey ;
230175 if ( videoIdToUse ) {
@@ -241,7 +186,6 @@ app.post(
241186 and ( eq ( Db . videos . id , videoId ) , eq ( Db . videos . ownerId , user . id ) ) ,
242187 ) ;
243188
244- // i hate this but it'll have to do
245189 const clientSupportsUploadProgress = isFromDesktopSemver (
246190 c . req ,
247191 UPLOAD_PROGRESS_VERSION ,
@@ -253,8 +197,8 @@ app.post(
253197 . where ( eq ( Db . videoUploads . videoId , videoId ) ) ;
254198 }
255199
256- if ( method === "post" ) return c . json ( { presignedPostData : data ! } ) ;
257- else return c . json ( { presignedPutData : data ! } ) ;
200+ if ( method === "post" ) return c . json ( { presignedPostData : data } ) ;
201+ else return c . json ( { presignedPutData : data } ) ;
258202 } catch ( s3Error ) {
259203 console . error ( "S3 operation failed:" , s3Error ) ;
260204 throw new Error (
0 commit comments