You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
paste/controllers/pastes.js

72 lines
1.8 KiB
JavaScript

const config = require('config');
const Paste = require('../models/paste');
9 years ago
module.exports = {
async view(ctx) {
9 years ago
try {
let paste = await Paste.findById(ctx.params.id).exec();
let lang = (Object.keys(ctx.query)[0] || '').toLowerCase();
ctx.set('Cache-Control', 'public');
ctx.set('Expires', paste.expiresAt.toUTCString());
9 years ago
if (lang) {
await ctx.render('highlight', {
9 years ago
pretty: config.prettyHtml,
title: config.name + ' ' + paste.id,
9 years ago
paste: paste.paste,
lang: Object.keys(config.highlights).includes(lang) ? lang : 'unknown'
9 years ago
});
} else {
ctx.type = 'text/plain';
ctx.body = paste.paste;
9 years ago
}
} catch (ex) {
ctx.throw(404, 'Paste Not Found', {
headers: {
'Cache-Control': 'no-cache'
}
});
9 years ago
}
},
async create(ctx) {
ctx.set('Cache-Control', 'no-cache');
if (ctx.request.body.fields) {
if (ctx.request.body.fields.paste) {
ctx.request.body.paste = ctx.request.body.fields.paste;
9 years ago
}
if (ctx.request.body.fields.highlight) {
ctx.request.body.highlight = ctx.request.body.fields.highlight.toLowerCase();
9 years ago
}
if (ctx.request.body.fields.expire) {
ctx.request.body.expire = ctx.request.body.fields.expire;
9 years ago
}
9 years ago
}
9 years ago
if (!ctx.request.body.expire) {
ctx.request.body.expire = config.expiresDefault;
9 years ago
}
let paste = new Paste({
paste: ctx.request.body.paste,
expiresAt: new Date(Date.now() + ctx.request.body.expire * 1000)
9 years ago
});
await paste.save();
9 years ago
9 years ago
let link = paste.id;
9 years ago
if (ctx.request.body.highlight && ctx.request.body.highlight != 'plain' && Object.keys(config.highlights).includes(ctx.request.body.highlight)) {
link += '?' + ctx.request.body.highlight;
9 years ago
}
if (Object.keys(ctx.query).includes('redirect')) {
ctx.redirect(link);
9 years ago
} else {
ctx.body = ctx.request.origin + '/' + link + '\n';
9 years ago
}
}
};