22 CloudFrontClient ,
33 CreateInvalidationCommand ,
44} from "@aws-sdk/client-cloudfront" ;
5- import { db , updateIfDefined } from "@cap/database" ;
5+ import { updateIfDefined } from "@cap/database" ;
66import * as Db from "@cap/database/schema" ;
77import { serverEnv } from "@cap/env" ;
88import {
@@ -11,8 +11,10 @@ import {
1111 provideOptionalAuth ,
1212 S3Buckets ,
1313 Videos ,
14+ VideosPolicy ,
15+ VideosRepo ,
1416} from "@cap/web-backend" ;
15- import { Video } from "@cap/web-domain" ;
17+ import { CurrentUser , Policy , Video } from "@cap/web-domain" ;
1618import { zValidator } from "@hono/zod-validator" ;
1719import { and , eq } from "drizzle-orm" ;
1820import { Effect , Option , Schedule } from "effect" ;
@@ -47,14 +49,18 @@ app.post(
4749 } ) ;
4850
4951 const videoIdFromFileKey = fileKey . split ( "/" ) [ 1 ] ;
50- const videoId = "videoId" in body ? body . videoId : videoIdFromFileKey ;
51- if ( ! videoId ) throw new Error ( "Video ID is required" ) ;
52+ const videoIdRaw = "videoId" in body ? body . videoId : videoIdFromFileKey ;
53+ if ( ! videoIdRaw ) return c . text ( "Video id not found" , 400 ) ;
54+ const videoId = Video . VideoId . make ( videoIdRaw ) ;
5255
5356 const resp = await Effect . gen ( function * ( ) {
54- const videos = yield * Videos ;
57+ const repo = yield * VideosRepo ;
58+ const policy = yield * VideosPolicy ;
5559 const db = yield * Database ;
5660
57- const video = yield * videos . getById ( Video . VideoId . make ( videoId ) ) ;
61+ const video = yield * repo
62+ . getById ( videoId )
63+ . pipe ( Policy . withPolicy ( policy . isOwner ( videoId ) ) ) ;
5864 if ( Option . isNone ( video ) ) return yield * new Video . NotFoundError ( ) ;
5965
6066 yield * db . use ( ( db ) =>
@@ -74,6 +80,7 @@ app.post(
7480 c . json ( { error : "Error initiating multipart upload" } , 500 ) ,
7581 ) ;
7682 } ) ,
83+ Effect . provideService ( CurrentUser , user ) ,
7784 runPromise ,
7885 ) ;
7986 if ( resp ) return resp ;
@@ -230,24 +237,28 @@ app.post(
230237 ] ) ,
231238 ) ,
232239 ) ,
233- ( c ) =>
234- Effect . gen ( function * ( ) {
235- const videos = yield * Videos ;
236- const db = yield * Database ;
240+ ( c ) => {
241+ const { uploadId, parts, ...body } = c . req . valid ( "json" ) ;
242+ const user = c . get ( "user" ) ;
237243
238- const { uploadId, parts, ...body } = c . req . valid ( "json" ) ;
239- const user = c . get ( "user" ) ;
244+ return Effect . gen ( function * ( ) {
245+ const repo = yield * VideosRepo ;
246+ const policy = yield * VideosPolicy ;
247+ const db = yield * Database ;
240248
241249 const fileKey = parseVideoIdOrFileKey ( user . id , {
242250 ...body ,
243251 subpath : "result.mp4" ,
244252 } ) ;
245253
246254 const videoIdFromFileKey = fileKey . split ( "/" ) [ 1 ] ;
247- const videoId = "videoId" in body ? body . videoId : videoIdFromFileKey ;
248- if ( ! videoId ) throw new Error ( "Video ID is required" ) ;
255+ const videoIdRaw = "videoId" in body ? body . videoId : videoIdFromFileKey ;
256+ if ( ! videoIdRaw ) return c . text ( "Video id not found" , 400 ) ;
257+ const videoId = Video . VideoId . make ( videoIdRaw ) ;
249258
250- const maybeVideo = yield * videos . getById ( Video . VideoId . make ( videoId ) ) ;
259+ const maybeVideo = yield * repo
260+ . getById ( videoId )
261+ . pipe ( Policy . withPolicy ( policy . isOwner ( videoId ) ) ) ;
251262 if ( Option . isNone ( maybeVideo ) ) {
252263 c . status ( 404 ) ;
253264 return c . text ( `Video '${ encodeURIComponent ( videoId ) } ' not found` ) ;
@@ -467,5 +478,6 @@ app.post(
467478 ) ;
468479 } ) ,
469480 ) ;
470- } ) . pipe ( provideOptionalAuth , runPromise ) ,
481+ } ) . pipe ( Effect . provideService ( CurrentUser , user ) , runPromise ) ;
482+ } ,
471483) ;
0 commit comments