From ed8c6f27a0bcf66f66770a594af5af2e64985889 Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Tue, 10 Feb 2015 16:16:52 +1000 Subject: [PATCH] + LDV::Zerobin->prune --- lib/LDV.pm | 1 + lib/LDV/Zerobin.pm | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/LDV.pm b/lib/LDV.pm index 86b5208..6e971de 100644 --- a/lib/LDV.pm +++ b/lib/LDV.pm @@ -48,6 +48,7 @@ sub startup { $zb->get ('/') -> to(action => 'create'); $zb->route('/:time', time => qr/\d+/) ->via('GET') -> to(action => 'view'); + $zb->get('/prune') -> to(action => 'prune'); my $root = $self->app->config->{zerobin}->{root}; mkdir $self->app->home->rel_dir($root); diff --git a/lib/LDV/Zerobin.pm b/lib/LDV/Zerobin.pm index 7ce3569..e90934e 100644 --- a/lib/LDV/Zerobin.pm +++ b/lib/LDV/Zerobin.pm @@ -4,6 +4,7 @@ use strict; use warnings; use utf8; +use POSIX qw(strftime); use Mojo::Base 'Mojolicious::Controller'; use Mojo::JSON; use Mojo::Asset::File; @@ -97,7 +98,20 @@ sub prune my ($self) = @_; eval { - die("not implemented\n"); 1; + my $time = time(); + my $storage = $self->app->config->{zerobin}->{root}; + my $files = $self->app->home->list_files($storage); + my $json = Mojo::JSON->new; + foreach my $file (@$files) { + next unless $file =~ m/^(\d+)\.json$/oi; + my $paste_time = $1; + my $path = "$storage/$file"; + my $data = $json->decode($self->app->home->slurp_rel_file("$storage/$file")); + next if ($data->{expire} > $time); # not yet + my $date = strftime("%Y-%m-%d %H:%M", localtime($data->{expire})); + $self->app->log->info("Removing expired paste: $file ($date)"); + unlink $self->_paste_path($paste_time); + } 1; } or do { chomp $@; $self->app->log->error($@);