Browse Source

* LDV::Zerobin : use database instead metadata files

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

69
lib/LDV/Zerobin.pm

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

3
templates/zerobin/view.html.ep

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

Loading…
Cancel
Save