Bug 25265: Prevent double reindex of the same item in batchmod
[koha.git] / tools / upload-cover-image.pl
1 #!/usr/bin/perl
2 #
3 # Copyright 2011 C & P Bibliography Services
4 #
5 # This file is part of Koha.
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 #
21 #
22
23 =head1 NAME
24
25 upload-cover-image.pl - Script for handling uploading of both single and bulk coverimages and importing them into the database.
26
27 =head1 SYNOPSIS
28
29 upload-cover-image.pl
30
31 =head1 DESCRIPTION
32
33 This script is called and presents the user with an interface allowing him/her to upload a single cover image or bulk cover images via a zip file.
34 Images will be resized into thumbnails of 140x200 pixels and larger images of
35 800x600 pixels. If the images that are uploaded are larger, they will be
36 resized, maintaining aspect ratio.
37
38 =cut
39
40 use Modern::Perl;
41
42 use File::Temp;
43 use CGI qw ( -utf8 );
44 use GD;
45 use C4::Context;
46 use C4::Auth;
47 use C4::Output;
48 use C4::Images;
49 use Koha::UploadedFiles;
50 use C4::Log;
51
52 my $debug = 1;
53
54 my $input = new CGI;
55
56 my $fileID = $input->param('uploadedfileid');
57 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
58     {
59         template_name   => "tools/upload-images.tt",
60         query           => $input,
61         type            => "intranet",
62         flagsrequired   => { tools => 'upload_local_cover_images' },
63         debug           => 0,
64     }
65 );
66
67 my $filetype       = $input->param('filetype');
68 my $biblionumber   = $input->param('biblionumber');
69 #my $uploadfilename = $input->param('uploadfile'); # obsolete?
70 my $replace        = !C4::Context->preference("AllowMultipleCovers")
71   || $input->param('replace');
72 my $op        = $input->param('op');
73 my %cookies   = parse CGI::Cookie($cookie);
74 my $sessionID = $cookies{'CGISESSID'}->value;
75
76 my $error;
77
78 $template->{VARS}->{'filetype'}     = $filetype;
79 $template->{VARS}->{'biblionumber'} = $biblionumber;
80
81 my $total = 0;
82
83 if ($fileID) {
84     my $upload = Koha::UploadedFiles->find( $fileID );
85     if ( $filetype eq 'image' ) {
86         my $fh       = $upload->file_handle;
87         my $srcimage = GD::Image->new($fh);
88         $fh->close if $fh;
89         if ( defined $srcimage ) {
90             my $dberror = PutImage( $biblionumber, $srcimage, $replace );
91             if ($dberror) {
92                 $error = 'DBERR';
93             }
94             else {
95                 $total = 1;
96             }
97         }
98         else {
99             $error = 'OPNIMG';
100         }
101         undef $srcimage;
102     }
103     else {
104         my $filename = $upload->full_path;
105         my $dirname = File::Temp::tempdir( CLEANUP => 1 );
106         qx/unzip $filename -d $dirname/;
107         my $exit_code = $?;
108         unless ( $exit_code == 0 ) {
109             $error = 'UZIPFAIL';
110         }
111         else {
112             my @directories;
113             push @directories, "$dirname";
114             foreach my $recursive_dir (@directories) {
115                 my $dir;
116                 opendir $dir, $recursive_dir;
117                 while ( my $entry = readdir $dir ) {
118                     push @directories, "$recursive_dir/$entry"
119                       if ( -d "$recursive_dir/$entry" and $entry !~ /^[._]/ );
120                 }
121                 closedir $dir;
122             }
123             foreach my $dir (@directories) {
124                 my $file;
125                 if ( -e "$dir/idlink.txt" ) {
126                     $file = "$dir/idlink.txt";
127                 }
128                 elsif ( -e "$dir/datalink.txt" ) {
129                     $file = "$dir/datalink.txt";
130                 }
131                 else {
132                     next;
133                 }
134                 if ( open( my $fh, '<', $file ) ) {
135                     while ( my $line = <$fh> ) {
136                         my $delim =
137                             ( $line =~ /\t/ ) ? "\t"
138                           : ( $line =~ /,/ )  ? ","
139                           :                     "";
140
141                         #$debug and warn "Delimeter is \'$delim\'";
142                         unless ( $delim eq "," || $delim eq "\t" ) {
143                             warn
144 "Unrecognized or missing field delimeter. Please verify that you are using either a ',' or a 'tab'";
145                             $error = 'DELERR';
146                         }
147                         else {
148                             ( $biblionumber, $filename ) = split $delim, $line, 2;
149                             $biblionumber =~
150                               s/[\"\r\n]//g;    # remove offensive characters
151                             $filename =~ s/[\"\r\n]//g;
152                             $filename =~ s/^\s+//;
153                             $filename =~ s/\s+$//;
154                             if (C4::Context->preference("CataloguingLog")) {
155                                 logaction('CATALOGUING', 'MODIFY', $biblionumber, "biblio cover image: $filename");
156                             }
157                             my $srcimage = GD::Image->new("$dir/$filename");
158                             if ( defined $srcimage ) {
159                                 $total++;
160                                 my $dberror =
161                                   PutImage( $biblionumber, $srcimage,
162                                     $replace );
163                                 if ($dberror) {
164                                     $error = 'DBERR';
165                                 }
166                             }
167                             else {
168                                 $error = 'OPNIMG';
169                             }
170                             undef $srcimage;
171                         }
172                     }
173                     close($fh);
174                 }
175                 else {
176                     $error = 'OPNLINK';
177                 }
178             }
179         }
180     }
181     $template->{VARS}->{'total'}        = $total;
182     $template->{VARS}->{'uploadimage'}  = 1;
183     $template->{VARS}->{'error'}        = $error;
184     $template->{VARS}->{'biblionumber'} = $biblionumber;
185 }
186
187 output_html_with_http_headers $input, $cookie, $template->output;
188
189 exit 0;
190
191 =head1 AUTHORS
192
193 Written by Jared Camins-Esakov of C & P Bibliography Services, in part based on
194 code by Koustubha Kale of Anant Corporation and Chris Nighswonger of Foundation
195 Bible College.
196
197 =cut