Browse Source

* LDV::Zerobin : first working version of controller

master
Alex "AdUser" Z 10 years ago
parent
commit
00625e01e9
  1. 88
      lib/LDV/Zerobin.pm

88
lib/LDV/Zerobin.pm

@ -5,12 +5,55 @@ use warnings;
use utf8;
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
{
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();
}
@ -18,15 +61,31 @@ sub view
sub save
{
my ($self) = @_;
my $id = $self->stash('id');
eval {
die("not implemented\n");
$self->redirect_to("/zerobin2/$id");
1;
my $expire = $self->req->param('expire') || 30; # 1 month
my $syntax = $self->req->param('syntax') || '';
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 {
$self->stash({'result' => $@});
$self->redirect_to("/zerobin2/$id");
chomp $@;
$self->app->log->error($@);
$@ = "internal error" if ($@ =~ m|at \S+ line \d+|oi);
$self->flash({'result' => $@});
$self->redirect_to("/zerobin2");
};
$self->rendered();
@ -35,7 +94,20 @@ sub save
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;

Loading…
Cancel
Save