Skip to content

Commit 50c5e8c

Browse files
committed
Added support for new API firmware
1 parent 6477a03 commit 50c5e8c

File tree

2 files changed

+107
-12
lines changed

2 files changed

+107
-12
lines changed

functions/api/fetch_nightly_firmware.ts

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,24 @@ const corsHeaders = {
77
};
88

99
export const onRequestGet: PagesFunction = async (context) => {
10+
// Get device type from query parameter, default to 'hackrf'
11+
const url = new URL(context.request.url);
12+
const deviceType = url.searchParams.get("device") || "hackrf";
13+
14+
// Validate device type
15+
const validDevices = ["hackrf", "hpro", "portarf"];
16+
if (!validDevices.includes(deviceType.toLowerCase())) {
17+
return new Response(
18+
JSON.stringify({
19+
error: `Invalid device type. Must be one of: ${validDevices.join(", ")}`,
20+
}),
21+
{
22+
status: 400,
23+
headers: { ...corsHeaders, "Content-Type": "application/json" },
24+
},
25+
);
26+
}
27+
1028
let apiUrl =
1129
"https://api.github.com/repos/portapack-mayhem/mayhem-firmware/releases";
1230

@@ -21,11 +39,40 @@ export const onRequestGet: PagesFunction = async (context) => {
2139

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

24-
// assuming you want to fetch the first release data
25-
let browser_download_url = apiData[0].assets.find((asset: any) => {
26-
const assetName: string = asset["name"];
27-
return assetName.includes(".ppfw.tar");
28-
}).browser_download_url;
42+
// Find the asset that matches the device type
43+
let asset = apiData[0].assets.find((asset: any) => {
44+
const assetName: string = asset["name"].toLowerCase();
45+
return (
46+
assetName.includes(".ppfw.tar") &&
47+
assetName.includes(deviceType.toLowerCase())
48+
);
49+
});
50+
51+
// Fallback: if no device-specific asset found, just look for any .ppfw.tar file
52+
if (!asset) {
53+
console.log(
54+
`No device-specific firmware found for ${deviceType}, falling back to generic .ppfw.tar`,
55+
);
56+
asset = apiData[0].assets.find((asset: any) => {
57+
const assetName: string = asset["name"];
58+
return assetName.includes(".ppfw.tar");
59+
});
60+
}
61+
62+
// If still no asset found, return error
63+
if (!asset) {
64+
return new Response(
65+
JSON.stringify({
66+
error: `No firmware found`,
67+
}),
68+
{
69+
status: 404,
70+
headers: { ...corsHeaders, "Content-Type": "application/json" },
71+
},
72+
);
73+
}
74+
75+
const browser_download_url = asset.browser_download_url;
2976
console.log(browser_download_url);
3077

3178
const fileUrl = browser_download_url;
@@ -44,7 +91,7 @@ export const onRequestGet: PagesFunction = async (context) => {
4491
});
4592
proxyResponse.headers.set(
4693
"Content-Disposition",
47-
`attachment; filename="${fileName}`
94+
`attachment; filename="${fileName}"`,
4895
);
4996

5097
return proxyResponse;

functions/api/fetch_stable_firmware.ts

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,24 @@ const corsHeaders = {
77
};
88

99
export const onRequestGet: PagesFunction = async (context) => {
10+
// Get device type from query parameter, default to 'hackrf'
11+
const url = new URL(context.request.url);
12+
const deviceType = url.searchParams.get("device") || "hackrf";
13+
14+
// Validate device type
15+
const validDevices = ["hackrf", "hpro", "portarf"];
16+
if (!validDevices.includes(deviceType.toLowerCase())) {
17+
return new Response(
18+
JSON.stringify({
19+
error: `Invalid device type. Must be one of: ${validDevices.join(", ")}`,
20+
}),
21+
{
22+
status: 400,
23+
headers: { ...corsHeaders, "Content-Type": "application/json" },
24+
},
25+
);
26+
}
27+
1028
let apiUrl =
1129
"https://api.github.com/repos/portapack-mayhem/mayhem-firmware/releases/latest";
1230

@@ -20,11 +38,41 @@ export const onRequestGet: PagesFunction = async (context) => {
2038
}
2139

2240
let apiData: any = await apiResponse.json();
23-
// assuming you want to fetch the first release data
24-
let browser_download_url = apiData.assets.find((asset: any) => {
25-
const assetName: string = asset["name"];
26-
return assetName.includes(".ppfw.tar");
27-
}).browser_download_url;
41+
42+
// Find the asset that matches the device type
43+
let asset = apiData.assets.find((asset: any) => {
44+
const assetName: string = asset["name"].toLowerCase();
45+
return (
46+
assetName.includes(".ppfw.tar") &&
47+
assetName.includes(deviceType.toLowerCase())
48+
);
49+
});
50+
51+
// Fallback: if no device-specific asset found, just look for any .ppfw.tar file
52+
if (!asset) {
53+
console.log(
54+
`No device-specific firmware found for ${deviceType}, falling back to generic .ppfw.tar`,
55+
);
56+
asset = apiData.assets.find((asset: any) => {
57+
const assetName: string = asset["name"];
58+
return assetName.includes(".ppfw.tar");
59+
});
60+
}
61+
62+
// If still no asset found, return error
63+
if (!asset) {
64+
return new Response(
65+
JSON.stringify({
66+
error: `No firmware found`,
67+
}),
68+
{
69+
status: 404,
70+
headers: { ...corsHeaders, "Content-Type": "application/json" },
71+
},
72+
);
73+
}
74+
75+
const browser_download_url = asset.browser_download_url;
2876
console.log(browser_download_url);
2977

3078
let fileUrl = browser_download_url;
@@ -43,7 +91,7 @@ export const onRequestGet: PagesFunction = async (context) => {
4391
});
4492
proxyResponse.headers.set(
4593
"Content-Disposition",
46-
`attachment; filename="${fileName}`
94+
`attachment; filename="${fileName}"`,
4795
);
4896

4997
return proxyResponse;

0 commit comments

Comments
 (0)