Merge remote-tracking branch 'origin/new/bug_5347'
[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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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 strict;
41 use warnings;
42
43 use File::Temp;
44 use CGI;
45 use GD;
46 use C4::Context;
47 use C4::Auth;
48 use C4::Output;
49 use C4::Images;
50 use C4::UploadedFile;
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.tmpl",
60         query           => $input,
61         type            => "intranet",
62         authnotrequired => 0,
63         flagsrequired   => { tools => 'upload_cover_images' },
64         debug           => 0,
65     }
66 );
67
68 my $filetype       = $input->param('filetype');
69 my $biblionumber   = $input->param('biblionumber');
70 my $uploadfilename = $input->param('uploadfile');
71 my $replace        = !C4::Context->preference("AllowMultipleCovers")
72   || $input->param('replace');
73 my $op        = $input->param('op');
74 my %cookies   = parse CGI::Cookie($cookie);
75 my $sessionID = $cookies{'CGISESSID'}->value;
76
77 my $error;
78
79 $template->{VARS}->{'filetype'}     = $filetype;
80 $template->{VARS}->{'biblionumber'} = $biblionumber;
81
82 my $total = 0;
83
84 if ($fileID) {
85     my $uploaded_file = C4::UploadedFile->fetch( $sessionID, $fileID );
86     if ( $filetype eq 'image' ) {
87         my $fh       = $uploaded_file->fh();
88         my $srcimage = GD::Image->new($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 = $uploaded_file->filename();
105         my $dirname = File::Temp::tempdir( CLEANUP => 1 );
106         unless ( system( "unzip", $filename, '-d', $dirname ) == 0 ) {
107             $error = 'UZIPFAIL';
108         }
109         else {
110             my @directories;
111             push @directories, "$dirname";
112             foreach my $recursive_dir (@directories) {
113                 my $dir;
114                 opendir $dir, $recursive_dir;
115                 while ( my $entry = readdir $dir ) {
116                     push @directories, "$recursive_dir/$entry"
117                       if ( -d "$recursive_dir/$entry" and $entry !~ /^[._]/ );
118                 }
119                 closedir $dir;
120             }
121             foreach my $dir (@directories) {
122                 my $file;
123                 if ( -e "$dir/idlink.txt" ) {
124                     $file = "$dir/idlink.txt";
125                 }
126                 elsif ( -e "$dir/datalink.txt" ) {
127                     $file = "$dir/datalink.txt";
128                 }
129                 else {
130                     next;
131                 }
132                 if ( open( FILE, $file ) ) {
133                     while ( my $line = <FILE> ) {
134                         my $delim =
135                             ( $line =~ /\t/ ) ? "\t"
136                           : ( $line =~ /,/ )  ? ","
137                           :                     "";
138
139                         #$debug and warn "Delimeter is \'$delim\'";
140                         unless ( $delim eq "," || $delim eq "\t" ) {
141                             warn
142 "Unrecognized or missing field delimeter. Please verify that you are using either a ',' or a 'tab'";
143                             $error = 'DELERR';
144                         }
145                         else {
146                             ( $biblionumber, $filename ) = split $delim, $line;
147                             $biblionumber =~
148                               s/[\"\r\n]//g;    # remove offensive characters
149                             $filename =~ s/[\"\r\n\s]//g;
150                             my $srcimage = GD::Image->new("$dir/$filename");
151                             if ( defined $srcimage ) {
152                                 $total++;
153                                 my $dberror =
154                                   PutImage( $biblionumber, $srcimage,
155                                     $replace );
156                                 if ($dberror) {
157                                     $error = 'DBERR';
158                                 }
159                             }
160                             else {
161                                 $error = 'OPNIMG';
162                             }
163                             undef $srcimage;
164                         }
165                     }
166                     close(FILE);
167                 }
168                 else {
169                     $error = 'OPNLINK';
170                 }
171             }
172         }
173     }
174     $template->{VARS}->{'total'}        = $total;
175     $template->{VARS}->{'uploadimage'}  = 1;
176     $template->{VARS}->{'error'}        = $error;
177     $template->{VARS}->{'biblionumber'} = $biblionumber;
178 }
179
180 output_html_with_http_headers $input, $cookie, $template->output;
181
182 exit 0;
183
184 =head1 AUTHORS
185
186 Written by Jared Camins-Esakov of C & P Bibliography Services, in part based on
187 code by Koustubha Kale of Anant Corporation and Chris Nighswonger of Foundation
188 Bible College.
189
190 =cut