Add paste expiry

pull/1/head
Joe Biellik 9 years ago
parent 5271bd812b
commit 3f3feab55e

@ -4,6 +4,14 @@
"cacheAge": 86400,
"sizeLimit": "2mb",
"prettyHtml": true,
"expiresDefault": 604800,
"expires": {
"3600": "1 hour",
"86400": "1 day",
"604800": "1 week",
"2592000": "1 month",
"31536000 ": "1 year"
},
"highlights": {
"abap": "ABAP",
"actionscript": "ActionScript",

@ -4,13 +4,13 @@ var Paste = require('../models/paste');
module.exports = {
*view(next) {
try {
let paste = yield Paste.findOne({ link: this.params.id }).exec();
let paste = yield Paste.findById(this.params.id).exec();
let lang = Object.keys(this.query)[0];
if (lang) {
yield this.render('highlight', {
pretty: config.prettyHtml,
title: 'Paste ' + paste.link,
title: 'Paste ' + paste.id,
paste: paste.paste,
lang: lang
});
@ -24,20 +24,30 @@ module.exports = {
},
*create(next) {
if (this.request.body.fields && this.request.body.fields.paste) {
this.request.body.paste = this.request.body.fields.paste;
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;
}
}
if (this.request.body.fields && this.request.body.fields.highlight) {
this.request.body.highlight = this.request.body.fields.highlight;
if (!this.request.body.expire) {
this.request.body.expire = config.expiresDefault;
}
let paste = new Paste({
paste: this.request.body.paste
paste: this.request.body.paste,
expiresAt: new Date(Date.now() + this.request.body.expire * 1000)
});
yield paste.save();
let link = paste.link;
let link = paste.id;
if (this.request.body.highlight) {
link += '?' + this.request.body.highlight;

@ -1,19 +1,12 @@
var mongoose = require('mongoose');
var shortId = require('short-mongo-id');
var shortid = require('shortid');
var paste = new mongoose.Schema({
_id: { type: String, unique: true, default: shortid.generate },
paste: { type: String },
link: { type: String }
expiresAt: { type: Date, expires: 0, default: new Date(Date.now() + 1000 * 60 * 60 * 24 * 7) }
}, {
timestamps: true
});
paste.pre('save', function(next) {
if (this.isNew) {
this.link = shortId(this._id);
next();
}
});
module.exports = mongoose.model('Paste', paste);

@ -19,9 +19,8 @@
],
"main": "server.js",
"scripts": {
"start": "node .",
"watch": "./node_modules/.bin/nodemon -L -e js,json",
"lint": "./node_modules/.bin/eslint ."
"lint": "./node_modules/.bin/eslint . && ./node_modules/.bin/jade-lint ./views/"
},
"dependencies": {
"babel-core": "^6.1.2",
@ -37,11 +36,12 @@
"koa-static-cache": "^3.1.2",
"koa-views": "^3.1.0",
"mongoose": "^4.1.12",
"short-mongo-id": "^0.1.1"
"shortid": "^2.2.4"
},
"devDependencies": {
"babel-eslint": "^4.1.3",
"eslint": "^1.7.2",
"jade-lint": "^2.0.0",
"nodemon": "^1.7.2"
}
}

@ -6,7 +6,8 @@ router.get('/', function *() {
yield this.render('index', {
pretty: config.prettyHtml,
title: 'Paste',
url: this.request.origin + '/',
url: this.request.origin,
expires: config.expires,
highlights: config.highlights
});
});

@ -1,7 +1,7 @@
extends layout
block head
link(rel='stylesheet', href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,600')
link(rel='stylesheet', href='http://fonts.googleapis.com/css?family=Open+Sans:400,300')
link(rel='stylesheet', href='https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha/css/bootstrap.min.css')
style.
html {
@ -33,10 +33,17 @@ block content
form(action='/?redirect', method='POST', accept-charset='UTF-8')
fieldset.form-group
textarea.form-control(name='paste', rows='5', required, autofocus)
select.c-select.select-lg.m-t-md(name='highlight')
select.c-select.select-lg.m-t-md.m-r-md(name='highlight')
option(value='', selected) Choose syntax highlighting
each val, key in highlights
option(value='#{key}') #{val}
select.c-select.select-lg.m-t-md(name='expire')
option(value='', selected) Choose expiry
each val, key in expires
option(value='#{key}') #{val}
button.btn.btn-primary.btn-lg.m-t-md.m-l-md.pull-right(type='submit') Upload
footer.hidden-sm-down

Loading…
Cancel
Save