From da3e3a12db81f04ec6808acd12eb64af870f66af Mon Sep 17 00:00:00 2001 From: Teh PeGaSuS Date: Sun, 26 Apr 2026 21:40:37 +0200 Subject: [PATCH] Add pages/api/raw/[id].js --- pages/api/raw/[id].js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 pages/api/raw/[id].js diff --git a/pages/api/raw/[id].js b/pages/api/raw/[id].js new file mode 100644 index 0000000..2dcabb9 --- /dev/null +++ b/pages/api/raw/[id].js @@ -0,0 +1,38 @@ +import db from '../../../lib/db'; + +export default function handler(req, res) { + const { id } = req.query; + + try { + const paste = db.prepare('SELECT * FROM pastes WHERE id = ?').get(id); + + if (!paste) { + return res.status(404).send('Error: Paste not found\n'); + } + + if (paste.expires_at && new Date(paste.expires_at) < new Date()) { + db.prepare('DELETE FROM pastes WHERE id = ?').run(id); + return res.status(404).send('Error: Paste has expired\n'); + } + + if (paste.expiry_type === 'burn') { + if (paste.view_count === 0) { + db.prepare('UPDATE pastes SET view_count = 1 WHERE id = ?').run(id); + } else { + db.prepare('DELETE FROM pastes WHERE id = ?').run(id); + } + } + + res.setHeader('Content-Type', 'text/plain; charset=utf-8'); + res.setHeader('Cache-Control', 'no-store, max-age=0'); + + // Ensure the content ends with exactly one newline + // We trim any existing trailing whitespace/newlines first, then add one back. + const cleanContent = paste.content.replace(/\n+$/, ''); + + return res.status(200).send(`${cleanContent}\n`); + } catch (error) { + console.error('Raw endpoint error:', error); + return res.status(500).send('Internal Server Error\n'); + } +}