File size: 714 Bytes
2cae2a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { lookup } from "mime-types"

export const downloadFileAsBase64 = async (remoteUrl: string): Promise<string> => {
  // const controller = new AbortController()

  // download the file
  const response = await fetch(remoteUrl, {
    // signal: controller.signal
  })

  // get as Buffer
  const arrayBuffer = await response.arrayBuffer()
  const buffer = Buffer.from(arrayBuffer)

  // convert it to base64
  const base64 = buffer.toString('base64')


  const res = lookup(remoteUrl)
  let contentType = res.toString()
  if (typeof res === "boolean" && res === false) {
    contentType = response.headers.get('content-type')
  }

  const assetUrl = `data:${contentType};base64,${base64}`
  return assetUrl
};