From 8d818dd8f9b2cd452a03af36cdd3292fdc0d973b Mon Sep 17 00:00:00 2001 From: John Doe Date: Wed, 15 Oct 2025 18:06:47 +0000 Subject: [PATCH] Bug 41020: Add ability to delete files to Koha::File::Transport and derived classes --- Koha/File/Transport.pm | 15 ++++++- Koha/File/Transport/FTP.pm | 29 ++++++++++++++ Koha/File/Transport/Local.pm | 57 +++++++++++++++++++++++++++ Koha/File/Transport/SFTP.pm | 29 ++++++++++++++ t/db_dependent/Koha/File/Transports.t | 4 +- 5 files changed, 131 insertions(+), 3 deletions(-) diff --git a/Koha/File/Transport.pm b/Koha/File/Transport.pm index 188aaa1ec8e..b543ce21f10 100644 --- a/Koha/File/Transport.pm +++ b/Koha/File/Transport.pm @@ -184,8 +184,6 @@ sub test_connection { =head2 Subclass methods -Interface methods that must be implemented by subclasses - =head3 connect $transport->connect(); @@ -225,6 +223,19 @@ sub download_file { die "Subclass must implement download_file"; } +=head3 delete_file + + $transport->delete_file($file); + +Method for deleting a file from the current file server + +=cut + +sub delete_file { + my ( $self, $file ) = @_; + die "delete_file method not implemented in subclass"; +} + =head3 change_directory my $files = $transport->change_directory($path); diff --git a/Koha/File/Transport/FTP.pm b/Koha/File/Transport/FTP.pm index f965ebd0ede..b07e21b41e4 100644 --- a/Koha/File/Transport/FTP.pm +++ b/Koha/File/Transport/FTP.pm @@ -126,6 +126,35 @@ sub download_file { return 1; } +=head3 delete_file + + my $success = $server->delete_file($filename); + +Passed a filename, this will delete the file from the current directory of the server connection. + +Returns true on success or undefined on failure. + +=cut + +sub delete_file { + my ( $self, $remote_file ) = @_; + my $operation = "delete"; + + $self->{connection}->delete($remote_file) or return $self->_abort_operation( $operation, $remote_file ); + + $self->add_message( + { + message => $operation, + type => 'success', + payload => { + remote_file => $remote_file, + } + } + ); + + return 1; +} + =head3 change_directory my $success = $server->change_directory($directory); diff --git a/Koha/File/Transport/Local.pm b/Koha/File/Transport/Local.pm index 729a7f4a13b..9dbdb526dea 100644 --- a/Koha/File/Transport/Local.pm +++ b/Koha/File/Transport/Local.pm @@ -397,6 +397,63 @@ sub rename_file { return 1; } +=head3 delete_file + + $server->delete_file(); + +Deletes a file in the current directory. + +Returns true on success or undefined on failure. + +=cut + +sub delete_file { + my ( $self, $remote_file ) = @_; + my $operation = "delete"; + + my $directory = $self->download_directory || $self->{current_directory} || '.'; + my $source = File::Spec->catfile( $directory, $remote_file ); + + # Check if the file exists + unless ( -e $source ) { + $self->add_message( + { + message => $operation, + type => 'error', + payload => { + error => "File not found: $source", + path => $source + } + } + ); + return; + } + + # Attempt to delete the file + if ( unlink $source ) { + $self->add_message( + { + message => $operation, + type => 'success', + payload => { path => $source } + } + ); + } else { + $self->add_message( + { + message => $operation, + type => 'error', + payload => { + error => "File could not be deleted: $source", + path => $source + } + } + ); + } + + return 1; +} + =head3 disconnect $server->disconnect(); diff --git a/Koha/File/Transport/SFTP.pm b/Koha/File/Transport/SFTP.pm index d0abdd69d3a..179d1c17802 100644 --- a/Koha/File/Transport/SFTP.pm +++ b/Koha/File/Transport/SFTP.pm @@ -138,6 +138,35 @@ sub download_file { return 1; } +=head3 delete_file + + my $success = $server->delete_file($filename); + +Passed a filename, this will delete the file from the current directory of the server connection. + +Returns true on success or undefined on failure. + +=cut + +sub delete_file { + my ( $self, $remote_file ) = @_; + my $operation = "delete"; + + $self->{connection}->remove($remote_file) or return $self->_abort_operation( $operation, $remote_file ); + + $self->add_message( + { + message => $operation, + type => 'success', + payload => { + remote_file => $remote_file, + } + } + ); + + return 1; +} + =head3 change_directory my $success = $server->change_directory($directory); diff --git a/t/db_dependent/Koha/File/Transports.t b/t/db_dependent/Koha/File/Transports.t index 928c48423f0..597bd646bb4 100755 --- a/t/db_dependent/Koha/File/Transports.t +++ b/t/db_dependent/Koha/File/Transports.t @@ -29,7 +29,7 @@ my $schema = Koha::Database->new->schema; my $builder = t::lib::TestBuilder->new; subtest 'Polymorphic object creation' => sub { - plan tests => 6; + plan tests => 7; $schema->storage->txn_begin; @@ -90,6 +90,8 @@ subtest 'Polymorphic object creation' => sub { can_ok( $local_transport, 'rename_file' ); + can_ok( $local_transport, 'delete_file' ); + $schema->storage->txn_rollback; }; -- 2.39.5 (Apple Git-154)