From 2ff3b386766e569c354f98033cc15b930ffab36f Mon Sep 17 00:00:00 2001 From: Ryan Voots Date: Sun, 23 Apr 2017 00:41:05 -0400 Subject: [PATCH] Add slugs and expiration --- app.pl | 64 +++++++++++++++++++++++++++++++++++------- pastes.db | Bin 1306624 -> 1309696 bytes templates/viewer.html | 32 +++++++++++++++++++-- 3 files changed, 84 insertions(+), 12 deletions(-) diff --git a/app.pl b/app.pl index 82ea992..307623c 100755 --- a/app.pl +++ b/app.pl @@ -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}); diff --git a/pastes.db b/pastes.db index a9615cf00773ca84026fd632b5795249862a41da..0a844a960500af7c342588c32bf809e6f18f44ab 100644 GIT binary patch delta 789 zcmY*WT}TvB7`=DC(fye_>AGy2%EC>Al-9HaQS65;w-guuSW!r_aUFK2oo2_`SxpGk z-MEyuOb6wcs_xo{nB0ml6Y>>*gbj6TN zC8g1>o+$0Ak8q^hLFGg4fpladijNJd@rnfm&C00wwVxH0dk zJvnn7Y+i0IU`k`EoK`dO^nT&@E)9f3(J+mME=Iz1zenrzRN5R>#%;;)mGHo!Y;Yg& zugu}7zQoevVAHDnFZcnQ@D0}C3w(wT@D^4A{2L$^;C*(|s_*9HW#=B?jVxgVGr}h`^6H$MH*>{BDsXgzq453|+$(8G?nvf-0OZ5LK8dzEFi{ zm_yGC3!3nh=T@`F^4^liOc$6=m@YE?2-C}$Ue0t6(<`3nUjDX|LotLNpqLfW(7DE3 z(t!p=QRFjenWjxciJ9%9TXgG&qM5X&Pn8sn%HxWeGHTj~ce-m>Ja@y1?hT3U-T7A| zM+=K}<&MJave#F9z3x2)<)R%1bRu{J^B_ZK(bIufxqMUW=uA~EECq!_Z; z|BzEdnxeXtHf1VnrcpUfEia1PMyC`rLC1{LRN8uH{URS~sHzy=>AQ-S?2TTq!nREO zw1d7=^b@>|d44ySEHX_nF|i$Tu!dk8)(+gQG_{supTGfEi8qpM@kuRFih(P`Ut4|y Dx+3K& delta 279 zcmYL>F-yZx6ouc-eNEE7o0=Fjf&?5@)Wy+%phLmM!S0GEJ{%lWG)>?lDpCn0zrv+L z^E-4C9UPpbqZTa%eVshR;qdWslJ7?H)@aWLeIevX{0HuqRt;SKx%3RQFcx@-aTyTY zTX~F|wN@RRD2kwUnr~YiEL4mJI2_BbIK;t&FW-@>iw3xcy`(c;_ZQ(KB_?39n2<>_ zRhVohhskBCGSzydWYf(c96vpmw5z^QnSBmqT8@hKeRWZIpJ7(k57Y^iw}bEqY$5O} zJ!Y*~6Kjuko%IB3pLM``vZq6NW`pPm(aD3kYsEX+o-1y)ytR$|u{m9|=Y6M0y5Us$ I)DKti50LFi6#xJL diff --git a/templates/viewer.html b/templates/viewer.html index 597200d..1df0d6a 100755 --- a/templates/viewer.html +++ b/templates/viewer.html @@ -1,6 +1,6 @@ [% BLOCK body_style %]