Bug 20695: Fix multiple upload result in tools/upload script
[koha.git] / tools / upload.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright (C) 2015 Rijksmuseum
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use CGI qw/-utf8/;
22 use JSON;
23
24 use C4::Auth;
25 use C4::Output;
26 use Koha::UploadedFiles;
27
28 my $input  = CGI::->new;
29 my $op     = $input->param('op') // 'new';
30 my $plugin = $input->param('plugin');
31 my $index  = $input->param('index');         # MARC editor input field id
32 my $term   = $input->param('term');
33 my $id     = $input->param('id');
34 my $msg    = $input->param('msg');
35
36 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
37     {   template_name   => "tools/upload.tt",
38         query           => $input,
39         type            => "intranet",
40         authnotrequired => 0,
41         flagsrequired   => { tools => 'upload_general_files' },
42     }
43 );
44
45 $template->param(
46     index      => $index,
47     owner      => $loggedinuser,
48     plugin     => $plugin,
49     uploadcategories => Koha::UploadedFiles->getCategories,
50 );
51
52 if ( $op eq 'new' ) {
53     $template->param(
54         mode             => 'new',
55     );
56     output_html_with_http_headers $input, $cookie, $template->output;
57
58 } elsif ( $op eq 'search' ) {
59     my $uploads;
60     if( $id ) { # might be a comma separated list
61         my @id = split /,/, $id;
62         foreach my $recid (@id) {
63             my $rec = Koha::UploadedFiles->find( $recid );
64             push @$uploads, $rec->unblessed
65                 if $rec && ( $rec->public || !$plugin );
66                 # Do not show private uploads in the plugin mode (:editor)
67         }
68     } else {
69         $uploads = Koha::UploadedFiles->search_term({
70             term => $term,
71             $plugin? (): ( include_private => 1 ),
72         })->unblessed;
73     }
74
75     $template->param(
76         mode    => 'report',
77         msg     => $msg,
78         uploads => $uploads,
79     );
80     output_html_with_http_headers $input, $cookie, $template->output;
81
82 } elsif ( $op eq 'delete' ) {
83     # delete only takes the id parameter
84     my $rec = Koha::UploadedFiles->find($id);
85     undef $rec if $rec && $plugin && !$rec->public;
86     my $fn = $rec ? $rec->filename : '';
87     my $delete = $rec ? $rec->delete : undef;
88     #TODO Improve error handling
89     my $msg = $delete
90         ? JSON::to_json({ $fn => { code => 6 }})
91         : $id
92         ? JSON::to_json({ $fn || $id, { code => 7 }})
93         : '';
94     $template->param(
95         mode             => 'deleted',
96         msg              => $msg,
97     );
98     output_html_with_http_headers $input, $cookie, $template->output;
99
100 } elsif ( $op eq 'download' ) {
101     my $rec = Koha::UploadedFiles->find( $id );
102     undef $rec if $rec && $plugin && !$rec->public;
103     my $fh  = $rec? $rec->file_handle:  undef;
104     if ( !$rec || !$fh ) {
105         $template->param(
106             mode             => 'new',
107             msg              => JSON::to_json({ $id => { code => 5 }}),
108         );
109         output_html_with_http_headers $input, $cookie, $template->output;
110     } else {
111         print Encode::encode_utf8( $input->header( $rec->httpheaders ) );
112         while (<$fh>) {
113             print $_;
114         }
115         $fh->close;
116     }
117 }