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 <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
This commit is contained in:
Marcel de Rooy 2016-11-21 14:12:17 +01:00 committed by Kyle M Hall
parent 4efe9e82cd
commit 42f0bdf9f1

View file

@ -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)