Getting MIME Type from Headers in Effect

Hello community, I just started exploring Effect. I got a question regarding the a function I am trying to convert:

I am able to download the file and handle the retry/timeout.

One of the items that I am trying to do and cannot figure out is how to read headers to get mime time to save the file with proper file extension. I would appreciate if you could guide me! Thanks!

const downloadFile = ({
  id,
  url,
  dir
}: {
  readonly id: string;
  readonly url: URL;
  readonly dir: string;
}) =>
  Effect.gen(function* (_) {
    const fs = yield* _(Fs.FileSystem);
    const filePath = path.join(dir, id);
    yield* fs.makeDirectory(dir, { recursive: true });
    yield* Effect.log('downloadFile');
    const retryPolicy = Schedule.exponential(1000).pipe(
      Schedule.compose(Schedule.recurs(3)),
    );
    return yield* Http.request.get(url).pipe(
      Http.client.fetchOk,
      Effect.timeout(5000),
      Effect.tap((_) => Effect.log(`response.statusCode: ${_.status}`)),
      Effect.tap((_) => Effect.log(`response.headers: ${JSON.stringify(_.headers)}`)),
      Http.response.arrayBuffer,
      Effect.andThen((_) => fs.writeFile(filePath, Buffer.from(_))),
      Effect.retry(retryPolicy),
      Effect.withSpan('downloadFile', { attributes: { id } })
    );
  });
Was this page helpful?