Skip to content

Commit

Permalink
Merge pull request #26 from portapack-mayhem/firmware_cors
Browse files Browse the repository at this point in the history
Firmware cors
  • Loading branch information
jLynx authored Jan 11, 2024
2 parents c2a662d + a3e0593 commit bb13f0f
Show file tree
Hide file tree
Showing 2 changed files with 205 additions and 200 deletions.
46 changes: 27 additions & 19 deletions functions/api/fetch_nightly_firmware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,48 @@ const corsHeaders = {
"Access-Control-Allow-Methods": "GET,HEAD,POST,OPTIONS,DELETE",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Max-Age": "86400",
};
}

export const onRequestGet: PagesFunction = async (context) => {
let apiUrl =
"https://api.github.com/repos/portapack-mayhem/mayhem-firmware/releases";
"https://api.github.com/repos/portapack-mayhem/mayhem-firmware/releases"

let apiResponse = await fetch(apiUrl, {
method: "GET",
headers: { "User-Agent": "portapack-mayhem" },
});
})

if (!apiResponse.ok) {
throw new Error(`HTTP error! status: ${apiResponse.status}`);
throw new Error(`HTTP error! status: ${apiResponse.status}`)
}

let apiData: any = await apiResponse.json();
let apiData: any = await apiResponse.json()

// assuming you want to fetch the first release data
let browser_download_url = apiData[0].assets.find((asset: any) => {
const assetName: string = asset["name"];
return assetName.includes(".ppfw.tar");
}).browser_download_url;
console.log(browser_download_url);
const assetName: string = asset["name"]
return assetName.includes(".ppfw.tar")
}).browser_download_url
console.log(browser_download_url)

let fileUrl = browser_download_url;
const 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)

let fileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);
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}`
)

return new Response(response.body, {
headers: {
...corsHeaders,
"Content-Disposition": `attachment; filename="${fileName}"`,
},
});
};
return proxyResponse
}
Loading

0 comments on commit bb13f0f

Please sign in to comment.