Skip to content

Commit

Permalink
fix: add proxy server to user photos
Browse files Browse the repository at this point in the history
to speed up load and to prevent high CPU usage on user-photos server for resizing photos
  • Loading branch information
FreekBes committed Feb 21, 2024
1 parent a74e651 commit 69d62e8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
4 changes: 2 additions & 2 deletions public/js/interactive_maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -6259,9 +6259,9 @@ var $webbhuset$elm_json_decode$Json$Decode$Field$attempt = F3(
$elm$json$Json$Decode$maybe(
A2($elm$json$Json$Decode$field, fieldName, valueDecoder)));
});
var $author$project$Clustermap$user_photos_domain = 'https://user-photos.codam.nl/';
var $author$project$Clustermap$user_photos_domain = '/api/images/';
var $author$project$Clustermap$getInitialImage = function (username) {
return _Utils_ap($author$project$Clustermap$user_photos_domain, username);
return $author$project$Clustermap$user_photos_domain + ('?login=' + username);
};
var $webbhuset$elm_json_decode$Json$Decode$Field$require = F3(
function (fieldName, valueDecoder, continuation) {
Expand Down
4 changes: 2 additions & 2 deletions src/elm/clustermap/Clustermap.elm
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Asset exposing (image)
import Html.Attributes exposing (classList)

user_photos_domain : String
user_photos_domain = "https://user-photos.codam.nl/"
user_photos_domain = "/api/images/"

-- MODEL

Expand Down Expand Up @@ -555,7 +555,7 @@ compareSessionUsername username session =

getInitialImage : String -> String
getInitialImage username =
user_photos_domain ++ username ++ "/100"
user_photos_domain ++ "?login=" ++ username


sessionListDecoder : (List Session) -> Decoder (List Session)
Expand Down
22 changes: 19 additions & 3 deletions src/pages/api/images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Cors from "cors";
import NodeCache from "node-cache";

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

const cors = Cors({
methods: ['POST', 'GET', 'HEAD'],
Expand All @@ -29,9 +30,24 @@ function runMiddleware(
const images = async (req: NextApiRequest, res: NextApiResponse) => {
await runMiddleware(req, res, cors);

//TODO: add proxy for user-photos.codam.nl to make internal clustermap available externally (once it's behind a login wall)

res.status(501).json({ error: "Not Implemented" });
// Get the image from the cache if it exists
const image = imageCache.get(req.query.login as string);
if (image) {
res.send(image as Buffer);
} else {
// Fetch the image from the user-photos server if it's not in the cache
const response = await fetch(`${userPhotosServer}/${req.query.login}/100`);
const data = await response.arrayBuffer();
const buffer = Buffer.from(data); // Convert to Node.JS buffer to make Next.js send the raw data back correctly
if (response.ok) {
console.log(`Cache miss for ${req.query.login}, fetching image from user-photos.codam.nl...`);
imageCache.set(req.query.login as string, buffer, 3600); // Cache the image for 1 hour
res.status(response.status).send(buffer);
} else {
console.log(`Failed to fetch image for ${req.query.login} from user-photos.codam.nl`);
res.status(response.status).send(buffer);
}
}
};

export default images;

0 comments on commit 69d62e8

Please sign in to comment.