master
Barrington 2023-02-12 13:29:44 +01:00
parent f52dcbb721
commit dcea6b2a24
Signed by: svragv
GPG Key ID: B39C0BCEE94A4A89
1 changed files with 55 additions and 53 deletions

View File

@ -4,12 +4,14 @@
use if $^O eq "openbsd", OpenBSD::Pledge, qw(pledge);
use Mojolicious::Lite -signatures;
use Mojolicious::Routes::Pattern;
use List::MoreUtils qw(any uniq);
use List::MoreUtils qw(uniq);
use Carp;
use Term::ANSIColor;
use English;
use MIME::Types;
use warnings;
use experimental 'signatures';
use feature 'say';
plugin 'RenderFile';
@ -17,14 +19,15 @@ plugin 'RenderFile';
my $openbsd = 0;
$openbsd = 1 if $^O eq "openbsd";
pledge("stdio cpath rpath wpath inet flock fattr") if $openbsd;
use strict;
my $MAX_SIZE = 1024 * 1024 * 100;
my @BANNED = qw(); # Add banned IP addresses here
my @BANNED = qw(); # Add banned IP addresses here
my $dirname;
my $host;
mkdir "f";
# Function to handle file uploads
sub logger ( $level, $address, $message ) {
@ -38,17 +41,17 @@ sub handle_file {
my $filedata = $c->param("file");
if ( $filedata->size > $MAX_SIZE ) {
return $c->render(
text => "Max upload size: $MAX_SIZE",
status => 400
);
text => "Max upload size: $MAX_SIZE",
status => 400
);
}
if ( any { /$c->tx->remote_address/ } uniq @BANNED ) {
if ( List::MoreUtils::any { /$c->tx->remote_address/ } uniq @BANNED ) {
$c->render(
text =>
"Hi! Seems like the server admin added your IP address to the banned IP array." .
"As the developer of sakisafe, I can't do anything.",
status => 403
);
text =>
"Hi! Seems like the server admin added your IP address to the banned IP array." .
"As the developer of sakisafe, I can't do anything.",
status => 403
);
return;
}
@ -63,11 +66,11 @@ sub handle_file {
$filedata->move_to( "f/" . $dirname . "/" . $filename );
my $host = $c->req->url->to_abs->host;
$c->res->headers->header(
'Location' => "http://$host/$dirname/" . $filename );
'Location' => "http://$host/$dirname/" . $filename );
$c->render(
text => "http://$host/f/$dirname/" . $filename,
status => 201,
);
text => "http://$host/f/$dirname/" . $filename,
status => 201,
);
logger( "INFO", $c->tx->remote_address, $dirname . "/" . $filename );
$dirname = "";
@ -80,17 +83,16 @@ post '/' => sub ($c) { handle_file($c) };
# Allow files to be downloaded.
get '/f/:dir/:name' => sub ($c) {
my $captures = $c->req->url;
$captures =~ s/^.//;
my $filerender = Mojolicious::Plugin::RenderFile->new;
my $ext = $captures;
get '/f/:dir/#name' => sub ($c) {
my $dir = $c->param("dir");
my $file = $c->param("name");
print $dir, $file . "\n";
my $ext = $file;
$ext =~ s/.*\.//;
carp(color("bold yellow"), "sakisafe warning: could not get file: $ERRNO", color("reset")) unless
$c->render_file( filepath => $captures,
format => $ext,
content_disposition => 'inline'
);
$c->render_file( filepath => "f/".$dir . "/" . $file,
format => 'jpg',
content_disposition => 'inline'
);
};
app->max_request_size( 1024 * 1024 * 100 );
@ -107,30 +109,30 @@ app->start;
__DATA__
@@ index.html.ep
<!DOCTYPE html>
<html lang="en">
<head>
<title>sakisafe</title>
<link rel="stylesheet" type="text/css" href="index.css"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>sakisafe</h1>
<h2>shitless file upload, pastebin and url shorter</h2>
<img src="saki.png"/>
<h2>USAGE</h2>
<p>POST a file:</p>
<code>curl -F 'file=@yourfile.png' https://<%= $c->req->url->to_abs->host; %></code>
<p>Post your text directly</p>
<code>curl -F 'file=@-' https://<%= $c->req->url->to_abs->host; %></code>
</center>
<div class="left">
<h2>Or just upload a file here</h2>
<form ENCTYPE='multipart/form-data' method='post' action='/upload'>
<input type='file' name='file' size='30'/>
<input type='submit' value='upload'/>
</form>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<title>sakisafe</title>
<link rel="stylesheet" type="text/css" href="index.css"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>sakisafe</h1>
<h2>shitless file upload, pastebin and url shorter</h2>
<img src="saki.png"/>
<h2>USAGE</h2>
<p>POST a file:</p>
<code>curl -F 'file=@yourfile.png' https://<%= $c->req->url->to_abs->host; %></code>
<p>Post your text directly</p>
<code>curl -F 'file=@-' https://<%= $c->req->url->to_abs->host; %></code>
</center>
<div class="left">
<h2>Or just upload a file here</h2>
<form ENCTYPE='multipart/form-data' method='post' action='/upload'>
<input type='file' name='file' size='30'/>
<input type='submit' value='upload'/>
</form>
</div>
</body>
</html>