portapack-mayhem/MayhemHub

Downloading firmware file from Github

Closed this issue · 1 comments

jLynx commented

Since we cannot download the file directly from GitHub because of the CORS issue, we will need to create a Cloudflare function that acts as a proxy for us to download the file. But we need to limit its scope so that it can only download from this specific repo.

Here is some example code to get started (Note this is for a worker, so will need to be updated to a function)

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  const url = new URL(request.url)

  // Define the URL for the file to be downloaded
  let fileUrl = 'https://example.com/file.zip' 

  // Modify this for the logic to determine your file URL
  // if(url.pathname.startsWith('/specific-path/')) 
  //    fileUrl = 'https://example.com/file2.zip'

  let response = await fetch(fileUrl, request)

  // You can modify the response here, like setting content-disposition to force a file download
  response = new Response(response.body, response)
  response.headers.set('Content-Disposition', `attachment; filename="yourdesiredfilename.zip"`)

  return response
}
jLynx commented

Done