←
@@ -86,11 +121,34 @@ async function generateSite() {
async function serveStaticSite(port = 3000) {
const server = http.createServer(async (req, res) => {
try {
+ // Parse URL and query parameters
+ const url = new URL(req.url, `http://localhost:${port}`);
+ const quality = url.searchParams.get('quality');
+ const width = url.searchParams.get('width');
+
// Convert URL to filesystem path
let filePath;
- if (req.url.startsWith('/images/')) {
+ if (url.pathname.startsWith('/images/')) {
// Serve directly from images folder
- filePath = req.url.slice(1); // Remove leading slash
+ filePath = url.pathname.slice(1); // Remove leading slash
+
+ // If quality parameter is present, serve a resized version
+ if (quality && width && (filePath.endsWith('.jpg') || filePath.endsWith('.jpeg') || filePath.endsWith('.png'))) {
+ try {
+ const Sharp = (await import('sharp')).default;
+ const image = await Sharp(filePath)
+ .resize(parseInt(width))
+ .jpeg({ quality: parseInt(quality) })
+ .toBuffer();
+
+ res.writeHead(200, { 'Content-Type': 'image/jpeg' });
+ res.end(image);
+ return;
+ } catch (error) {
+ console.error('Error processing image:', error);
+ // Fall through to serve original image
+ }
+ }
} else {
// Serve from public folder
filePath = path.join('public', req.url === '/' ? 'index.html' : req.url);
diff --git a/package.json b/package.json
index 4ead12a..35504ad 100644
--- a/package.json
+++ b/package.json
@@ -9,6 +9,7 @@
},
"dependencies": {
"axios": "^1.7.9",
- "node-fetch": "^3.3.2"
+ "node-fetch": "^3.3.2",
+ "sharp": "^0.32.0"
}
}