From ad2ed95cba8b3b9957fbe177e5043ca4c79ffded Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Fri, 17 Jul 2015 16:00:49 +1000 Subject: [PATCH] * LDV::Imgbin : implement 'prune' --- lib/LDV.pm | 2 +- lib/LDV/Imgbin.pm | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/LDV.pm b/lib/LDV.pm index fa8b968..a33d013 100644 --- a/lib/LDV.pm +++ b/lib/LDV.pm @@ -61,7 +61,7 @@ sub startup { $ib->get ('/') -> to(action => 'create'); $ib->route('/:time', time => qr/\d+/) ->via('GET') -> to(action => 'view'); - #$ib->get('/prune') -> to(action => 'prune'); + $ib->get ('/prune') -> to(action => 'prune'); $ib->get ('/latest') -> to(action => 'latest') if ($conf->{show_latest}); diff --git a/lib/LDV/Imgbin.pm b/lib/LDV/Imgbin.pm index ebdfa23..60e384c 100644 --- a/lib/LDV/Imgbin.pm +++ b/lib/LDV/Imgbin.pm @@ -152,7 +152,27 @@ sub latest { sub prune { my ($self) = @_; - die("unimplemented\n"); + eval { + my $root = $self->app->config->{imgbin}->{root}; + opendir(my $DH, $root) + or die("opendir: $!\n"); + my @files = grep { m/\d+\.json/oi } readdir($DH); + my @times = map { s/\.json//oi } @items; + closedir($DH); + + my $now = time(); + foreach my $time (@times) { + my $data = Mojo::Asset::File->new(path => $self->_data_path($time)); + my $image = decode_json($data->slurp); + next if $image->{expire} > $now; + unlink $self->_image_path($time, $image->{format}, 1); + unlink $self->_thumb_path($time); + } 1; + } or do { + $self->flash(error => $@); + }; + $self->redirect_to('/imgbin'); + $self->rendered(); } 1;