From 5b019381d6f682ec5e459e8705cd1e1ccd03ea92 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Thu, 24 Nov 2016 09:27:23 +0100 Subject: [PATCH] Bug 17501: Move getCategories and httpheaders from Upload.pm Class method getCategories has no strict binding to Upload.pm. While Upload.pm is now restricted to the actual uploading process with CGI hook, this routine fits better in the UploadedFile package. Class method httpheaders can be moved as well for the same reason. Note that it actually is an instance method. The parameter $name is dropped. Test plan: [1] Run t/db_dependent/Upload.t. [2] Check the categories in the combo box of tools/upload. [3] Check a download via tools/upload and opac-retrieve-file. Signed-off-by: Marcel de Rooy Signed-off-by: Tomas Cohen Arazi Signed-off-by: Jonathan Druart --- Koha/Upload.pm | 39 ++++++--------------------------------- Koha/UploadedFile.pm | 29 ++++++++++++++++++++++++++++- opac/opac-retrieve-file.pl | 4 +--- t/db_dependent/Upload.t | 6 ++++-- tools/upload.pl | 11 +++++------ 5 files changed, 44 insertions(+), 45 deletions(-) diff --git a/Koha/Upload.pm b/Koha/Upload.pm index 6a375cd..9879fa1 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::UploadedFile; use Koha::UploadedFiles; # add an upload (see tools/upload-file.pl) @@ -34,15 +35,14 @@ Koha::Upload - Facilitate file uploads (temporary and permanent) my $cgi = $upload->cgi; # Do something with $upload->count, $upload->result or $upload->err - # get some upload records (in staff) + # get some upload records (in staff) via Koha::UploadedFiles my @uploads1 = Koha::UploadedFiles->search({ filename => $name }); my @uploads2 = Koha::UploadedFiles->search_term({ term => $term }); - # staff download + # staff download (via Koha::UploadedFile[s]) 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 ) ); + print Encode::encode_utf8( $input->header( $rec->httpheaders ) ); while( <$fh> ) { print $_; } $fh->close; @@ -56,8 +56,6 @@ Koha::Upload - Facilitate file uploads (temporary and permanent) The module has been revised to use Koha::Object[s]; the delete method has been moved to Koha::UploadedFile[s], as well as the get method. -=head1 INSTANCE METHODS - =cut use constant KOHA_UPLOAD => 'koha_upload'; @@ -80,6 +78,8 @@ use Koha::UploadedFiles; __PACKAGE__->mk_ro_accessors( qw|| ); +=head1 INSTANCE METHODS + =head2 new Returns new object based on Class::Accessor. @@ -160,33 +160,6 @@ sub err { =head1 CLASS METHODS -=head2 getCategories - - getCategories returns a list of upload category codes and names - -=cut - -sub getCategories { - my ( $class ) = @_; - my $cats = C4::Koha::GetAuthorisedValues('UPLOAD'); - [ map {{ code => $_->{authorised_value}, name => $_->{lib} }} @$cats ]; -} - -=head2 httpheaders - - httpheaders returns http headers for a retrievable upload - Will be extended by report 14282 - -=cut - -sub httpheaders { - my ( $class, $name ) = @_; - return ( - '-type' => 'application/octet-stream', - '-attachment' => $name, - ); -} - =head2 allows_add_by allows_add_by checks if $userid has permission to add uploaded files diff --git a/Koha/UploadedFile.pm b/Koha/UploadedFile.pm index 4b2a151..085037d 100644 --- a/Koha/UploadedFile.pm +++ b/Koha/UploadedFile.pm @@ -101,9 +101,24 @@ sub file_handle { return $self->{_file_handle}; } +=head3 httpheaders + + httpheaders returns http headers for a retrievable upload + Will be extended by report 14282 + +=cut + +sub httpheaders { + my ( $self ) = @_; + return ( + '-type' => 'application/octet-stream', + '-attachment' => $self->filename, + ); +} + =head2 CLASS METHODS -=head3 root_directory +=head3 permanent_directory =cut @@ -121,6 +136,18 @@ sub temporary_directory { return File::Spec->tmpdir; } +=head3 getCategories + + getCategories returns a list of upload category codes and names + +=cut + +sub getCategories { + my ( $class ) = @_; + my $cats = C4::Koha::GetAuthorisedValues('UPLOAD'); + [ map {{ code => $_->{authorised_value}, name => $_->{lib} }} @$cats ]; +} + =head3 _type Returns name of corresponding DBIC resultset diff --git a/opac/opac-retrieve-file.pl b/opac/opac-retrieve-file.pl index cd2afbe..d19f9dd 100755 --- a/opac/opac-retrieve-file.pl +++ b/opac/opac-retrieve-file.pl @@ -24,7 +24,6 @@ use Encode; use C4::Auth; use C4::Context; use C4::Output; -use Koha::Upload; use Koha::UploadedFiles; my $input = CGI::->new; @@ -46,8 +45,7 @@ if( !$rec || !$fh ) { $template->param( hash => $hash ); output_html_with_http_headers $input, $cookie, $template->output; } else { - my @hdr = Koha::Upload->httpheaders( $rec->filename ); - print Encode::encode_utf8( $input->header( @hdr ) ); + print Encode::encode_utf8( $input->header( $rec->httpheaders ) ); while( <$fh> ) { print $_; } diff --git a/t/db_dependent/Upload.t b/t/db_dependent/Upload.t index b35e18b..b6fc223 100644 --- a/t/db_dependent/Upload.t +++ b/t/db_dependent/Upload.t @@ -11,6 +11,7 @@ use t::lib::TestBuilder; use C4::Context; use Koha::Database; use Koha::Upload; +use Koha::UploadedFile; use Koha::UploadedFiles; my $schema = Koha::Database->new->schema; @@ -191,11 +192,12 @@ sub test06 { #search_term with[out] private flag } sub test07 { #simple test for httpheaders and getCategories - my @hdrs = Koha::Upload->httpheaders('does_not_matter_yet'); + my $rec = Koha::UploadedFiles->search_term({ term => 'file' })->next; + my @hdrs = $rec->httpheaders; is( @hdrs == 4 && $hdrs[1] =~ /application\/octet-stream/, 1, 'Simple test for httpheaders'); my $builder = t::lib::TestBuilder->new; $builder->build({ source => 'AuthorisedValue', value => { category => 'UPLOAD', authorised_value => 'HAVE_AT_LEAST_ONE', lib => 'Hi there' } }); - my $cat = Koha::Upload->getCategories; + my $cat = Koha::UploadedFile->getCategories; is( @$cat >= 1, 1, 'getCategories returned at least one category' ); } diff --git a/tools/upload.pl b/tools/upload.pl index a5d3b3c..76f2cb6 100755 --- a/tools/upload.pl +++ b/tools/upload.pl @@ -23,7 +23,7 @@ use JSON; use C4::Auth; use C4::Output; -use Koha::Upload; +use Koha::UploadedFile; use Koha::UploadedFiles; my $input = CGI::->new; @@ -52,7 +52,7 @@ $template->param( if ( $op eq 'new' ) { $template->param( mode => 'new', - uploadcategories => Koha::Upload->getCategories, + uploadcategories => Koha::UploadedFile->getCategories, ); output_html_with_http_headers $input, $cookie, $template->output; @@ -93,7 +93,7 @@ if ( $op eq 'new' ) { $template->param( mode => 'deleted', msg => $msg, - uploadcategories => Koha::Upload->getCategories, + uploadcategories => Koha::UploadedFile->getCategories, ); output_html_with_http_headers $input, $cookie, $template->output; @@ -107,12 +107,11 @@ if ( $op eq 'new' ) { $template->param( mode => 'new', msg => JSON::to_json( { $id => 5 } ), - uploadcategories => Koha::Upload->getCategories, + uploadcategories => Koha::UploadedFile->getCategories, ); output_html_with_http_headers $input, $cookie, $template->output; } else { - my @hdr = Koha::Upload->httpheaders( $rec->filename ); - print Encode::encode_utf8( $input->header(@hdr) ); + print Encode::encode_utf8( $input->header( $rec->httpheaders ) ); while (<$fh>) { print $_; } -- 2.1.4