From 42f0bdf9f1a3f14ca454fee93507d795f6da74c0 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Mon, 21 Nov 2016 14:12:17 +0100 Subject: [PATCH] Bug 17501: Use Koha::Object in Koha::Upload::_delete Note: This is the last occurrence where we use DBI to perform a CRUD operation. In this case a delete from uploaded_files. We now call Koha::UploadedFile[s]->delete to only delete the record from the table. A next step will be moving the additional functionality of removing the file(s) too. Test plan: [1] Run t/db_dependent/Upload.t [2] Delete an upload from tools/upload.pl Signed-off-by: Marcel de Rooy Signed-off-by: Tomas Cohen Arazi Signed-off-by: Jonathan Druart Signed-off-by: Kyle M Hall --- Koha/Upload.pm | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/Koha/Upload.pm b/Koha/Upload.pm index f30de0e7d6..4f4f5c5788 100644 --- a/Koha/Upload.pm +++ b/Koha/Upload.pm @@ -202,15 +202,7 @@ sub get { sub delete { my ( $self, $params ) = @_; - return if !$params->{id}; - my @res; - my $temp = $self->_lookup({ id => $params->{id} }); - foreach( @$temp ) { - my $d = $self->_delete( $_ ); - push @res, $d if $d; - } - return if !@res; - return @res; + return $self->_delete( $params->{id} ); } =head1 CLASS METHODS @@ -411,17 +403,23 @@ sub _lookup { } sub _delete { - my ( $self, $rec ) = @_; - my $dbh = C4::Context->dbh; - my $sql = 'DELETE FROM uploaded_files WHERE id=?'; - my $file = $self->_full_fname($rec); + my ( $self, $id ) = @_; + my $rec = Koha::UploadedFiles->find($id) || return; + my $filename = $rec->filename; + my $file = $self->_full_fname({ + permanent => $rec->permanent, + dir => $rec->dir, + hashvalue => $rec->hashvalue, + filename => $filename, + }); + if( !-e $file ) { # we will just delete the record # TODO Should we add a trace here for the missing file? - $dbh->do( $sql, undef, ( $rec->{id} ) ); - return $rec->{filename}; + $rec->delete; + return $filename; } elsif( unlink($file) ) { - $dbh->do( $sql, undef, ( $rec->{id} ) ); - return $rec->{filename}; + $rec->delete; + return $filename; } $self->{files}->{ $rec->{filename} }->{errcode} = 7; #NOTE: errcode=6 is used to report successful delete (see template) -- 2.39.5