@@ -, +, @@ --- 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(-) --- a/Koha/Upload.pm +++ a/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 --- a/Koha/UploadedFile.pm +++ a/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 --- a/opac/opac-retrieve-file.pl +++ a/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 $_; } --- a/t/db_dependent/Upload.t +++ a/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' ); } --- a/tools/upload.pl +++ a/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 $_; } --