Compare commits

..

2 Commits

Author SHA1 Message Date
ImBenji
d4b1dffd74 Add cache clearing function and invoke on startup 2025-12-31 10:36:21 +00:00
ImBenji
084130feb7 Hide tweet image container and update image URL to null in quote data 2025-12-31 09:01:19 +00:00
3 changed files with 21 additions and 8 deletions

View File

@@ -28,7 +28,6 @@ curl -X POST https://quotes.imbenji.net/generate \
"username": "@geofftech", "username": "@geofftech",
"avatarUrl": "https://example.com/avatar.jpg", "avatarUrl": "https://example.com/avatar.jpg",
"text": "Does anyone else find it immensely satisfying when you turn a pocket inside out and get rid of the crumbs and fluff stuck in the bottom.", "text": "Does anyone else find it immensely satisfying when you turn a pocket inside out and get rid of the crumbs and fluff stuck in the bottom.",
"imageUrl": null,
"timestamp": 1499766270 "timestamp": 1499766270
}' --output quote.png }' --output quote.png
``` ```

22
api.js
View File

@@ -44,6 +44,20 @@ function cacheImage(config, imageBuffer) {
fs.writeFileSync(cachePath, imageBuffer); fs.writeFileSync(cachePath, imageBuffer);
} }
function clearCache() {
if (!fs.existsSync(CACHE_DIR)) {
return;
}
const files = fs.readdirSync(CACHE_DIR);
files.forEach(file => {
const filePath = path.join(CACHE_DIR, file);
fs.unlinkSync(filePath);
});
console.log(`Cleared ${files.length} cached image(s)`);
}
function cleanupOldCache() { function cleanupOldCache() {
const ONE_DAY = 24 * 60 * 60 * 1000; // 24 hours in milliseconds const ONE_DAY = 24 * 60 * 60 * 1000; // 24 hours in milliseconds
const now = Date.now(); const now = Date.now();
@@ -190,17 +204,17 @@ app.get('/health', (req, res) => {
res.status(200).json({ status: 'ok' }); res.status(200).json({ status: 'ok' });
}); });
// Clear all cache on startup
clearCache();
// Run cleanup every hour // Run cleanup every hour
setInterval(() => { setInterval(() => {
cleanupOldCache(); cleanupOldCache();
}, 60 * 60 * 1000); }, 60 * 60 * 1000);
// Run cleanup on startup
cleanupOldCache();
app.listen(PORT, () => { app.listen(PORT, () => {
console.log(`Quote generator API running on http://localhost:${PORT}`); console.log(`Quote generator API running on http://localhost:${PORT}`);
console.log(`GET: http://localhost:${PORT}/generate?text=Hello&displayName=Test&username=@test&timestamp=1735574400`); console.log(`GET: http://localhost:${PORT}/generate?text=Hello&displayName=Test&username=@test&timestamp=1735574400`);
console.log(`POST: http://localhost:${PORT}/generate`); console.log(`POST: http://localhost:${PORT}/generate`);
console.log(`Cache cleanup runs every hour, images older than 24h are removed`); console.log(`Cache cleared on startup, cleanup runs every hour`);
}); });

View File

@@ -197,8 +197,8 @@
<div class="tweet-text" id="tweetText">omg i brought these watches home because none of them worked but i thought id hang onto them and my dad fixed and polished them all for me 😭 i love him so much</div> <div class="tweet-text" id="tweetText">omg i brought these watches home because none of them worked but i thought id hang onto them and my dad fixed and polished them all for me 😭 i love him so much</div>
<div class="tweet-image-container" id="imageContainer"> <div class="tweet-image-container" id="imageContainer" style="display: none;">
<img src="https://pbs.twimg.com/media/G9Wtk1xWcAA-WMZ?format=jpg&name=large" alt="Image" class="tweet-image" id="tweetImage"> <img src="" alt="Image" class="tweet-image" id="tweetImage">
</div> </div>
<div class="tweet-metadata"> <div class="tweet-metadata">
@@ -254,7 +254,7 @@
username: "@katiopolis", username: "@katiopolis",
avatarUrl: "https://pbs.twimg.com/profile_images/2004569144893554688/KaYjqylC_x96.jpg", avatarUrl: "https://pbs.twimg.com/profile_images/2004569144893554688/KaYjqylC_x96.jpg",
text: "omg i brought these watches home because none of them worked but i thought id hang onto them and my dad fixed and polished them all for me 😭 i love him so much", text: "omg i brought these watches home because none of them worked but i thought id hang onto them and my dad fixed and polished them all for me 😭 i love him so much",
imageUrl: "https://pbs.twimg.com/media/G9Wtk1xWcAA-WMZ?format=jpg&name=large", imageUrl: null,
timestamp: "5:58 PM · Dec 29, 2025", timestamp: "5:58 PM · Dec 29, 2025",
viewsCount: "1.1M" viewsCount: "1.1M"
}; };