Bug 19915: (QA follow-up) Tidy up GetItemsForInventory.t
[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 use constant ERR_READING     => 'UPLERR_FILE_NOT_READ';
29 use constant ALERT_DELETED   => 'UPL_FILE_DELETED'; # alert, no error
30 use constant ERR_NOT_DELETED => 'UPLERR_FILE_NOT_DELETED';
31
32 my $input  = CGI::->new;
33 my $op     = $input->param('op') // 'new';
34 my $plugin = $input->param('plugin');
35 my $index  = $input->param('index');         # MARC editor input field id
36 my $term   = $input->param('term');
37 my $id     = $input->param('id');
38 my $msg    = $input->param('msg');
39
40 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
41     {   template_name   => "tools/upload.tt",
42         query           => $input,
43         type            => "intranet",
44         authnotrequired => 0,
45         flagsrequired   => { tools => 'upload_general_files' },
46     }
47 );
48
49 $template->param(
50     index      => $index,
51     owner      => $loggedinuser,
52     plugin     => $plugin,
53     uploadcategories => Koha::UploadedFiles->getCategories,
54 );
55
56 if ( $op eq 'new' ) {
57     $template->param(
58         mode             => 'new',
59     );
60     output_html_with_http_headers $input, $cookie, $template->output;
61
62 } elsif ( $op eq 'search' ) {
63     my $uploads;
64     if( $id ) { # might be a comma separated list
65         my @id = split /,/, $id;
66         foreach my $recid (@id) {
67             my $rec = Koha::UploadedFiles->find( $recid );
68             push @$uploads, $rec->unblessed
69                 if $rec && ( $rec->public || !$plugin );
70                 # Do not show private uploads in the plugin mode (:editor)
71         }
72     } else {
73         $uploads = Koha::UploadedFiles->search_term({
74             term => $term,
75             $plugin? (): ( include_private => 1 ),
76         })->unblessed;
77     }
78
79     $template->param(
80         mode    => 'report',
81         msg     => $msg,
82         uploads => $uploads,
83     );
84     output_html_with_http_headers $input, $cookie, $template->output;
85
86 } elsif ( $op eq 'delete' ) {
87     # delete only takes the id parameter
88     my $rec = Koha::UploadedFiles->find($id);
89     undef $rec if $rec && $plugin && !$rec->public;
90     my $fn = $rec ? $rec->filename : '';
91     my $delete = $rec ? $rec->delete : undef;
92     #TODO Improve error handling
93     my $msg = $delete
94         ? JSON::to_json({ $fn => { code => ALERT_DELETED }})
95         : $id
96         ? JSON::to_json({ $fn || $id, { code => ERR_NOT_DELETED }})
97         : '';
98     $template->param(
99         mode             => 'deleted',
100         msg              => $msg,
101     );
102     output_html_with_http_headers $input, $cookie, $template->output;
103
104 } elsif ( $op eq 'download' ) {
105     my $rec = Koha::UploadedFiles->find( $id );
106     undef $rec if $rec && $plugin && !$rec->public;
107     my $fh  = $rec? $rec->file_handle:  undef;
108     if ( !$rec || !$fh ) {
109         $template->param(
110             mode             => 'new',
111             msg              => JSON::to_json({ $id => { code => ERR_READING }}),
112         );
113         output_html_with_http_headers $input, $cookie, $template->output;
114     } else {
115         print Encode::encode_utf8( $input->header( $rec->httpheaders ) );
116         while (<$fh>) {
117             print $_;
118         }
119         $fh->close;
120     }
121 }