Improve caching and argument handing

pull/20/head
Joe Biellik 4 years ago
parent fcfea334b5
commit 0bde009573

@ -5,31 +5,40 @@ module.exports = {
async view(ctx) { async view(ctx) {
try { try {
let paste = await Paste.findById(ctx.params.id).exec(); let paste = await Paste.findById(ctx.params.id).exec();
let lang = Object.keys(ctx.query)[0]; let lang = (Object.keys(ctx.query)[0] || '').toLowerCase();
ctx.set('Cache-Control', 'public');
ctx.set('Expires', paste.expiresAt.toUTCString());
if (lang) { if (lang) {
await ctx.render('highlight', { await ctx.render('highlight', {
pretty: config.prettyHtml, pretty: config.prettyHtml,
title: config.name + ' ' + paste.id, title: config.name + ' ' + paste.id,
paste: paste.paste, paste: paste.paste,
lang: lang lang: Object.keys(config.highlights).includes(lang) ? lang : 'unknown'
}); });
} else { } else {
ctx.type = 'text/plain'; ctx.type = 'text/plain';
ctx.body = paste.paste; ctx.body = paste.paste;
} }
} catch (ex) { } catch (ex) {
ctx.throw(404, 'Paste Not Found'); ctx.throw(404, 'Paste Not Found', {
headers: {
'Cache-Control': 'no-cache'
}
});
} }
}, },
async create(ctx) { async create(ctx) {
ctx.set('Cache-Control', 'no-cache');
if (ctx.request.body.fields) { if (ctx.request.body.fields) {
if (ctx.request.body.fields.paste) { if (ctx.request.body.fields.paste) {
ctx.request.body.paste = ctx.request.body.fields.paste; ctx.request.body.paste = ctx.request.body.fields.paste;
} }
if (ctx.request.body.fields.highlight) { if (ctx.request.body.fields.highlight) {
ctx.request.body.highlight = ctx.request.body.fields.highlight; ctx.request.body.highlight = ctx.request.body.fields.highlight.toLowerCase();
} }
if (ctx.request.body.fields.expire) { if (ctx.request.body.fields.expire) {
ctx.request.body.expire = ctx.request.body.fields.expire; ctx.request.body.expire = ctx.request.body.fields.expire;
@ -49,7 +58,7 @@ module.exports = {
let link = paste.id; let link = paste.id;
if (ctx.request.body.highlight) { if (ctx.request.body.highlight && ctx.request.body.highlight != 'plain' && Object.keys(config.highlights).includes(ctx.request.body.highlight)) {
link += '?' + ctx.request.body.highlight; link += '?' + ctx.request.body.highlight;
} }

Loading…
Cancel
Save