| 1 | import * as Lambda from "@/AWS/Lambda"; |
| 2 | import * as S3 from "@/AWS/S3"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 5 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 6 | |
| 7 | export class PresignGetOnlyTestFunction extends Lambda.Function<PresignGetOnlyTestFunction>()( |
| 8 | "PresignGetOnlyTestFunction", |
| 9 | ) {} |
| 10 | |
| 11 | export default PresignGetOnlyTestFunction.make( |
| 12 | { main: import.meta.url, functionUrl: true }, |
| 13 | Effect.gen(function* () { |
| 14 | const bucket = yield* S3.Bucket("PresignGetOnlyBucket", { |
| 15 | forceDestroy: true, |
| 16 | versioning: "Enabled", |
| 17 | }); |
| 18 | const presignGet = yield* S3.PresignGetObject(bucket); |
| 19 | const bucketName = yield* bucket.bucketName; |
| 20 | const bucketArn = yield* bucket.bucketArn; |
| 21 | |
| 22 | return { |
| 23 | fetch: Effect.gen(function* () { |
| 24 | const request = yield* HttpServerRequest; |
| 25 | const url = yield* Effect.sync(() => new URL(request.originalUrl)); |
| 26 | if (url.pathname === "/info") { |
| 27 | return yield* HttpServerResponse.json({ |
| 28 | bucketName: yield* bucketName, |
| 29 | bucketArn: yield* bucketArn, |
| 30 | }); |
| 31 | } |
| 32 | const key = url.searchParams.get("key"); |
| 33 | if (!key) |
| 34 | return HttpServerResponse.text("Missing key", { status: 400 }); |
| 35 | return yield* HttpServerResponse.json({ |
| 36 | url: yield* presignGet({ |
| 37 | key, |
| 38 | versionId: url.searchParams.get("versionId") ?? undefined, |
| 39 | contentType: url.searchParams.get("contentType") ?? undefined, |
| 40 | expiresIn: url.searchParams.has("expiresIn") |
| 41 | ? Number(url.searchParams.get("expiresIn")) |
| 42 | : undefined, |
| 43 | }), |
| 44 | }); |
| 45 | }).pipe(Effect.orDie), |
| 46 | }; |
| 47 | }).pipe(Effect.provide(S3.PresignGetObjectHttp)), |
| 48 | ); |