|
|
@ -5,12 +5,55 @@ use warnings; |
|
|
|
use utf8; |
|
|
|
use utf8; |
|
|
|
|
|
|
|
|
|
|
|
use Mojo::Base 'Mojolicious::Controller'; |
|
|
|
use Mojo::Base 'Mojolicious::Controller'; |
|
|
|
|
|
|
|
use Mojo::JSON; |
|
|
|
|
|
|
|
use Mojo::Asset::File; |
|
|
|
|
|
|
|
use Mojo::Util qw(b64_encode b64_decode decode encode); |
|
|
|
|
|
|
|
|
|
|
|
sub create { my ($self) = @_; $self->render(); } |
|
|
|
sub _paste_path { |
|
|
|
|
|
|
|
my ($self, $time) = @_; |
|
|
|
|
|
|
|
my $root ||= $self->app->config->{zerobin}->{root}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return $self->app->home->rel_file("$root/$time.json"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub create |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
my ($self) = @_; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
my @syntax = ('auto'); |
|
|
|
|
|
|
|
my $langs = $self->app->config->{zerobin}->{syntax}; |
|
|
|
|
|
|
|
push @syntax, {'-------' => $langs} |
|
|
|
|
|
|
|
if (ref($langs) eq 'ARRAY'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$self->stash({syntax => \@syntax}); |
|
|
|
|
|
|
|
$self->render(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
sub view |
|
|
|
sub view |
|
|
|
{ |
|
|
|
{ |
|
|
|
my ($self) = @_; |
|
|
|
my ($self) = @_; |
|
|
|
|
|
|
|
my $time = $self->stash('time'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
eval { |
|
|
|
|
|
|
|
die("paste not found\n") unless ($time); |
|
|
|
|
|
|
|
my $path = $self->_paste_path($time); |
|
|
|
|
|
|
|
die("paste not found\n") unless (-f $path); |
|
|
|
|
|
|
|
my $asset = Mojo::Asset::File->new(path => $path); |
|
|
|
|
|
|
|
my $json = Mojo::JSON->new->decode($asset->slurp()); |
|
|
|
|
|
|
|
$json->{data} = decode('UTF-8', b64_decode($json->{data})); |
|
|
|
|
|
|
|
if (time() > $json->{expire}) { |
|
|
|
|
|
|
|
unlink($path); |
|
|
|
|
|
|
|
die("paste expired\n"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
$self->stash({paste => $json}); |
|
|
|
|
|
|
|
} or do { |
|
|
|
|
|
|
|
chomp $@; |
|
|
|
|
|
|
|
$self->app->log->error($@); |
|
|
|
|
|
|
|
$@ = "internal error" if ($@ =~ m|at \S+ line \d+|oi); |
|
|
|
|
|
|
|
$self->flash({'result' => $@}); |
|
|
|
|
|
|
|
$self->redirect_to("/zerobin2"); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
$self->render(); |
|
|
|
$self->render(); |
|
|
|
} |
|
|
|
} |
|
|
@ -18,15 +61,31 @@ sub view |
|
|
|
sub save |
|
|
|
sub save |
|
|
|
{ |
|
|
|
{ |
|
|
|
my ($self) = @_; |
|
|
|
my ($self) = @_; |
|
|
|
my $id = $self->stash('id'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
eval { |
|
|
|
eval { |
|
|
|
die("not implemented\n"); |
|
|
|
my $expire = $self->req->param('expire') || 30; # 1 month |
|
|
|
$self->redirect_to("/zerobin2/$id"); |
|
|
|
my $syntax = $self->req->param('syntax') || ''; |
|
|
|
1; |
|
|
|
my $paste = $self->req->param('paste') || ''; |
|
|
|
|
|
|
|
die("empty paste\n") unless $paste; |
|
|
|
|
|
|
|
$expire = 30 unless ($expire =~ m|^\d+$|o); |
|
|
|
|
|
|
|
$syntax = 'auto' unless ($syntax =~ m|^[a-z0-9]+$|oi); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
my $time = time(); |
|
|
|
|
|
|
|
my $json = { |
|
|
|
|
|
|
|
expire => $time + ($expire * 86400), |
|
|
|
|
|
|
|
syntax => $syntax, |
|
|
|
|
|
|
|
data => b64_encode(encode('UTF-8', $paste)), |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
my $asset = Mojo::Asset::File->new; |
|
|
|
|
|
|
|
$asset->add_chunk(Mojo::JSON->new->encode($json)); |
|
|
|
|
|
|
|
$asset->move_to($self->_paste_path($time)); |
|
|
|
|
|
|
|
$self->redirect_to("/zerobin2/$time"); 1; |
|
|
|
} or do { |
|
|
|
} or do { |
|
|
|
$self->stash({'result' => $@}); |
|
|
|
chomp $@; |
|
|
|
$self->redirect_to("/zerobin2/$id"); |
|
|
|
$self->app->log->error($@); |
|
|
|
|
|
|
|
$@ = "internal error" if ($@ =~ m|at \S+ line \d+|oi); |
|
|
|
|
|
|
|
$self->flash({'result' => $@}); |
|
|
|
|
|
|
|
$self->redirect_to("/zerobin2"); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
$self->rendered(); |
|
|
|
$self->rendered(); |
|
|
@ -35,7 +94,20 @@ sub save |
|
|
|
|
|
|
|
|
|
|
|
sub prune |
|
|
|
sub prune |
|
|
|
{ |
|
|
|
{ |
|
|
|
$self->stash({'result' => $@}); |
|
|
|
my ($self) = @_; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
eval { |
|
|
|
|
|
|
|
die("not implemented\n"); 1; |
|
|
|
|
|
|
|
} or do { |
|
|
|
|
|
|
|
chomp $@; |
|
|
|
|
|
|
|
$self->app->log->error($@); |
|
|
|
|
|
|
|
$@ = "internal error" if ($@ =~ m|at \S+ line \d+|oi); |
|
|
|
|
|
|
|
$self->stash({'result' => $@}); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$self->redirect_to("/zerobin2"); |
|
|
|
|
|
|
|
$self->rendered(); |
|
|
|
|
|
|
|
return 1; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
1; |
|
|
|
1; |
|
|
|