From cc3f0b03aa9646bce82a7ca144114a65b726f55e Mon Sep 17 00:00:00 2001 From: jLynx Date: Sun, 25 Feb 2024 16:02:00 +1300 Subject: [PATCH] Fixed stable API --- functions/api/fetch_stable_firmware.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/functions/api/fetch_stable_firmware.ts b/functions/api/fetch_stable_firmware.ts index 1961cfe..97aa051 100644 --- a/functions/api/fetch_stable_firmware.ts +++ b/functions/api/fetch_stable_firmware.ts @@ -28,17 +28,23 @@ export const onRequestGet: PagesFunction = async (context) => { console.log(browser_download_url); let fileUrl = browser_download_url; + const resourceResponse = await fetch(fileUrl); - let response = await fetch(fileUrl, context.request); + if (!resourceResponse.ok) { + throw new Error(`HTTP error! status: ${resourceResponse.status}`); + } + const resourceBody = await resourceResponse.body; let fileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1); - // You can modify the response here, like setting content-disposition to force a file download - response = new Response(response.body, response); - response.headers.set( + let proxyResponse = new Response(resourceBody, resourceResponse); + Object.entries(corsHeaders).forEach(([key, value]) => { + proxyResponse.headers.set(key, value); + }); + proxyResponse.headers.set( "Content-Disposition", - `attachment; filename="${fileName}"` + `attachment; filename="${fileName}` ); - return response; + return proxyResponse; };