From 438ce5050e6ed3bddd2db1473627e59d9d0f8c5e Mon Sep 17 00:00:00 2001 From: Luc Didry Date: Wed, 26 Apr 2023 12:13:57 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E2=80=94=20Add=20--nuke=20option?= =?UTF-8?q?=20to=20image=20command=20(fix=20#134)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG | 1 + lib/Lutim/Command/image.pm | 42 +++++++++++++++++++++++++++++++++--- lib/Lutim/DB/Image.pm | 14 ++++++++++++ lib/Lutim/DB/Image/Pg.pm | 21 ++++++++++++++++++ lib/Lutim/DB/Image/SQLite.pm | 21 ++++++++++++++++++ 5 files changed, 96 insertions(+), 3 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 042532b..0e41a71 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,7 @@ Revision history for Lutim 0.15.0 ????-??-?? + - ✨ — Add --nuke option to image command 0.14.0 2023-12-18 - ⬆️ Update dependencies diff --git a/lib/Lutim/Command/image.pm b/lib/Lutim/Command/image.pm index b4ad261..de58257 100644 --- a/lib/Lutim/Command/image.pm +++ b/lib/Lutim/Command/image.pm @@ -46,7 +46,8 @@ sub run { 'r|remove=s{1,}' => \my @remove, 'y|yes' => \my $yes, 'q|quiet' => \my $quiet, - 's|search=s' => \my $ip + 's|search=s' => \my $ip, + 'n|nuke=s' => \my $nuke, ; if (scalar @info) { @@ -85,9 +86,43 @@ sub run { push @shorts, $e->short; print_infos($e, $csv); }); - say sprintf('%d matching URLs', $u->size); + say sprintf('%d matching images', $u->size); say sprintf("If you want to delete those images, please do:\n carton exec script/lutim image --remove %s", join(' ', @shorts)) if @shorts; } + if ($nuke) { + my $i = get_short($c, $nuke); + if ($i && $i->created_by) { + my $u = Lutim::DB::Image->new(app => $c->app)->search_exact_created_by($i->created_by); + my @shorts; + say sprintf('%d images created by the same IP address (%s) than image %s', $u->size, $i->created_by, $nuke); + my $confirm = ($yes) ? 'yes' : undef; + unless (defined $confirm) { + printf('Are you sure you want to remove those %d images? [N/y] ', $u->size); + $confirm = ; + chomp $confirm; + } + if ($confirm =~ m/^y(es)?$/i) { + $u->each(sub { + my ($e, $num) = @_; + my $i = get_short($c, $e->short); + if ($i) { + print_infos($i, $csv); + if ($i->enabled) { + delete_short($c, $i, 1); + } else { + say sprintf('The image %s is already disabled', $e->short); + } + } + }); + } else { + say 'Answer was not "y" or "yes". Aborting deletion.'; + } + } elsif (! $i->created_by) { + say sprintf('Image %s does not contain its creator’s IP address.', $nuke); + } else { + say sprintf('Sorry, can’t find image %s', $nuke); + } + } } sub get_short { @@ -168,7 +203,7 @@ sub delete_short { my $confirm = ($y) ? 'yes' : undef; unless (defined $confirm) { - printf('Are you sure you want to remove this image (%s) ? [N/y] ', $i->short); + printf('Are you sure you want to remove this image (%s)? [N/y] ', $i->short); $confirm = ; chomp $confirm; } @@ -191,6 +226,7 @@ Lutim::Command::image - Manage URL in Lutim's database carton exec script/lutim image --info [--csv] Print infos about the space-separated images (--csv creates a CSV output) carton exec script/lutim image --remove [--yes] [--quiet] Delete the space-separated images (--yes disables confirmation, --quiet disables informations printing) carton exec script/lutim image --search Print infos about the images uploaded by this IP (database LIKE, may include images uploaded by other IPs) + carton exec script/lutim image --nuke Delete the image and all images sent by the same IP address and print infos about the deleted images =cut diff --git a/lib/Lutim/DB/Image.pm b/lib/Lutim/DB/Image.pm index 9722ee8..75a33fc 100644 --- a/lib/Lutim/DB/Image.pm +++ b/lib/Lutim/DB/Image.pm @@ -345,6 +345,20 @@ sub to_hash { =back +=head2 search_exact_created_by + +=over 1 + +=item B : C<$c-Esearch_exact_created_by($ip)> + +=item B : an IP address + +=item B : get enabled images that have been uploaded by this IP address + +=item B : a Mojo::Collection object containing the matching images as Lutim::DB::Image objects + +=back + =cut 1; diff --git a/lib/Lutim/DB/Image/Pg.pm b/lib/Lutim/DB/Image/Pg.pm index c5e2738..9397fbf 100644 --- a/lib/Lutim/DB/Image/Pg.pm +++ b/lib/Lutim/DB/Image/Pg.pm @@ -213,6 +213,27 @@ sub search_created_by { return c(@images); } +sub search_exact_created_by { + my $c = shift; + my $ip = shift; + + my @images; + + my $records = $c->app->pg->db->select('lutim', undef, { enabled => 1, created_by => $ip })->hashes; + + $records->each( + sub { + my ($e, $num) = @_; + my $i = Lutim::DB::Image->new(app => $c->app); + $i->_slurp($e); + + push @images, $i; + } + ); + + return c(@images); +} + sub _slurp { my $c = shift; my $r = shift; diff --git a/lib/Lutim/DB/Image/SQLite.pm b/lib/Lutim/DB/Image/SQLite.pm index 398e32a..540e1f1 100644 --- a/lib/Lutim/DB/Image/SQLite.pm +++ b/lib/Lutim/DB/Image/SQLite.pm @@ -214,6 +214,27 @@ sub search_created_by { return c(@images); } +sub search_exact_created_by { + my $c = shift; + my $ip = shift; + + my @images; + + my $records = $c->app->sqlite->db->select('lutim', undef, { enabled => 1, created_by => $ip })->hashes; + + $records->each( + sub { + my ($e, $num) = @_; + my $i = Lutim::DB::Image->new(app => $c->app); + $i->_slurp($e); + + push @images, $i; + } + ); + + return c(@images); +} + sub _slurp { my $c = shift; my $r = shift;