Skip to content

Commit 69d62e8

Browse files
committed
fix: add proxy server to user photos
to speed up load and to prevent high CPU usage on user-photos server for resizing photos
1 parent a74e651 commit 69d62e8

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

public/js/interactive_maps.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6259,9 +6259,9 @@ var $webbhuset$elm_json_decode$Json$Decode$Field$attempt = F3(
62596259
$elm$json$Json$Decode$maybe(
62606260
A2($elm$json$Json$Decode$field, fieldName, valueDecoder)));
62616261
});
6262-
var $author$project$Clustermap$user_photos_domain = 'https://user-photos.codam.nl/';
6262+
var $author$project$Clustermap$user_photos_domain = '/api/images/';
62636263
var $author$project$Clustermap$getInitialImage = function (username) {
6264-
return _Utils_ap($author$project$Clustermap$user_photos_domain, username);
6264+
return $author$project$Clustermap$user_photos_domain + ('?login=' + username);
62656265
};
62666266
var $webbhuset$elm_json_decode$Json$Decode$Field$require = F3(
62676267
function (fieldName, valueDecoder, continuation) {

src/elm/clustermap/Clustermap.elm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import Asset exposing (image)
2222
import Html.Attributes exposing (classList)
2323

2424
user_photos_domain : String
25-
user_photos_domain = "https://user-photos.codam.nl/"
25+
user_photos_domain = "/api/images/"
2626

2727
-- MODEL
2828

@@ -555,7 +555,7 @@ compareSessionUsername username session =
555555

556556
getInitialImage : String -> String
557557
getInitialImage username =
558-
user_photos_domain ++ username ++ "/100"
558+
user_photos_domain ++ "?login=" ++ username
559559

560560

561561
sessionListDecoder : (List Session) -> Decoder (List Session)

src/pages/api/images.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Cors from "cors";
33
import NodeCache from "node-cache";
44

55
const imageCache = new NodeCache();
6+
const userPhotosServer = "https://user-photos.codam.nl";
67

78
const cors = Cors({
89
methods: ['POST', 'GET', 'HEAD'],
@@ -29,9 +30,24 @@ function runMiddleware(
2930
const 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

3753
export default images;

0 commit comments

Comments
 (0)