1import * as Effect from "effect/Effect";
2import * as Layer from "effect/Layer";
3import type { Bucket } from "./Bucket.ts";
4import { GetObject, type GetObjectRequest } from "./GetObject.ts";
5import { grantOnBucket } from "./ObjectHttp.ts";
6import { makeObjectMedia } from "./ObjectMedia.ts";
7
8/**
9 * HTTP implementation of {@link GetObject}.
10 *
11 * @layer
12 * @provides GCP.Storage.GetObject
13 */
14export const GetObjectHttp = Layer.effect(
15 GetObject,
16 Effect.gen(function* () {
17 const media = yield* makeObjectMedia;
18 return Effect.fn(function* (bucket: Bucket) {
19 yield* grantOnBucket(
20 "GCP.Storage.GetObject",
21 bucket,
22 "roles/storage.objectViewer",
23 );
24 const bucketName = yield* bucket.bucketName;
25 return Effect.fn(`GCP.Storage.GetObject(${bucket.LogicalId})`)(function* (
26 request: GetObjectRequest,
27 ) {
28 return yield* media.download({
29 bucket: yield* bucketName,
30 object: request.object,
31 generation: request.generation,
32 });
33 });
34 });
35 }),
36);