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

63 lines
1.4 KiB
JavaScript

9 years ago
var config = require('config');
var Paste = require('../models/paste');
module.exports = {
*view() {
9 years ago
try {
9 years ago
let paste = yield Paste.findById(this.params.id).exec();
9 years ago
let lang = Object.keys(this.query)[0];
if (lang) {
yield this.render('highlight', {
pretty: config.prettyHtml,
9 years ago
title: 'Paste ' + paste.id,
9 years ago
paste: paste.paste,
lang: lang
});
} else {
this.type = 'text/plain';
this.body = paste.paste;
}
} catch (ex) {
this.throw('Paste Not Found', 404);
}
},
*create() {
9 years ago
if (this.request.body.fields) {
if (this.request.body.fields.paste) {
this.request.body.paste = this.request.body.fields.paste;
}
if (this.request.body.fields.highlight) {
this.request.body.highlight = this.request.body.fields.highlight;
}
if (this.request.body.fields.expire) {
this.request.body.expire = this.request.body.fields.expire;
}
9 years ago
}
9 years ago
if (!this.request.body.expire) {
this.request.body.expire = config.expiresDefault;
9 years ago
}
let paste = new Paste({
9 years ago
paste: this.request.body.paste,
expiresAt: new Date(Date.now() + this.request.body.expire * 1000)
9 years ago
});
yield paste.save();
9 years ago
let link = paste.id;
9 years ago
if (this.request.body.highlight) {
link += '?' + this.request.body.highlight;
}
if (Object.keys(this.query).includes('redirect')) {
this.redirect(link);
} else {
this.body = this.request.origin + '/' + link + '\n';
}
}
};