f55d8f128d9c8c12b020d2b26a80d29117cf1876
[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 qw( get_template_and_user );
47 use C4::Output qw( output_html_with_http_headers );
48 use Koha::Biblios;
49 use Koha::CoverImages;
50 use Koha::Items;
51 use Koha::UploadedFiles;
52 use C4::Log qw( logaction );
53
54 my $input = CGI->new;
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     }
64 );
65
66 my $filetype       = $input->param('filetype');
67 my $biblionumber   = $input->param('biblionumber');
68 my $itemnumber     = $input->param('itemnumber');
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->param(
79     filetype     => $filetype,
80     biblionumber => $biblionumber,
81     itemnumber   => $itemnumber,
82 );
83
84 my $total = 0;
85
86 if ($fileID) {
87     my $upload = Koha::UploadedFiles->find( $fileID );
88     if ( $filetype eq 'image' ) {
89         my $fh       = $upload->file_handle;
90         my $srcimage = GD::Image->new($fh);
91         $fh->close if $fh;
92         if ( defined $srcimage ) {
93             eval {
94                 if ( $replace ) {
95                     if ( $biblionumber ) {
96                         Koha::Biblios->find($biblionumber)->cover_images->delete;
97                     } elsif ( $itemnumber ) {
98                         Koha::Items->find($itemnumber)->cover_images->delete;
99                     }
100                 }
101
102                 Koha::CoverImage->new(
103                     {
104                         biblionumber => $biblionumber,
105                         itemnumber   => $itemnumber,
106                         src_image    => $srcimage
107                     }
108                 )->store;
109             };
110
111             if ($@) {
112                 warn $@;
113                 $error = 'DBERR';
114             }
115             else {
116                 $total = 1;
117             }
118         }
119         else {
120             $error = 'OPNIMG';
121         }
122         undef $srcimage;
123     }
124     else {
125         my $filename = $upload->full_path;
126         my $dirname = File::Temp::tempdir( CLEANUP => 1 );
127         qx/unzip $filename -d $dirname/;
128         my $exit_code = $?;
129         unless ( $exit_code == 0 ) {
130             $error = 'UZIPFAIL';
131         }
132         else {
133             my @directories;
134             push @directories, "$dirname";
135             foreach my $recursive_dir (@directories) {
136                 my $dir;
137                 opendir $dir, $recursive_dir;
138                 while ( my $entry = readdir $dir ) {
139                     push @directories, "$recursive_dir/$entry"
140                       if ( -d "$recursive_dir/$entry" and $entry !~ /^[._]/ );
141                 }
142                 closedir $dir;
143             }
144             foreach my $dir (@directories) {
145                 my $file;
146                 if ( -e "$dir/idlink.txt" ) {
147                     $file = "$dir/idlink.txt";
148                 }
149                 elsif ( -e "$dir/datalink.txt" ) {
150                     $file = "$dir/datalink.txt";
151                 }
152                 else {
153                     next;
154                 }
155                 if ( open( my $fh, '<', $file ) ) {
156                     while ( my $line = <$fh> ) {
157                         my $delim =
158                             ( $line =~ /\t/ ) ? "\t"
159                           : ( $line =~ /,/ )  ? ","
160                           :                     "";
161
162                         unless ( $delim eq "," || $delim eq "\t" ) {
163                             warn
164 "Unrecognized or missing field delimeter. Please verify that you are using either a ',' or a 'tab'";
165                             $error = 'DELERR';
166                         }
167                         else {
168                             ( $biblionumber, $filename ) = split $delim, $line, 2;
169                             $biblionumber =~
170                               s/[\"\r\n]//g;    # remove offensive characters
171                             $filename =~ s/[\"\r\n]//g;
172                             $filename =~ s/^\s+//;
173                             $filename =~ s/\s+$//;
174                             if (C4::Context->preference("CataloguingLog")) {
175                                 logaction('CATALOGUING', 'MODIFY', $biblionumber, "biblio cover image: $filename");
176                             }
177                             my $srcimage = GD::Image->new("$dir/$filename");
178                             if ( defined $srcimage ) {
179                                 $total++;
180                                 eval {
181                                     if ( $replace ) {
182                                         if ( $biblionumber ) {
183                                             Koha::Biblios->find($biblionumber)->cover_images->delete;
184                                         } elsif ( $itemnumber ) {
185                                             Koha::Items->find($itemnumber)->cover_images->delete;
186                                         }
187                                     }
188
189                                     Koha::CoverImage->new(
190                                         {
191                                             biblionumber => $biblionumber,
192                                             itemnumber   => $itemnumber,
193                                             src_image    => $srcimage
194                                         }
195                                     )->store;
196                                 };
197
198                                 if ($@) {
199                                     $error = 'DBERR';
200                                 }
201                             }
202                             else {
203                                 $error = 'OPNIMG';
204                             }
205                             undef $srcimage;
206                         }
207                     }
208                     close($fh);
209                 }
210                 else {
211                     $error = 'OPNLINK';
212                 }
213             }
214         }
215     }
216
217     $template->param(
218         total        => $total,
219         uploadimage  => 1,
220         error        => $error,
221         biblionumber => $biblionumber || Koha::Items->find($itemnumber)->biblionumber,
222         itemnumber   => $itemnumber,
223     );
224 }
225
226 output_html_with_http_headers $input, $cookie, $template->output;
227
228 exit 0;
229
230 =head1 AUTHORS
231
232 Written by Jared Camins-Esakov of C & P Bibliography Services, in part based on
233 code by Koustubha Kale of Anant Corporation and Chris Nighswonger of Foundation
234 Bible College.
235
236 =cut