Skip to content

Commit 587c7c5

Browse files
fixes
1 parent aeb0f84 commit 587c7c5

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

apps/web/app/api/upload/[...route]/multipart.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import {
55
import { updateIfDefined } from "@cap/database";
66
import * as Db from "@cap/database/schema";
77
import { serverEnv } from "@cap/env";
8+
import { HttpApiError } from "@effect/platform";
89
import {
910
AwsCredentials,
1011
Database,
1112
provideOptionalAuth,
1213
S3Buckets,
1314
Videos,
15+
VideosRepo,
1416
} from "@cap/web-backend";
1517
import { CurrentUser, Video } from "@cap/web-domain";
1618
import { zValidator } from "@hono/zod-validator";
@@ -52,11 +54,14 @@ app.post(
5254
const videoId = Video.VideoId.make(videoIdRaw);
5355

5456
const resp = await Effect.gen(function* () {
55-
const videos = yield* Videos;
57+
const user = yield* CurrentUser;
58+
const repo = yield* VideosRepo;
5659
const db = yield* Database;
5760

58-
const video = yield* videos.getByIdForOwner(videoId);
61+
const video = yield* repo.getById(videoId);
5962
if (Option.isNone(video)) return yield* new Video.NotFoundError();
63+
if (video.value[0].ownerId !== user.id)
64+
return yield* new HttpApiError.Unauthorized();
6065

6166
yield* db.use((db) =>
6267
db
@@ -71,6 +76,11 @@ app.post(
7176
if (e._tag === "VideoNotFoundError")
7277
return Effect.succeed<Response>(c.text("Video not found", 404));
7378

79+
if (e._tag === "Unauthorized")
80+
return Effect.succeed<Response>(
81+
c.text("User not authenticated", 401),
82+
);
83+
7484
return Effect.succeed<Response>(
7585
c.json({ error: "Error initiating multipart upload" }, 500),
7686
);
@@ -471,10 +481,6 @@ app.post(
471481
);
472482
}),
473483
);
474-
}).pipe(
475-
provideOptionalAuth,
476-
Effect.provideService(CurrentUser, user),
477-
runPromise,
478-
);
484+
}).pipe(Effect.provideService(CurrentUser, user), runPromise);
479485
},
480486
);

apps/web/lib/server.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ import {
1414
Videos,
1515
VideosPolicy,
1616
Workflows,
17+
VideosRepo,
1718
} from "@cap/web-backend";
1819
import { type HttpAuthMiddleware, Video } from "@cap/web-domain";
1920
import {
2021
FetchHttpClient,
2122
Headers,
2223
type HttpApi,
2324
HttpApiBuilder,
24-
HttpApiClient,
2525
HttpMiddleware,
2626
HttpServer,
2727
} from "@effect/platform";
28-
import { RpcClient, RpcMessage, RpcMiddleware } from "@effect/rpc";
28+
import { RpcClient, RpcMiddleware } from "@effect/rpc";
2929
import {
3030
Cause,
3131
Config,
@@ -100,6 +100,7 @@ export const Dependencies = Layer.mergeAll(
100100
S3Buckets.Default,
101101
Videos.Default,
102102
VideosPolicy.Default,
103+
VideosRepo.Default,
103104
Folders.Default,
104105
SpacesPolicy.Default,
105106
OrganisationsPolicy.Default,

packages/web-backend/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ export { Spaces } from "./Spaces/index.ts";
1111
export { SpacesPolicy } from "./Spaces/SpacesPolicy.ts";
1212
export { Videos } from "./Videos/index.ts";
1313
export { VideosPolicy } from "./Videos/VideosPolicy.ts";
14+
export { VideosRepo } from "./Videos/VideosRepo.ts";
1415
export * as Workflows from "./Workflows.ts";

0 commit comments

Comments
 (0)