Add pages/api/raw/[id].js
This commit is contained in:
@@ -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');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user