From 0a0fe81c928dd66de7034959b13fde292fdad23a Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Tue, 22 Nov 2016 16:57:21 +0100 Subject: [PATCH] Bug 17501: Remove Koha::Upload::get from Koha::Upload The get routine actually returns records from uploaded_files. It should be possible to replace its calls by direct calls of Koha::UploadedFiles. This patch is the crux of this patch set. It deals with all scripts that use Koha::Upload. In the process we do: [1] Add a file_handle method to Koha::UploadedFile. This was previously arranged via the fh parameter of get. [2] Add a full_path method to UploadedFile. Previously returned in the path hash key of get. (Name is replaced by filename.) [3] Add a search_term method too (implementing get({ term => .. }). This logic came from _lookup. [4] Add a keep_file parameter to delete method. Only used in test now. Test plan: [1] Run t/db_dependent/Upload.t [2] Go to Tools/Upload. Add an upload, download and delete. [3] Add another public upload , search for it. Use the hashvalue to download via opac with URL: cgi-bin/koha/opac-retrieve-file.pl?id=[hashvalue] [4] Go to Tools/Stage MARC for import. Import a marc file. [5] Go to Tools/Upload local cover image. Import an image file. Enable OPACLocalCoverImages to see result. [6] Test uploading a offline circulation file: Enable AllowOfflineCirculation, and create a koc file (plain text): Line1: Version=1.0\tA=1\tB=2 Line2: 2016-11-23 16:00:00 345\treturn\t[barcode] Note: Replace tabs and barcode. The number of tabs is essential! Checkout the item with your barcode. Go to Circulation/Offline circulation file upload. Upload and click Apply directly. Checkout again. Repeat Offline circulation file upload. Now click Add to offline circulation queue. [7] Connect the upload plugin to field 856$u. Enable HTML5MediaEnabled. Upload a webm file via the plugin. Click Choose to save the URL, and put 'video/webm' into 856$q. Save the biblio record. Check if you see the media tab with player on staff detail. (See also: Bug 17673 about empty OPACBaseURL.) Signed-off-by: Marcel de Rooy Signed-off-by: Tomas Cohen Arazi --- C4/HTML5Media.pm | 12 +-- Koha/Upload.pm | 94 +++------------------- Koha/UploadedFile.pm | 23 +++++- Koha/UploadedFiles.pm | 29 ++++++- .../intranet-tmpl/prog/en/modules/tools/upload.tt | 2 +- offline_circ/enqueue_koc.pl | 9 ++- offline_circ/process_koc.pl | 11 +-- opac/opac-retrieve-file.pl | 12 ++- t/db_dependent/Upload.t | 79 ++++++++++-------- tools/stage-marc-import.pl | 8 +- tools/upload-cover-image.pl | 11 +-- tools/upload.pl | 32 +++++--- 12 files changed, 164 insertions(+), 158 deletions(-) diff --git a/C4/HTML5Media.pm b/C4/HTML5Media.pm index 7f6dff60d8..1ac22f17c3 100644 --- a/C4/HTML5Media.pm +++ b/C4/HTML5Media.pm @@ -22,7 +22,7 @@ use warnings; use C4::Context; use MARC::Field; -use Koha::Upload; +use Koha::UploadedFiles; =head1 HTML5Media @@ -134,10 +134,12 @@ sub gethtml5media { if ( $HTML5Media{srcblock} =~ /\Qopac-retrieve-file.pl\E/ ) { my ( undef, $id ) = split /id=/, $HTML5Media{srcblock}; next if !$id; - my $public = ( ( caller )[1] =~ /opac/ ) ? { public => 1 }: {}; - my $upl = Koha::Upload->new( $public )->get({ hashvalue => $id }); - next if !$upl || $upl->{name} !~ /\./; - $HTML5Media{extension} = ( $upl->{name} =~ m/([^.]+)$/ )[0]; + my %public = ( ( caller )[1] =~ /opac/ ) ? ( public => 1 ): (); + my $upload = Koha::UploadedFiles->search({ + hashvalue => $id, %public, + })->next; + next if !$upload || $upload->filename !~ /\./; + $HTML5Media{extension} = ( $upload->filename =~ m/([^.]+)$/ )[0]; } # check remote files else { diff --git a/Koha/Upload.pm b/Koha/Upload.pm index e5a6c1701a..6a375cd627 100644 --- a/Koha/Upload.pm +++ b/Koha/Upload.pm @@ -26,6 +26,7 @@ Koha::Upload - Facilitate file uploads (temporary and permanent) =head1 SYNOPSIS use Koha::Upload; + use Koha::UploadedFiles; # add an upload (see tools/upload-file.pl) # the public flag allows retrieval via OPAC @@ -34,14 +35,13 @@ Koha::Upload - Facilitate file uploads (temporary and permanent) # Do something with $upload->count, $upload->result or $upload->err # get some upload records (in staff) - # Note: use the public flag for OPAC - my @uploads = Koha::Upload->new->get( term => $term ); - $template->param( uploads => \@uploads ); + my @uploads1 = Koha::UploadedFiles->search({ filename => $name }); + my @uploads2 = Koha::UploadedFiles->search_term({ term => $term }); # staff download - my $rec = Koha::Upload->new->get({ id => $id, filehandle => 1 }); - my $fh = $rec->{fh}; - my @hdr = Koha::Upload->httpheaders( $rec->{name} ); + my $rec = Koha::UploadedFiles->find( $id ); + my $fh = $rec->file_handle; + my @hdr = Koha::Upload->httpheaders( $rec->filename ); print Encode::encode_utf8( $input->header( @hdr ) ); while( <$fh> ) { print $_; } $fh->close; @@ -54,9 +54,9 @@ Koha::Upload - Facilitate file uploads (temporary and permanent) functionality of both. The module has been revised to use Koha::Object[s]; the delete method - has been moved to Koha::UploadedFile[s]. + has been moved to Koha::UploadedFile[s], as well as the get method. -=head1 METHODS +=head1 INSTANCE METHODS =cut @@ -158,40 +158,6 @@ sub err { return $err; } -=head2 get - - Returns arrayref of uploaded records (hash) or one uploaded record. - You can pass id => $id or hashvalue => $hash or term => $term. - Optional parameter filehandle => 1 returns you a filehandle too. - -=cut - -sub get { - my ( $self, $params ) = @_; - my $temp= $self->_lookup( $params ); - my ( @rv, $res); - foreach my $r ( @$temp ) { - undef $res; - foreach( qw[id hashvalue filesize uploadcategorycode public permanent owner] ) { - $res->{$_} = $r->{$_}; - } - $res->{name} = $r->{filename}; - $res->{path} = $self->_full_fname($r); - if( $res->{path} && -r $res->{path} ) { - if( $params->{filehandle} ) { - my $fh = IO::File->new( $res->{path}, "r" ); - $fh->binmode if $fh; - $res->{fh} = $fh; - } - push @rv, $res; - } else { - $self->{files}->{ $r->{filename} }->{errcode}=5; #not readable - } - last if !wantarray; - } - return wantarray? @rv: $res; -} - =head1 CLASS METHODS =head2 getCategories @@ -292,10 +258,10 @@ sub _create_file { # if the file exists and it is registered, then set error # if it exists, but is not in the database, we will overwrite if( -e "$dir/$fn" && - Koha::UploadedFiles->search({ - hashvalue => $hashval, - uploadcategorycode => $self->{category}, - })->count ) { + Koha::UploadedFiles->search({ + hashvalue => $hashval, + uploadcategorycode => $self->{category}, + })->count ) { $self->{files}->{$filename}->{errcode} = 1; #already exists return; } @@ -319,19 +285,6 @@ sub _dir { return $dir; } -sub _full_fname { - my ( $self, $rec ) = @_; - my $p; - if( ref $rec ) { - $p = File::Spec->catfile( - $rec->{permanent}? $self->{rootdir}: $self->{tmpdir}, - $rec->{dir}, - $rec->{hashvalue}. '_'. $rec->{filename} - ); - } - return $p; -} - sub _hook { my ( $self, $filename, $buffer, $bytes_read, $data ) = @_; $filename= Encode::decode_utf8( $filename ); # UTF8 chars in filename @@ -366,29 +319,6 @@ sub _register { $self->{files}->{$filename}->{id} = $rec->id if $rec; } -sub _lookup { - my ( $self, $params ) = @_; - - my ( $cond, $attr, %pubhash ); - %pubhash = $self->{public}? ( public => 1 ): (); - if( $params->{id} ) { - return [] if $params->{id} !~ /^\d+(,\d+)*$/; - $cond = { id => [ split ',', $params->{id} ], %pubhash }; - } elsif( $params->{hashvalue} ) { - $cond = { hashvalue => $params->{hashvalue}, %pubhash }; - } elsif( $params->{term} ) { - $cond = - [ { filename => { like => '%'.$params->{term}.'%' }, %pubhash }, - { hashvalue => { like => '%'.$params->{term}.'%' }, %pubhash } ]; - } else { - return []; - } - $attr = { order_by => { -asc => 'id' }}; - - return Koha::UploadedFiles->search( $cond, $attr )->unblessed; - # Does always return an arrayref (perhaps an empty one) -} - sub _compute { # Computes hash value when sub hook feeds the first block # For temporary files, the id is made unique with time diff --git a/Koha/UploadedFile.pm b/Koha/UploadedFile.pm index 01d0580a87..4b2a151d1f 100644 --- a/Koha/UploadedFile.pm +++ b/Koha/UploadedFile.pm @@ -43,19 +43,22 @@ Description =head3 delete Delete uploaded file. -It deletes not only the record, but also the actual file. +It deletes not only the record, but also the actual file (unless you pass +the keep_file parameter). Returns filename on successful delete or undef. =cut sub delete { - my ( $self ) = @_; + my ( $self, $params ) = @_; my $name = $self->filename; my $file = $self->full_path; - if( !-e $file ) { # we will just delete the record + if( $params->{keep_file} ) { + return $name if $self->SUPER::delete; + } elsif( !-e $file ) { # we will just delete the record warn "Removing record for $name within category ". $self->uploadcategorycode. ", but file was missing."; return $name if $self->SUPER::delete; @@ -84,6 +87,20 @@ sub full_path { return $path; } +=head3 file_handle + +Returns a file handle for an uploaded file. + +=cut + +sub file_handle { + my ( $self ) = @_; + $self->{_file_handle} = IO::File->new( $self->full_path, "r" ); + return if !$self->{_file_handle}; + $self->{_file_handle}->binmode; + return $self->{_file_handle}; +} + =head2 CLASS METHODS =head3 root_directory diff --git a/Koha/UploadedFiles.pm b/Koha/UploadedFiles.pm index 36eb0d2332..7587dfa9a9 100644 --- a/Koha/UploadedFiles.pm +++ b/Koha/UploadedFiles.pm @@ -46,15 +46,17 @@ Delete uploaded files. Returns true if no errors occur. Delete_errors returns the number of errors when deleting files. +Parameter keep_file may be used to delete records, but keep files. + =cut sub delete { - my ( $self ) = @_; + my ( $self, $params ) = @_; # We use the individual delete on each resultset record my $err = 0; while( my $row = $self->_resultset->next ) { my $kohaobj = Koha::UploadedFile->_new_from_dbic( $row ); - $err++ if !$kohaobj->delete; + $err++ if !$kohaobj->delete( $params ); } $self->{delete_errors} = $err; return $err==0; @@ -65,6 +67,29 @@ sub delete_errors { return $self->{delete_errors}; } +=head3 search_term + +Search_term allows you to pass a term to search in filename and hashvalue. +If you do not pass include_private, only public records are returned. + +Is only a wrapper around Koha::Objects search. Has similar return value. + +=cut + +sub search_term { + my ( $self, $params ) = @_; + my $term = $params->{term} // ''; + my %public = (); + if( !$params->{include_private} ) { + %public = ( public => 1 ); + } + return $self->search( + [ { filename => { like => '%'.$term.'%' }, %public }, + { hashvalue => { like => '%'.$params->{term}.'%' }, %public } ], + { order_by => { -asc => 'id' }}, + ); +} + =head2 CLASS METHODS =head3 _type diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/upload.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/upload.tt index 4fd5409161..0f3c014f4c 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/upload.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/upload.tt @@ -162,7 +162,7 @@ [% FOREACH record IN uploads %] - [% record.name %] + [% record.filename %] [% record.filesize %] [% record.hashvalue %] [% record.uploadcategorycode %] diff --git a/offline_circ/enqueue_koc.pl b/offline_circ/enqueue_koc.pl index a81aecb406..73a505387e 100755 --- a/offline_circ/enqueue_koc.pl +++ b/offline_circ/enqueue_koc.pl @@ -32,7 +32,7 @@ use C4::Circulation; use C4::Items; use C4::Members; use C4::Stats; -use Koha::Upload; +use Koha::UploadedFiles; use Date::Calc qw( Add_Delta_Days Date_to_Days ); @@ -60,9 +60,10 @@ my $sessionID = $cookies{'CGISESSID'}->value; our $dbh = C4::Context->dbh(); if ($fileID) { - my $upload = Koha::Upload->new->get({ id => $fileID, filehandle => 1 }); - my $fh = $upload->{fh}; - my @input_lines = <$fh>; + my $upload = Koha::UploadedFiles->find($fileID); + my $fh = $upload? $upload->file_handle: undef; + my @input_lines = $fh? <$fh>: (); + $fh->close if $fh; my $header_line = shift @input_lines; my $file_info = parse_header_line($header_line); diff --git a/offline_circ/process_koc.pl b/offline_circ/process_koc.pl index f959d6f166..dbe8c402b8 100755 --- a/offline_circ/process_koc.pl +++ b/offline_circ/process_koc.pl @@ -35,7 +35,7 @@ use C4::Items; use C4::Members; use C4::Stats; use C4::BackgroundJob; -use Koha::Upload; +use Koha::UploadedFiles; use Koha::Account; use Koha::Patrons; @@ -73,10 +73,11 @@ if ($completedJobID) { $template->param(transactions_loaded => 1); $template->param(messages => $results->{results}); } elsif ($fileID) { - my $upload = Koha::Upload->new->get({ id => $fileID, filehandle => 1 }); - my $fh = $upload->{fh}; - my $filename = $upload->{name}; - my @input_lines = <$fh>; + my $upload = Koha::UploadedFiles->find( $fileID ); + my $fh = $upload? $upload->file_handle: undef; + my $filename = $upload? $upload->filename: undef; + my @input_lines = $fh? <$fh>: (); + $fh->close if $fh; my $job = undef; diff --git a/opac/opac-retrieve-file.pl b/opac/opac-retrieve-file.pl index b443a7c2fe..cd2afbeb5b 100755 --- a/opac/opac-retrieve-file.pl +++ b/opac/opac-retrieve-file.pl @@ -25,13 +25,17 @@ use C4::Auth; use C4::Context; use C4::Output; use Koha::Upload; +use Koha::UploadedFiles; my $input = CGI::->new; my $hash = $input->param('id'); # historically called id (used in URLs?) -my $upl = Koha::Upload->new({ public => 1 }); -my $rec = $upl->get({ hashvalue => $hash, filehandle => 1 }); -my $fh = $rec->{fh}; +my $rec = Koha::UploadedFiles->search({ + hashvalue => $hash, public => 1, + # DO NOT REMOVE the public flag: this is an opac script ! +})->next; +my $fh = $rec? $rec->file_handle: undef; + if( !$rec || !$fh ) { my ( $template, $user, $cookie ) = get_template_and_user({ query => $input, @@ -42,7 +46,7 @@ if( !$rec || !$fh ) { $template->param( hash => $hash ); output_html_with_http_headers $input, $cookie, $template->output; } else { - my @hdr = $upl->httpheaders( $rec->{name} ); + my @hdr = Koha::Upload->httpheaders( $rec->filename ); print Encode::encode_utf8( $input->header( @hdr ) ); while( <$fh> ) { print $_; diff --git a/t/db_dependent/Upload.t b/t/db_dependent/Upload.t index 239874d648..b35e18b314 100644 --- a/t/db_dependent/Upload.t +++ b/t/db_dependent/Upload.t @@ -15,7 +15,6 @@ use Koha::UploadedFiles; my $schema = Koha::Database->new->schema; $schema->storage->txn_begin; -my $dbh = C4::Context->dbh; our $current_upload = 0; our $uploads = [ @@ -37,7 +36,7 @@ our $uploads = [ { name => 'file4', cat => undef, size => 5000 }, # temp duplicate ], [ - { name => 'file5', cat => undef, size => 7000 }, # temp duplicate + { name => 'file5', cat => undef, size => 7000 }, ], ]; @@ -51,11 +50,11 @@ $cgimod->mock( 'new' => \&newCGI ); # Start testing subtest 'Test01' => sub { - plan tests => 9; + plan tests => 11; test01(); }; subtest 'Test02' => sub { - plan tests => 4; + plan tests => 5; test02(); }; subtest 'Test03' => sub { @@ -71,7 +70,7 @@ subtest 'Test05' => sub { test05(); }; subtest 'Test06' => sub { - plan tests => 2; + plan tests => 3; test06(); }; subtest 'Test07' => sub { @@ -86,7 +85,8 @@ $schema->storage->txn_rollback; sub test01 { # Delete existing records (for later tests) - $dbh->do( "DELETE FROM uploaded_files" ); + # Passing keep_file suppresses warnings + Koha::UploadedFiles->new->delete({ keep_file => 1 }); # Check mocked directories is( Koha::UploadedFile->permanent_directory, $tempdir, @@ -101,16 +101,19 @@ sub test01 { my $res= $upl->result; is( $res =~ /^\d+,\d+$/, 1, 'Upload 1 includes two files' ); is( $upl->count, 2, 'Count returns 2 also' ); - foreach my $r ( $upl->get({ id => $res }) ) { - if( $r->{name} eq 'file1' ) { - is( $r->{uploadcategorycode}, 'A', 'Check category A' ); - is( $r->{filesize}, 6000, 'Check size of file1' ); - } elsif( $r->{name} eq 'file2' ) { - is( $r->{filesize}, 8000, 'Check size of file2' ); - is( $r->{public}, undef, 'Check public undefined' ); - } - } is( $upl->err, undef, 'No errors reported' ); + + my $rs = Koha::UploadedFiles->search({ + id => [ split ',', $res ] + }, { order_by => { -asc => 'filename' }}); + my $rec = $rs->next; + is( $rec->filename, 'file1', 'Check file name' ); + is( $rec->uploadcategorycode, 'A', 'Check category A' ); + is( $rec->filesize, 6000, 'Check size of file1' ); + $rec = $rs->next; + is( $rec->filename, 'file2', 'Check file name 2' ); + is( $rec->filesize, 8000, 'Check size of file2' ); + is( $rec->public, undef, 'Check public undefined' ); } sub test02 { @@ -121,18 +124,24 @@ sub test02 { my $cgi= $upl->cgi; is( $upl->count, 1, 'Upload 2 includes one file' ); my $res= $upl->result; - my $r = $upl->get({ id => $res, filehandle => 1 }); - is( $r->{uploadcategorycode}, 'B', 'Check category B' ); - is( $r->{public}, 1, 'Check public == 1' ); - is( ref($r->{fh}) eq 'IO::File' && $r->{fh}->opened, 1, 'Get returns a file handle' ); + my $rec = Koha::UploadedFiles->find( $res ); + is( $rec->uploadcategorycode, 'B', 'Check category B' ); + is( $rec->public, 1, 'Check public == 1' ); + my $fh = $rec->file_handle; + is( ref($fh) eq 'IO::File' && $fh->opened, 1, 'Get returns a file handle' ); + + my $orgname = $rec->filename; + $rec->filename( 'doesprobablynotexist' )->store; + is( $rec->file_handle, undef, 'Sabotage with file handle' ); + $rec->filename( $orgname )->store; } sub test03 { my $upl = Koha::Upload->new({ tmp => 1 }); #temporary my $cgi= $upl->cgi; is( $upl->count, 1, 'Upload 3 includes one temporary file' ); - my $r = $upl->get({ id => $upl->result }); - is( $r->{uploadcategorycode} =~ /_upload$/, 1, 'Check category temp file' ); + my $rec = Koha::UploadedFiles->find( $upl->result ); + is( $rec->uploadcategorycode =~ /_upload$/, 1, 'Check category temp file' ); } sub test04 { # Fail on a file already there @@ -151,34 +160,34 @@ sub test05 { # add temporary file with same name and contents, delete it my $cgi= $upl->cgi; is( $upl->count, 1, 'Upload 5 adds duplicate temporary file' ); my $id = $upl->result; - my $r = $upl->get({ id => $id }); + my $path = Koha::UploadedFiles->find( $id )->full_path; # testing delete via UploadedFiles (plural) my $delete = Koha::UploadedFiles->search({ id => $id })->delete; is( $delete, 1, 'Delete successful' ); - isnt( -e $r->{path}, 1, 'File no longer found after delete' ); - is( scalar $upl->get({ id => $id }), undef, 'Record also gone' ); + isnt( -e $path, 1, 'File no longer found after delete' ); + is( Koha::UploadedFiles->find( $id ), undef, 'Record also gone' ); # testing delete via UploadedFile (singular) # Note that find returns a Koha::Object $upl = Koha::Upload->new({ tmp => 1 }); $upl->cgi; - $id = $upl->result; - my $kohaobj = Koha::UploadedFiles->find( $id ); + my $kohaobj = Koha::UploadedFiles->find( $upl->result ); my $name = $kohaobj->filename; - my $path = $kohaobj->full_path; + $path = $kohaobj->full_path; $delete = $kohaobj->delete; is( $delete, $name, 'Delete successful' ); isnt( -e $path, 1, 'File no longer found after delete' ); } -sub test06 { #some extra tests for get - my $upl = Koha::Upload->new({ public => 1 }); - my @rec = $upl->get({ term => 'file' }); - is( @rec, 1, 'Get returns only one public result (file3)' ); - $upl = Koha::Upload->new; # public == 0 - @rec = $upl->get({ term => 'file' }); - is( @rec, 4, 'Get returns now four results' ); +sub test06 { #search_term with[out] private flag + my @recs = Koha::UploadedFiles->search_term({ term => 'file' }); + is( @recs, 1, 'Returns only one public result' ); + is( $recs[0]->filename, 'file3', 'Should be file3' ); + + is( Koha::UploadedFiles->search_term({ + term => 'file', include_private => 1, + })->count, 4, 'Returns now four results' ); } sub test07 { #simple test for httpheaders and getCategories @@ -237,7 +246,7 @@ subtest 'Some basic CRUD testing' => sub { my $upload01 = $builder->build({ source => 'UploadedFile' }); my $found = Koha::UploadedFiles->find( $upload01->{id} ); is( $found->id, $upload01->{id}, 'Koha::Object returns id' ); - $found->delete; + $found->delete({ keep_file => 1 }); #note that it does not exist $found = Koha::UploadedFiles->search( { id => $upload01->{id} }, ); diff --git a/tools/stage-marc-import.pl b/tools/stage-marc-import.pl index 99506fc562..63dd4e5130 100755 --- a/tools/stage-marc-import.pl +++ b/tools/stage-marc-import.pl @@ -39,7 +39,7 @@ use C4::Output; use C4::Biblio; use C4::ImportBatch; use C4::Matcher; -use Koha::Upload; +use Koha::UploadedFiles; use C4::BackgroundJob; use C4::MarcModificationTemplates; use Koha::Plugins; @@ -85,8 +85,10 @@ if ($completedJobID) { my $results = $job->results(); $template->param(map { $_ => $results->{$_} } keys %{ $results }); } elsif ($fileID) { - my $upload = Koha::Upload->new->get({ id => $fileID }); - my ( $file, $filename ) = ( $upload->{path}, $upload->{name} ); + my $upload = Koha::UploadedFiles->find( $fileID ); + my $file = $upload->full_path; + my $filename = $upload->filename; + my ( $errors, $marcrecords ); if( $format eq 'MARCXML' ) { ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromMARCXMLFile( $file, $encoding); diff --git a/tools/upload-cover-image.pl b/tools/upload-cover-image.pl index 79c37f25f3..dd73bbbe59 100755 --- a/tools/upload-cover-image.pl +++ b/tools/upload-cover-image.pl @@ -47,7 +47,7 @@ use C4::Context; use C4::Auth; use C4::Output; use C4::Images; -use Koha::Upload; +use Koha::UploadedFiles; use C4::Log; my $debug = 1; @@ -68,7 +68,7 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user( my $filetype = $input->param('filetype'); my $biblionumber = $input->param('biblionumber'); -my $uploadfilename = $input->param('uploadfile'); +#my $uploadfilename = $input->param('uploadfile'); # obsolete? my $replace = !C4::Context->preference("AllowMultipleCovers") || $input->param('replace'); my $op = $input->param('op'); @@ -83,10 +83,11 @@ $template->{VARS}->{'biblionumber'} = $biblionumber; my $total = 0; if ($fileID) { - my $upload = Koha::Upload->new->get({ id => $fileID, filehandle => 1 }); + my $upload = Koha::UploadedFiles->find( $fileID ); if ( $filetype eq 'image' ) { - my $fh = $upload->{fh}; + my $fh = $upload->file_handle; my $srcimage = GD::Image->new($fh); + $fh->close if $fh; if ( defined $srcimage ) { my $dberror = PutImage( $biblionumber, $srcimage, $replace ); if ($dberror) { @@ -102,7 +103,7 @@ if ($fileID) { undef $srcimage; } else { - my $filename = $upload->{path}; + my $filename = $upload->full_path; my $dirname = File::Temp::tempdir( CLEANUP => 1 ); unless ( system( "unzip", $filename, '-d', $dirname ) == 0 ) { $error = 'UZIPFAIL'; diff --git a/tools/upload.pl b/tools/upload.pl index ff31dd7247..a5d3b3cd08 100755 --- a/tools/upload.pl +++ b/tools/upload.pl @@ -24,6 +24,7 @@ use JSON; use C4::Auth; use C4::Output; use Koha::Upload; +use Koha::UploadedFiles; my $input = CGI::->new; my $op = $input->param('op') // 'new'; @@ -48,7 +49,6 @@ $template->param( plugin => $plugin, ); -my $upar = $plugin ? { public => 1 } : {}; if ( $op eq 'new' ) { $template->param( mode => 'new', @@ -57,12 +57,24 @@ if ( $op eq 'new' ) { output_html_with_http_headers $input, $cookie, $template->output; } elsif ( $op eq 'search' ) { - my $h = $id ? { id => $id } : { term => $term }; - my @uploads = Koha::Upload->new($upar)->get($h); + my $uploads; + if( $id ) { + my $rec = Koha::UploadedFiles->search({ + id => $id, + $plugin? ( public => 1 ) : (), + })->next; + push @$uploads, $rec->unblessed if $rec; + } else { + $uploads = Koha::UploadedFiles->search_term({ + term => $term, + $plugin? (): ( include_private => 1 ), + })->unblessed; + } + $template->param( mode => 'report', msg => $msg, - uploads => \@uploads, + uploads => $uploads, ); output_html_with_http_headers $input, $cookie, $template->output; @@ -86,18 +98,20 @@ if ( $op eq 'new' ) { output_html_with_http_headers $input, $cookie, $template->output; } elsif ( $op eq 'download' ) { - my $upl = Koha::Upload->new($upar); - my $rec = $upl->get( { id => $id, filehandle => 1 } ); - my $fh = $rec->{fh}; + my $rec = Koha::UploadedFiles->search({ + id => $id, + $plugin? ( public => 1 ) : (), + })->next; + my $fh = $rec? $rec->file_handle: undef; if ( !$rec || !$fh ) { $template->param( mode => 'new', msg => JSON::to_json( { $id => 5 } ), - uploadcategories => $upl->getCategories, + uploadcategories => Koha::Upload->getCategories, ); output_html_with_http_headers $input, $cookie, $template->output; } else { - my @hdr = $upl->httpheaders( $rec->{name} ); + my @hdr = Koha::Upload->httpheaders( $rec->filename ); print Encode::encode_utf8( $input->header(@hdr) ); while (<$fh>) { print $_; -- 2.11.0