1
0
Fork 0
mirror of https://github.com/perlbot/perlbuut-pastebin synced 2025-06-07 14:17:26 -04:00

Add slugs and expiration

This commit is contained in:
Ryan Voots 2017-04-23 00:41:05 -04:00
parent cf1760340b
commit 2ff3b38676
3 changed files with 84 additions and 12 deletions

64
app.pl
View file

@ -41,12 +41,17 @@ $dbh->{sqlite_unicode} = 1;
# hardcode some channels first
sub insert_pastebin {
my ($paste, $who, $what, $where) = @_;
my ($paste, $who, $what, $where, $expire, $lang) = @_;
$dbh->do("INSERT INTO posts (paste, who, 'where', what, 'when') VALUES (?, ?, ?, ?, ?)", {}, $paste, $who, $where, $what, time());
$dbh->do("INSERT INTO posts (paste, who, 'where', what, 'when', 'expiration', 'language') VALUES (?, ?, ?, ?, ?, ?, ?)", {}, $paste, $who, $where, $what, time(), $expire, $lang);
my $id = $dbh->last_insert_id('', '', 'posts', 'id');
return $id;
# TODO this needs to retry when it fails.
my @chars = ('a'..'z', 1..9);
my $slug = join '', map {$chars[rand() *@chars]} 1..6;
$dbh->do("INSERT INTO slugs (post_id, slug) VAlUES (?, ?)", {}, $id, $slug);
return $slug;
}
get '/' => sub {
@ -61,7 +66,7 @@ get '/paste' => sub {$_[0]->redirect_to('/')};
post '/paste' => sub {
my $c = shift;
my @args = map {($c->param($_))} qw/paste name desc chan/;
my @args = map {($c->param($_))} qw/paste name desc chan expire language/;
my $id = insert_pastebin(@args);
my ($code, $who, $desc, $channel) = @args;
@ -96,17 +101,56 @@ get '/edit/:pasteid' => sub {
}
};
get '/pastebin/:pasteid' => sub {
my $c = shift;
my $pasteid = $c->param('pasteid');
my $row = $dbh->selectrow_hashref("SELECT * FROM posts WHERE id = ? LIMIT 1", {}, $pasteid);
sub get_paste {
my $pasteid = shift;
my $row = $dbh->selectrow_hashref(q{
SELECT p.*
FROM posts p
LEFT JOIN slugs s ON p.id = s.post_id
WHERE p.id = ? OR s.slug = ?
ORDER BY s.slug DESC
LIMIT 1
}, {}, $pasteid, $pasteid);
my $when = delete $row->{when};
if ($when) {
my $whendt = DateTime->from_epoch(epoch => $when);
if ($whendt->clone()->add(hours => $row->{expiration}) >= DateTime->now()) {
$row->{when} = $whendt->iso8601;
return $row;
} else {
return undef;
}
} else {
return undef;
}
}
get '/raw/:pasteid' => sub {
my $c = shift;
my $pasteid = $c->param('pasteid');
my $row = get_paste($pasteid);
if ($row) {
$c->render(text => $row->{paste});
} else {
# 404
return $c->reply->not_found;
}
};
get '/pastebin/:pasteid' => sub {
my $c = shift;
my $pasteid = $c->param('pasteid');
my $row = get_paste($pasteid);
if ($row) {
$c->stash($row);
$c->stash({when => DateTime->from_epoch(epoch => $when)->iso8601});
$c->stash({page_tmpl => 'viewer.html'});
$c->stash({eval => get_eval($pasteid, $row->{paste})});
$c->stash({paste_id => $pasteid});

BIN
pastes.db

Binary file not shown.

View file

@ -1,6 +1,6 @@
[% BLOCK body_style %]
<style type="text/css" media="screen">
#editor {
#editor, #paste {
margin: auto;
position: relative !important;
width: 100%;
@ -10,6 +10,12 @@
html, body, #content {
width: 100%;
}
.options label {
padding-right: 0.5em;
}
.options label:not(:first-child) {
padding-left: 2em;
}
@font-face {
font-family: "AnonymousPro";
@ -42,8 +48,15 @@
</div>
<div class="panel-body">
<div class="row">
<div class="options">
<label>Raw Viewer</label>
<input type="checkbox" id="raw_editor"/>
<label><a href="/raw/[% id %]">Raw Link</a></label>
<!-- TODO this should come from the eval server somehow -->
</div>
<div class="col-md-12">
<pre id="editor">[% paste | html %]</pre>
<pre id="paste">[% paste | html %]</pre>
<pre id="editor"></pre>
</div>
</div>
<div class="row">
@ -65,6 +78,21 @@
//editor.setTheme("ace/theme/twilight");
editor.session.setMode("ace/mode/perl");
var use_editor = function() {
if ($("#raw_editor").is(":checked")) {
$("#editor").hide();
$("#paste").show();
} else {
console.log($("#paste").text());
editor.setValue($("#paste").text(),0);
editor.clearSelection();
$("#paste").hide();
$("#editor").show();
}
};
use_editor();
$("#raw_editor").on("change", use_editor);
editor.setReadOnly(true);
editor.setOptions({maxLines: Infinity, fontFamily: ['AnonymousPro', 'monospace', 'mono']});
/*function resizeAce() {