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 <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-24 09:27:23 +01:00 committed by Kyle M Hall
parent 158442eb9e
commit ba5cd24553
5 changed files with 44 additions and 45 deletions

View File

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

View File

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

View File

@ -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 $_;
}

View File

@ -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' );
}

View File

@ -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 $_;
}