@@ -6,69 +6,69 @@ import sharp from 'sharp';
66// Add any http handler here (get, push , delete etc., and middleware as needed)
77
88function handler ( pathName : string ) {
9- return [
10- http . get ( `/${ pathName } ` , ( ) => {
11- return HttpResponse . text (
12- `<body style="background-color: #383838; color:white">
9+ return [
10+ http . get ( `/${ pathName } ` , ( ) => {
11+ return HttpResponse . text (
12+ `<body style="background-color: #383838; color:white">
1313 <div style="text-align:center; padding:50px 0px 0px 0px">
1414 <h4>Access images stored in the src/resources/images folder using the format: <span style="color:red">api/images/{filename}</span></h4>
1515 <h4>Example: api/images/placeholder.png</h4>
1616 </div>
1717 </body>
1818 ` ,
19- {
20- headers : {
21- 'Content-Type' : 'text/html' ,
22- 'Access-Control-Allow-Origin' : '*' ,
23- } ,
24- } ,
25- ) ;
26- } ) ,
27- http . get ( `/${ pathName } /:imageID` , async ( { request } ) => {
28- const url = new URL ( request . url ) ;
29- const width = url . searchParams . get ( 'width' ) ;
30- const height = url . searchParams . get ( 'height' ) ;
31- console . log ( `height ${ height } and width ${ width } ` ) ;
32- const params = url . pathname . split ( '/' ) . pop ( ) ;
19+ {
20+ headers : {
21+ 'Content-Type' : 'text/html' ,
22+ 'Access-Control-Allow-Origin' : '*' ,
23+ } ,
24+ } ,
25+ ) ;
26+ } ) ,
27+ http . get ( `/${ pathName } /:imageID` , async ( { request } ) => {
28+ const url = new URL ( request . url ) ;
29+ const width = url . searchParams . get ( 'width' ) ;
30+ const height = url . searchParams . get ( 'height' ) ;
31+ console . log ( `height ${ height } and width ${ width } ` ) ;
32+ const params = url . pathname . split ( '/' ) . pop ( ) ;
3333
34- console . log ( `starting ${ pathName } ` ) ;
34+ console . log ( `starting ${ pathName } ` ) ;
3535
36- try {
37- // Convert width and height to integers, if provided
38- const resizeOptions : sharp . ResizeOptions = { } ;
39- if ( width && height )
40- resizeOptions . width = Number . parseInt ( width , 10 ) ;
41- if ( height && width )
42- resizeOptions . height = Number . parseInt ( height , 10 ) ;
36+ try {
37+ // Convert width and height to integers, if provided
38+ const resizeOptions : sharp . ResizeOptions = { } ;
39+ if ( width && height )
40+ resizeOptions . width = Number . parseInt ( width , 10 ) ;
41+ if ( height && width )
42+ resizeOptions . height = Number . parseInt ( height , 10 ) ;
4343
44- const inputBuffer = fs . readFileSync (
45- path . resolve ( `./src/resources/images/${ params } ` ) ,
46- ) ;
47- const resizedImageBuffer = await sharp ( inputBuffer )
48- . resize ( resizeOptions ) // Only applies resize if width/height are present
49- . png ( )
50- . toBuffer ( ) ;
44+ const inputBuffer = fs . readFileSync (
45+ path . resolve ( `./src/resources/images/${ params } ` ) ,
46+ ) ;
47+ const resizedImageBuffer = await sharp ( inputBuffer )
48+ . resize ( resizeOptions ) // Only applies resize if width/height are present
49+ . png ( )
50+ . toBuffer ( ) ;
5151
52- return HttpResponse . arrayBuffer ( resizedImageBuffer , {
53- headers : {
54- 'Content-Type' : 'image/png' ,
55- 'Access-Control-Allow-Origin' : '*' ,
56- } ,
57- } ) ;
58- } catch {
59- return HttpResponse . text (
60- 'Error: File not found. Check file is in the src/resources/images folder' ,
61- {
62- status : 404 ,
63- headers : {
64- 'Content-Type' : 'text/html' ,
65- 'Access-Control-Allow-Origin' : '*' ,
66- } ,
67- } ,
68- ) ;
69- }
70- } ) ,
71- ] ;
52+ return HttpResponse . arrayBuffer ( resizedImageBuffer , {
53+ headers : {
54+ 'Content-Type' : 'image/png' ,
55+ 'Access-Control-Allow-Origin' : '*' ,
56+ } ,
57+ } ) ;
58+ } catch {
59+ return HttpResponse . text (
60+ 'Error: File not found. Check file is in the src/resources/images folder' ,
61+ {
62+ status : 404 ,
63+ headers : {
64+ 'Content-Type' : 'text/html' ,
65+ 'Access-Control-Allow-Origin' : '*' ,
66+ } ,
67+ } ,
68+ ) ;
69+ }
70+ } ) ,
71+ ] ;
7272}
7373
7474export default handler ;
0 commit comments