Browse Source

* ability to upload files to zerobin

master
Alex 'AdUser' Z 9 years ago
parent
commit
e6c8f9984b
  1. 27
      lib/LDV/Zerobin.pm

27
lib/LDV/Zerobin.pm

@ -5,7 +5,7 @@ use warnings;
use utf8; use utf8;
use JSON qw(encode_json decode_json); use JSON qw(encode_json decode_json);
use POSIX qw(strftime); use POSIX qw(strftime tmpnam);
use Mojo::Base 'Mojolicious::Controller'; use Mojo::Base 'Mojolicious::Controller';
use Mojo::Asset::File; use Mojo::Asset::File;
use Mojo::Util qw(b64_encode b64_decode decode encode); use Mojo::Util qw(b64_encode b64_decode decode encode);
@ -61,10 +61,33 @@ sub save {
my ($self) = @_; my ($self) = @_;
eval { eval {
my $source = $self->req->param('source') || '-';
my $expire = $self->req->param('expire') || 30; # 1 month my $expire = $self->req->param('expire') || 30; # 1 month
my $syntax = $self->req->param('syntax') || ''; my $syntax = $self->req->param('syntax') || '';
my $paste = $self->req->param('paste') || ''; my $paste;
if ($source eq 'form') {
$paste = $self->req->param('paste') || '';
die("empty paste\n") unless $paste; die("empty paste\n") unless $paste;
} elsif ($source eq 'file') {
require File::MimeInfo::Magic;
my $tmpfile = tmpnam();
my $maxsize = $self->app->config->{zerobin}->{maxsize};
my $upload = $self->req->upload('file');
die("empty uploaded file\n")
if $upload->size == 0;
die("uploaded file too large\n")
if $upload->size > $maxsize;
$upload->move_to($tmpfile);
$upload->asset->cleanup(1)
if $upload->asset->SUPER::can('cleanup');
my $mime = File::MimeInfo::Magic::mimetype($tmpfile);
die("uploaded file not looks like text\n")
unless $mime =~ m{^text/};
$paste = $upload->asset->slurp;
unlink $tmpfile;
} else {
die("unknown 'source'\n");
}
$expire = 30 unless ($expire =~ m|^\d+$|o); $expire = 30 unless ($expire =~ m|^\d+$|o);
$syntax = 'auto' unless ($syntax =~ m|^[a-z0-9]+$|oi); $syntax = 'auto' unless ($syntax =~ m|^[a-z0-9]+$|oi);

Loading…
Cancel
Save