4 # This file is part of Koha.
6 # Koha is free software; you can redistribute it and/or modify it under the
7 # terms of the GNU General Public License as published by the Free Software
8 # Foundation; either version 2 of the License, or (at your option) any later
11 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along with
16 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
17 # Suite 330, Boston, MA 02111-1307 USA
35 my ($template, $loggedinuser, $cookie)
36 = get_template_and_user({template_name => "tools/picture-upload.tmpl",
40 flagsrequired => {management => 1, tools => 'batch_upload_patron_images'},
44 my $filetype = $input->param('filetype');
45 my $cardnumber = $input->param('cardnumber');
46 my $uploadfilename = $input->param('uploadfile');
47 my $uploadfile = $input->upload('uploadfile');
48 my $borrowernumber = $input->param('borrowernumber');
49 my $op = $input->param('op');
51 #FIXME: This code is really in the rough. The variables need to be re-scoped as the two subs depend on global vars to operate.
52 # Other parts of this code could be optimized as well, I think. Perhaps the file upload could be done with YUI's upload
55 $debug and warn "Params are: filetype=$filetype, cardnumber=$cardnumber, borrowernumber=$borrowernumber, uploadfile=$uploadfilename";
59 picture-upload.p. - Script for handling uploading of both single and bulk patronimages and importing them into the database.
67 This script is called and presents the user with an interface allowing him/her to upload a single patron image or bulk patron images via a zip file.
68 Files greater than 100K will be refused. Images should be 140x200 pixels. If they are larger they will be auto-resized to comply.
72 $debug and warn "Operation requested: $op";
74 my ( $total, $handled, @counts, $tempfile, $tfh );
76 if ( ($op eq 'Upload') && $uploadfile ) { # Case is important in these operational values as the template must use case to be visually pleasing!
77 my $dirname = File::Temp::tempdir( CLEANUP => 1);
78 $debug and warn "dirname = $dirname";
79 my $filesuffix = $1 if $uploadfilename =~ m/(\..+)$/i;
80 ( $tfh, $tempfile ) = File::Temp::tempfile( SUFFIX => $filesuffix, UNLINK => 1 );
81 $debug and warn "tempfile = $tempfile";
82 my ( @directories, $errors );
84 $errors{'NOTZIP'} = 1 if ( $uploadfilename !~ /\.zip$/i && $filetype =~ m/zip/i );
85 $errors{'NOWRITETEMP'} = 1 unless ( -w $dirname );
86 $errors{'EMPTYUPLOAD'} = 1 unless ( length( $uploadfile ) > 0 );
89 $template->param( ERRORS => [ \%errors ] );
91 while ( <$uploadfile> ) {
96 if ( $filetype eq 'zip' ) {
97 unless (system("unzip $tempfile -d $dirname") == 0) {
98 $errors{'UZIPFAIL'} = $uploadfilename;
99 $template->param( ERRORS => [ \%errors ] );
100 output_html_with_http_headers $input, $cookie, $template->output; # This error is fatal to the import, so bail out here
103 push @directories, "$dirname";
104 foreach $recursive_dir ( @directories ) {
105 opendir $dir, $recursive_dir;
106 while ( my $entry = readdir $dir ) {
107 push @directories, "$recursive_dir/$entry" if ( -d "$recursive_dir/$entry" and $entry !~ /^\./ );
108 $debug and warn "$recursive_dir/$entry";
113 foreach my $dir ( @directories ) {
114 $results = handle_dir( $dir, $filesuffix );
115 $handled++ if $results == 1;
117 $total = scalar @directories;
118 } else { #if ($filetype eq 'zip' )
119 $results = handle_dir( $dirname, $filesuffix );
124 if ( %$results || %errors ) {
125 $template->param( ERRORS => [ \%$results ] );
127 $debug and warn "Total files processed: $total";
128 warn "Errors in \$errors." if $errors;
133 TCOUNTS => scalar(@counts),
137 } elsif ( ($op eq 'Upload') && !$uploadfile ) {
138 warn "Problem uploading file or no file uploaded.";
139 $template->param(cardnumber => $cardnumber);
140 $template->param(filetype => $filetype);
141 } elsif ( $op eq 'Delete' ) {
142 my $dberror = RmPatronImage($cardnumber);
143 $debug and warn "Patron image deleted for $cardnumber";
144 warn "Database returned $dberror" if $dberror;
146 if ( $borrowernumber && !$errors && !$template->param('ERRORS') ) {
147 print $input->redirect ("/cgi-bin/koha/members/moremember.pl?borrowernumber=$borrowernumber");
149 output_html_with_http_headers $input, $cookie, $template->output;
153 my ( $dir, $suffix ) = @_;
155 $debug and warn "Entering sub handle_dir; passed \$dir=$dir, \$suffix=$suffix";
156 if ($suffix =~ m/zip/i) { # If we were sent a zip file, process any included data/idlink.txt files
157 my ( $file, $filename, $cardnumber );
158 $debug and warn "Passed a zip file.";
159 opendir my $dirhandle, $dir;
160 while ( my $filename = readdir $dirhandle ) {
161 $file = "$dir/$filename" if ($filename =~ m/datalink\.txt/i || $filename =~ m/idlink\.txt/i);
163 unless (open (FILE, $file)) {
164 warn "Opening $dir/$file failed!";
165 $errors{'OPNLINK'} = $file;
166 return $errors; # This error is fatal to the import of this directory contents, so bail and return the error to the caller
169 while (my $line = <FILE>) {
170 $debug and warn "Reading contents of $file";
172 $debug and warn "Examining line: $line";
173 my $delim = ($line =~ /\t/) ? "\t" : ($line =~ /,/) ? "," : "";
174 $debug and warn "Delimeter is \'$delim\'";
175 unless ( $delim eq "," || $delim eq "\t" ) {
176 warn "Unrecognized or missing field delimeter. Please verify that you are using either a ',' or a 'tab'";
177 $errors{'DELERR'} = 1; # This error is fatal to the import of this directory contents, so bail and return the error to the caller
180 ($cardnumber, $filename) = split $delim, $line;
181 $cardnumber =~ s/[\"\r\n]//g; # remove offensive characters
182 $filename =~ s/[\"\r\n\s]//g;
183 $debug and warn "Cardnumber: $cardnumber Filename: $filename";
184 $source = "$dir/$filename";
185 %counts = handle_file($cardnumber, $source, %counts);
188 closedir ($dirhandle);
191 %counts = handle_file($cardnumber, $source, %counts);
193 push @counts, \%counts;
198 my ($cardnumber, $source, %count) = @_;
199 $debug and warn "Entering sub handle_file; passed \$cardnumber=$cardnumber, \$source=$source";
200 $count{filenames} = () if !$count{filenames};
201 $count{source} = $source if !$count{source};
202 if ($cardnumber && $source) { # Now process any imagefiles
205 if ($filetype eq 'image') {
206 $filename = $uploadfilename;
208 $filename = $1 if ($source =~ /\/([^\/]+)$/);
210 $debug and warn "Source: $source";
211 my $size = (stat($source))[7];
212 if ($size > 100000) { # This check is necessary even with image resizing to avoid possible security/performance issues...
213 warn "$filename is TOO BIG!!! I refuse to beleagur my database with that much data. Try reducing the pixel dimensions and I\'ll reconsider.";
214 $filerrors{'OVRSIZ'} = 1;
215 push my @filerrors, \%filerrors;
216 push @{ $count{filenames} }, { filerrors => \@filerrors, source => $filename, cardnumber => $cardnumber };
217 $template->param( ERRORS => 1 );
218 return %count; # this one is fatal so bail here...
220 my $image = Image::Magick->new;
221 if (open (IMG, "$source")) {
222 $image->Read(file=>\*IMG);
224 my $mimetype = $image->Get('mime');
225 # Check the pixel size of the image we are about to import...
226 my ($height, $width) = $image->Get('height', 'width');
227 $debug and warn "$filename is $width pix X $height pix.";
228 if ($width > 140 || $height > 200) { # MAX pixel dims are 140 X 200...
229 warn "$filename exceeds the maximum pixel dimensions of 140 X 200. Resizing...";
230 my $percent_reduce; # Percent we will reduce the image dimensions by...
232 $percent_reduce = sprintf("%.5f",(140/$width)); # If the width is oversize, scale based on width overage...
234 $percent_reduce = sprintf("%.5f",(200/$height)); # otherwise scale based on height overage.
236 my $width_reduce = sprintf("%.0f", ($width * $percent_reduce));
237 my $height_reduce = sprintf("%.0f", ($height * $percent_reduce));
238 warn "Reducing $filename by " . ($percent_reduce * 100) . "\% or to $width_reduce pix X $height_reduce pix";
239 $image->Resize(width=>$width_reduce, height=>$height_reduce);
240 my @img = $image->ImageToBlob();
242 warn "$filename is " . length($imgfile) . " bytes after resizing.";
243 undef $image; # This object can get big...
245 my @img = $image->ImageToBlob();
247 $debug and warn "$filename is " . length($imgfile) . " bytes.";
250 $debug and warn "Image is of mimetype $mimetype";
251 my $dberror = PutPatronImage($cardnumber,$mimetype, $imgfile) if $mimetype;
252 if ( !$dberror && $mimetype ) { # Errors from here on are fatal only to the import of a particular image, so don't bail, just note the error and keep going
254 push @{ $count{filenames} }, { source => $filename, cardnumber => $cardnumber };
255 } elsif ( $dberror ) {
256 warn "Database returned error: $dberror";
257 ($dberror =~ /patronimage_fk1/) ? $filerrors{'IMGEXISTS'} = 1 : $filerrors{'DBERR'} = 1;
258 push my @filerrors, \%filerrors;
259 push @{ $count{filenames} }, { filerrors => \@filerrors, source => $filename, cardnumber => $cardnumber };
260 $template->param( ERRORS => 1 );
261 } elsif ( !$mimetype ) {
262 warn "Unable to determine mime type of $filename. Please verify mimetype and add to \%mimemap if necessary.";
263 $filerrors{'MIMERR'} = 1;
264 push my @filerrors, \%filerrors;
265 push @{ $count{filenames} }, { filerrors => \@filerrors, source => $filename, cardnumber => $cardnumber };
266 $template->param( ERRORS => 1 );
269 warn "Opening $dir/$filename failed!";
270 $filerrors{'OPNERR'} = 1;
271 push my @filerrors, \%filerrors;
272 push @{ $count{filenames} }, { filerrors => \@filerrors, source => $filename, cardnumber => $cardnumber };
273 $template->param( ERRORS => 1 );
275 } else { # The need for this seems a bit unlikely, however, to maximize error trapping it is included
276 warn "Missing " . ($cardnumber ? "filename" : ($filename ? "cardnumber" : "cardnumber and filename"));
277 $filerrors{'CRDFIL'} = ($cardnumber ? "filename" : ($filename ? "cardnumber" : "cardnumber and filename"));
278 push my @filerrors, \%filerrors;
279 push @{ $count{filenames} }, { filerrors => \@filerrors, source => $filename, cardnumber => $cardnumber };
280 $template->param( ERRORS => 1 );
289 Original contributor(s) undocumented
291 Database storage, single patronimage upload option, and extensive error trapping contributed by Chris Nighswonger cnighswonger <at> foundations <dot> edu
292 Image scaling/resizing contributed by the same.