From c34e18a03dea850bcddd3aea374cd0a149a921af Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Sun, 21 Nov 2021 00:03:44 +0100 Subject: [PATCH] Initial revision --- config.yml.sample | 5 ++ paste.pl | 178 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 183 insertions(+) create mode 100644 config.yml.sample create mode 100755 paste.pl diff --git a/config.yml.sample b/config.yml.sample new file mode 100644 index 0000000..94aea6b --- /dev/null +++ b/config.yml.sample @@ -0,0 +1,5 @@ +--- +# Password for uploads. If not set, uploads should be done by other means, +# such as direct writing to data directory +password: change_this +# base: https://external.name/prefix diff --git a/paste.pl b/paste.pl new file mode 100755 index 0000000..6c25fde --- /dev/null +++ b/paste.pl @@ -0,0 +1,178 @@ +#!/usr/bin/perl + +use Mojolicious::Lite -signatures; +use Mojo::File qw(curfile); + +plugin NotYAMLConfig => { file => 'config.yml', + default => { + appname => 'Paste Bin', + } }; + +my $datadir = curfile->sibling('data'); +if (app->config->{datadir}) { + $datadir = Mojo::File->new(app->config->{datadir}); +} + +chdir curfile->dirname; + +get '/' => sub ($c) { + $c->render(template => 'forbidden', status => 403) + if !length app->config->{password}; +} => 'index'; + +post '/' => sub ($c) { + # print STDERR "pass=" . $c->param('password') . "\n"; + return $c->render(template => 'forbidden', status => 403) + if !defined app->config->{password} + || !length $c->param('password') + || $c->param('password') ne app->config->{password}; + + my $file_content = $c->param('text'); + my $filename = $c->param('filename'); + my $upload = $c->param('file'); + + if (defined $upload && $upload->size) { + # print STDERR "FILENAME = " . $upload->filename . "\n"; + $filename = $upload->filename; + $file_content = $upload->slurp; + } + + if ($filename !~ /\A\w[\w\.]*\.\w+\z/) { + # print STDERR "FILENAME2 = " . $upload->filename . "\n"; + return $c->render(template => 'forbidden', status => 403); + } + + $datadir->child($filename)->spurt($file_content); + $c->redirect_to($c->req->url->base . "$filename"); +}; + +get '/.' + => [ filename => qr/\w[\w\.]*/, ext => qr/\w+/ ] + => sub ($c) { + my $fullname = $c->param('filename').'.'.$c->param('ext'); + my $file = $datadir->child($fullname); + my $stat = $file->stat; + + return $c->reply->not_found + if !defined $stat; + + $c->stash(mtime => POSIX::strftime('%Y-%m-%d %H:%M:%S', + localtime($stat->mtime))); + $c->stash(file_content => $file->slurp); + my $lang = $c->param('ext'); + + $c->stash(language => "language-$lang"); + $c->render; +} => 'default'; + +app->start; + +__DATA__ +@@ index.html.ep +% layout 'default'; +

<%= config->{appname} %>

+
+ + + + + + + + + +
+ + +@@ default.html.ep +% layout 'default'; + +%= content_for header => begin + + + + +% end + +

<%= $filename%>.<%= $ext %> + — <%= config->{appname} %>
+Created: <%= $mtime %>

+
<%= $file_content %>
+ + +@@ forbidden.html.ep +% layout 'default'; + +

Forbidden

+ + +@@ layouts/default.html.ep + + + + + + + + + + + + <%= content 'header' %> + + <%= config->{appname} %> + +
+ <%= content %> + +
+ -- 2.43.5