@@ -7,6 +7,24 @@ const corsHeaders = {
77} ;
88
99export 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 ;
0 commit comments