Browse Source

* LDV::Zerobin : use database instead metadata files

master
Alex 'AdUser' Z 7 years ago
parent
commit
a28a0c34fa
  1. 67
      lib/LDV/Zerobin.pm
  2. 3
      templates/zerobin/view.html.ep

67
lib/LDV/Zerobin.pm

@ -9,35 +9,6 @@ use Mojo::Base 'Mojolicious::Controller';
use Mojo::Asset::File;
use Mojo::Util qw(b64_encode b64_decode decode encode);
sub _metadata_path {
my ($self, $time) = @_;
my $root = $self->app->config->{zerobin}->{root};
return $self->app->home->rel_file("$root/$time.json");
}
sub _metadata_save {
my ($self, $time, $data) = @_;
my $path = $self->_metadata_path($time);
my $asset = Mojo::Asset::File->new;
$asset->add_chunk($self->app->json->encode($data));
$asset->move_to($path);
return 1;
}
sub _metadata_load {
my ($self, $time) = @_;
my $path = $self->_metadata_path($time);
die("paste metadata not found\n") unless (-f $path);
my $asset = Mojo::Asset::File->new(path => $path);
$asset->cleanup(0);
return $self->app->json->decode($asset->slurp);
}
sub _content_path {
my ($self, $time) = @_;
@ -86,16 +57,13 @@ sub create {
sub view {
my ($self) = @_;
my $time = $self->stash('time');
my $time = $self->stash('time') || 0;
eval {
die("paste not found\n") unless $time;
my $paste = $self->_metadata_load($time);
$paste->{data} = $self->_content_load($time);
$paste->{time} = $time;
utf8::decode($paste->{data});
$self->stash({paste => $paste});
1;
my $paste = $self->app->db->select('zerobin', '*', {created => $time})->hash;
die("paste not found\n") unless $paste;
$paste->{data} = $self->_content_load($paste->{created});
$self->stash({paste => $paste}); 1;
} or do {
chomp $@;
$self->app->log->error($@);
@ -143,9 +111,9 @@ sub save {
$syntax = 'auto' unless ($syntax =~ m|^[a-z0-9]+$|oi);
my $time = time();
$self->_metadata_save($time, {
$self->app->db->insert('zerobin', {
created => $time, syntax => $syntax,
expire => $time + ($expire * 86400),
syntax => $syntax,
});
$self->_content_save($time, $paste);
$self->redirect_to("/zerobin/$time"); 1;
@ -164,18 +132,15 @@ sub prune {
my ($self) = @_;
eval {
my $currtime = time();
my $files = $self->app->home->list_files("data/zerobin");
foreach my $file (@{ $files }) {
next unless $file =~ m/^(\d+)\.json$/oi;
my $time = $1;
my $data = $self->_metadata_load($time);
next if $data->{expire} > $currtime; # not yet
my $date = strftime("%Y-%m-%d %H:%M", localtime($data->{expire}));
$self->app->log->info("Removing expired paste: $file ($date)");
unlink $self->_metadata_path($time);
unlink $self->_content_path($time);
} 1;
my @expired = $self->app->db->select('zerobin', '*', {expire => {'<=' => time()}})->hashes;
foreach my $paste (@expired) {
my $path = $self->_content_path($paste->{created});
$self->app->log->info("Removing expired paste: $path");
unlink $path if -f $path;
$self->app->db->delete('zerobin', {id => $paste->{id}});
}
my $msg = sprintf '%d records pruned', scalar @expired;
$self->app->log->info($msg); 1;
} or do {
chomp $@;
$self->app->log->error($@);

3
templates/zerobin/view.html.ep

@ -1,11 +1,12 @@
% layout 'default';
% title 'Zerobin -- View';
% my $paste = stash('paste') || {};
% my $path = sprintf '/zerobin/%d.txt', $paste->{created};
<h1><%= l('View paste') %></h1>
<div>
<div>
<%= link_to l('Create new') => "/zerobin", class => 'zerobin create' %>
<%= link_to l('Download') => "/zerobin/$paste->{time}.txt", class => 'zerobin download' %>
<%= link_to l('Download') => $path, class => 'zerobin download' %>
</div>
<br/>
<div class="paste">

Loading…
Cancel
Save