@@ -3,6 +3,7 @@ import Cors from "cors";
33import NodeCache from "node-cache" ;
44
55const imageCache = new NodeCache ( ) ;
6+ const userPhotosServer = "https://user-photos.codam.nl" ;
67
78const cors = Cors ( {
89 methods : [ 'POST' , 'GET' , 'HEAD' ] ,
@@ -29,9 +30,24 @@ function runMiddleware(
2930const images = async ( req : NextApiRequest , res : NextApiResponse ) => {
3031 await runMiddleware ( req , res , cors ) ;
3132
32- //TODO: add proxy for user-photos.codam.nl to make internal clustermap available externally (once it's behind a login wall)
33-
34- res . status ( 501 ) . json ( { error : "Not Implemented" } ) ;
33+ // Get the image from the cache if it exists
34+ const image = imageCache . get ( req . query . login as string ) ;
35+ if ( image ) {
36+ res . send ( image as Buffer ) ;
37+ } else {
38+ // Fetch the image from the user-photos server if it's not in the cache
39+ const response = await fetch ( `${ userPhotosServer } /${ req . query . login } /100` ) ;
40+ const data = await response . arrayBuffer ( ) ;
41+ const buffer = Buffer . from ( data ) ; // Convert to Node.JS buffer to make Next.js send the raw data back correctly
42+ if ( response . ok ) {
43+ console . log ( `Cache miss for ${ req . query . login } , fetching image from user-photos.codam.nl...` ) ;
44+ imageCache . set ( req . query . login as string , buffer , 3600 ) ; // Cache the image for 1 hour
45+ res . status ( response . status ) . send ( buffer ) ;
46+ } else {
47+ console . log ( `Failed to fetch image for ${ req . query . login } from user-photos.codam.nl` ) ;
48+ res . status ( response . status ) . send ( buffer ) ;
49+ }
50+ }
3551} ;
3652
3753export default images ;
0 commit comments