Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// src/server.ts

import express, { Request, Response } from "express";
import axios from "axios";
import sharp from "sharp";
Expand All @@ -19,15 +21,28 @@ app.get("/", (req, res) => {
app.get("/api/framed-avatar/:username", async (req: Request, res: Response) => {
try {
const username = req.params.username;
const theme = (req.query.theme as string) || "base"; // Default to base theme for testing
const size = Math.max(64, Math.min(Number(req.query.size ?? 256), 1024)); // Limit size between 64 and 1024
const theme = (req.query.theme as string) || "base";

console.log(`Fetching avatar for username=${username}, theme=${theme}, size=${size}`);
// --- START OF MODIFICATIONS ---

// 1. Get the 'size' parameter as a string, with a default value.
const sizeStr = (req.query.size as string) ?? "256";

if (isNaN(size) || size <= 0 || size > 1024) {
return res.status(400).json({ error: "Invalid size parameter" });
// 2. Validate the string to ensure it only contains digits.
if (!/^\d+$/.test(sizeStr)) {
return res.status(400).json({
error: "Bad Request",
message: "The 'size' parameter must be a valid integer.",
});
}

// 3. Safely parse the string to a number and clamp it to the allowed range.
const size = Math.max(64, Math.min(parseInt(sizeStr, 10), 1024));

// --- END OF MODIFICATIONS ---

console.log(`Fetching avatar for username=${username}, theme=${theme}, size=${size}`);

// 1. Fetch GitHub avatar
const avatarUrl = `https://github.com/${username}.png?size=${size}`;
const avatarResponse = await axios.get(avatarUrl, { responseType: "arraybuffer" });
Expand Down Expand Up @@ -81,10 +96,15 @@ app.get("/api/framed-avatar/:username", async (req: Request, res: Response) => {
res.send(finalImage);
} catch (error) {
console.error("Error creating framed avatar:", error);
// Add a check for specific errors, like user not found from GitHub
if (axios.isAxiosError(error) && error.response?.status === 404) {
return res.status(404).json({ error: `GitHub user '${req.params.username}' not found.` });
}
res.status(500).json({ error: "Something went wrong." });
}
});


/**
* GET /api/themes
* Lists all available themes + metadata
Expand Down Expand Up @@ -112,7 +132,8 @@ app.get("/api/themes", (req: Request, res: Response) => {
}
});


// Start server
app.listen(PORT, () => {
console.log(`🚀 Server running at http://localhost:${PORT}`);
});
});