Bug 30128: DBrev 21.12.00.017
[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 qw( get_template_and_user );
25 use C4::Output qw( output_html_with_http_headers );
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 my $browsecategory = $input->param('browsecategory');
40
41 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
42     {   template_name   => "tools/upload.tt",
43         query           => $input,
44         type            => "intranet",
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 'browse' ) {
63     my $uploads;
64     if ($browsecategory){
65         $uploads = Koha::UploadedFiles->search({
66             uploadcategorycode => $browsecategory,
67             $plugin? ( public => 1 ): (),
68         })->unblessed;
69     }
70
71     $template->param(
72         mode           => 'report',
73         msg            => $msg,
74         uploads        => $uploads,
75         browsecategory => $browsecategory,
76     );
77     output_html_with_http_headers $input, $cookie, $template->output;
78
79 } elsif ( $op eq 'search' ) {
80     my $uploads;
81     if( $id ) { # might be a comma separated list
82         my @id = split /,/, $id;
83         foreach my $recid (@id) {
84             my $rec = Koha::UploadedFiles->find( $recid );
85             push @$uploads, $rec->unblessed
86                 if $rec && ( $rec->public || !$plugin );
87                 # Do not show private uploads in the plugin mode (:editor)
88         }
89     } else {
90         $uploads = Koha::UploadedFiles->search_term({
91             term => $term,
92             $plugin? (): ( include_private => 1 ),
93         })->unblessed;
94     }
95
96     $template->param(
97         mode    => 'report',
98         msg     => $msg,
99         uploads => $uploads,
100     );
101     output_html_with_http_headers $input, $cookie, $template->output;
102
103 } elsif ( $op eq 'delete' ) {
104     # delete only takes the id parameter
105     my $rec = Koha::UploadedFiles->find($id);
106     undef $rec if $rec && $plugin && !$rec->public;
107     my $fn = $rec ? $rec->filename : '';
108     my $delete = $rec ? $rec->delete : undef;
109     #TODO Improve error handling
110     my $msg = $delete
111         ? JSON::to_json({ $fn => { code => ALERT_DELETED }})
112         : $id
113         ? JSON::to_json({ $fn || $id, { code => ERR_NOT_DELETED }})
114         : '';
115     $template->param(
116         mode             => 'deleted',
117         msg              => $msg,
118     );
119     output_html_with_http_headers $input, $cookie, $template->output;
120
121 } elsif ( $op eq 'download' ) {
122     my $rec = Koha::UploadedFiles->find( $id );
123     undef $rec if $rec && $plugin && !$rec->public;
124     my $fh  = $rec? $rec->file_handle:  undef;
125     if ( !$rec || !$fh ) {
126         $template->param(
127             mode             => 'new',
128             msg              => JSON::to_json({ $id => { code => ERR_READING }}),
129         );
130         output_html_with_http_headers $input, $cookie, $template->output;
131     } else {
132         print Encode::encode_utf8( $input->header( $rec->httpheaders ) );
133         while (<$fh>) {
134             print $_;
135         }
136         $fh->close;
137     }
138 }