From 4b232a0c362b4f9ecffd1ef2358b32e13242544b Mon Sep 17 00:00:00 2001 From: Chris Nighswonger Date: Sat, 23 Feb 2008 06:28:04 -0500 Subject: [PATCH] Second installment on moving patronimages into the database Signed-off-by: Joshua Ferraro --- .../modules/help/members/picture-upload.tmpl | 6 + .../prog/en/modules/tools/picture-upload.tmpl | 62 +++++--- tools/picture-upload.pl | 144 ++++++++++-------- 3 files changed, 128 insertions(+), 84 deletions(-) create mode 100644 koha-tmpl/intranet-tmpl/prog/en/modules/help/members/picture-upload.tmpl diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/help/members/picture-upload.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/help/members/picture-upload.tmpl new file mode 100644 index 0000000000..05592ef671 --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/help/members/picture-upload.tmpl @@ -0,0 +1,6 @@ + + +

Patron Image Upload Screen

+

Select a file to upload to the server. Each .jpg file contained therein will be copied to the appropriate place on the server for patron pictures.

+

You can include multiple pictures in a .zip file.

+

There should be a DATALINK.TXT or IDLINK.TXT file for each group of pictures that has the cardnumber of the patron and the file containing that patrons picture. One patron per line seperated by either commas or tabs. Quotes around the fields are ignored.

diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/picture-upload.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/picture-upload.tmpl index 4e43f2d7db..ad259a5ba3 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/picture-upload.tmpl +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/picture-upload.tmpl @@ -15,7 +15,15 @@
+ +
+

Patron Images Uploaded With Some Errors

+
+ +

Patron Images Successfully Uploaded

+
+
  • Unpacking completed
  • directories scanned.
  • @@ -23,13 +31,20 @@
    -
  • image(s) moved from to :
  • -
+
  • image(s) moved into the database:
  • -
      -
    • To
    • -
    +
  • - Cardnumber: +
    WARNING: This image not imported because + + the database returned an error.
  • + the image format is unrecognized. + Koha was unable to open the image for reading. + of an unknown error. Please refer to the error log for more details. + + imported successfully. + + Upload More Images Return to Tools @@ -41,25 +56,32 @@

    Upload Patron Images

    -
      - -
    • The patronimages/ directory isn't present on your server, please ask your systems administrator to create it
    • - -
    • The upload file does not appear to be a zip file. The extention is not '.zip'.
    • +
    • The upload file does not appear to be a zip file. The extention is not '.zip'.
    • -
    • This script is not able to create/write to the necessary temporary directory.
    • - - -
    • This script is not able to write to the patron image storage directory.
    • +
    • This script is not able to create/write to the necessary temporary directory.
    • -
    • The upload file appears to be empty.
    • +
    • The upload file appears to be empty.
    • + + +
    • Cannot open to read.
      Please verify that it exists.
    • + + +
    • Cannot open to read.
      Please verify that it exists.
    • + + +
    • Unrecognized or missing field delimeter.
      Please verify that you are using either a single quote or a tab.
    • + + +
    • failed to unpack.
      Please verify the integrity of the zip file and retry.
    • + + +
    • An unknown error has occurred.
      Please review the error log for more details.
    • -
    @@ -75,14 +97,6 @@
    -
    -

    Notes:

    -
      -
    • Select a file to upload to the server. Each .jpg file contained therein will be copied to the appropriate place on the server for patron pictures.
    • -
    • You can include multiple pictures in a .zip file.
    • -
    • There should be a DATALINK.TXT or IDLINK.TXT file for each group of pictures that has the cardnumber of the patron and the file containing that patrons picture. One patron per line seperated by either commas or tabs. Quotes around the fields are ignored.
    • -
    -
    diff --git a/tools/picture-upload.pl b/tools/picture-upload.pl index 78ca35ff8f..b08b656988 100755 --- a/tools/picture-upload.pl +++ b/tools/picture-upload.pl @@ -7,15 +7,12 @@ use C4::Context; use C4::Auth; use C4::Output; use C4::Members; +use Data::Dumper; my $DEBUG = ($ENV{DEBUG}) ? 1 : 0; -#my $destdir = "/usr/local/koha/intranet/htdocs/intranet-tmpl/images/patronpictures"; -#my $uploadfile = shift @ARGV; my $input = new CGI; -my $destdir = C4::Context->config('intrahtdocs') . "/patronimages"; -warn "DEST : $destdir"; my ($template, $loggedinuser, $cookie) = get_template_and_user({template_name => "tools/picture-upload.tmpl", query => $input, @@ -25,13 +22,6 @@ my ($template, $loggedinuser, $cookie) debug => 0, }); -unless (-d $destdir) { - $errors{'NODIR'} = 1; - warn "patronimages directory not present"; -} -if ( %errors ) { - $template->param( ERRORS => [ \%errors ] ); -} my $uploadfilename = $input->param( 'uploadfile' ); my $uploadfile = $input->upload( 'uploadfile' ); my ( $total, $handled, @counts ); @@ -41,11 +31,10 @@ if ( $uploadfile ) { warn "dirname = $dirname" if $DEBUG; my ( $tfh, $tempfile ) = File::Temp::tempfile( SUFFIX => '.zip', UNLINK => 1 ); warn "tempfile = $tempfile" if $DEBUG; - my ( @directories, %errors ); + my ( @directories, $errors ); $errors{'NOTZIP'} = 1 unless ( $uploadfilename =~ /\.zip$/i ); $errors{'NOWRITETEMP'} = 1 unless ( -w $dirname ); - $errors{'NOWRITEDEST'} = 1 unless ( -w $destdir ); $errors{'EMPTYUPLOAD'} = 1 unless ( length( $uploadfile ) > 0 ); if ( %errors ) { @@ -53,34 +42,44 @@ if ( $uploadfile ) { } else { while ( <$uploadfile> ) { print $tfh $_; - } - - close $tfh; - - system("unzip $tempfile -d $dirname"); - - push @directories, "$dirname"; - foreach $recursive_dir ( @directories ) { - opendir $dir, $recursive_dir; - while ( my $entry = readdir $dir ) { - push @directories, "$recursive_dir/$entry" if ( -d "$recursive_dir/$entry" and $entry !~ /^\./ ); - warn "$recursive_dir/$entry" if $DEBUG; - } - closedir $dir; - } - - foreach my $dir ( @directories ) { - $handled += handle_dir( $dir ); - } - - $total = scalar @directories; - warn "Total files processed: $total" if $DEBUG; - $template->param( - TOTAL => $total, - HANDLED => $handled, - COUNTS => \@counts, - TCOUNTS => scalar(@counts), - ); + } + + close $tfh; + + unless (system("unzip $tempfile -d $dirname")) { + $errors{'UZIPFAIL'} = $uploadfilename; + $template->param( ERRORS => [ \%errors ] ); + output_html_with_http_headers $input, $cookie, $template->output; # This error is fatal to the import, so bail out here + exit; + } + push @directories, "$dirname"; + foreach $recursive_dir ( @directories ) { + opendir $dir, $recursive_dir; + while ( my $entry = readdir $dir ) { + push @directories, "$recursive_dir/$entry" if ( -d "$recursive_dir/$entry" and $entry !~ /^\./ ); + warn "$recursive_dir/$entry" if $DEBUG; + } + closedir $dir; + } + my $results; + foreach my $dir ( @directories ) { + $results = handle_dir( $dir ); + $handled++ if $results == 1; + } + + if ( %$results || %errors ) { + $template->param( ERRORS => [ \%$results ] ); + } else { + $total = scalar @directories; + warn "Total files processed: $total" if $DEBUG; + warn "Errors in \$errors." if $errors; + $template->param( + TOTAL => $total, + HANDLED => $handled, + COUNTS => \@counts, + TCOUNTS => scalar(@counts), + ); + } } } @@ -105,43 +104,68 @@ sub handle_dir { $file = "$dir/$filename" if ($filename =~ m/datalink\.txt/i || $filename =~ m/idlink\.txt/i); } unless (open (FILE, $file)) { - warn "Opening $dir/$file failed!" if $DEBUG; - return 0; - }; + warn "Opening $dir/$file failed!"; + $errors{'OPNLINK'} = $file; + return $errors; # This error is fatal to the import of this directory contents, so bail and return the error to the caller + }; while (my $line = ) { warn "Reading contents of $file" if $DEBUG; chomp $line; warn "Examining line: $line" if $DEBUG; my ( $filename, $cardnumber ); - my $delim = ($line =~ /\t/) ? "\t" : ","; + my $delim = ($line =~ /\t/) ? "\t" : ($line =~ /,/) ? "," : ""; warn "Delimeter is \'$delim\'" if $DEBUG; + unless ( $delim eq "," || $delim eq "\t" ) { + warn "Unrecognized or missing field delimeter. Please verify that you are using either a ',' or a 'tab'"; + $errors{'DELERR'} = 1; # This error is fatal to the import of this directory contents, so bail and return the error to the caller + return $errors; + } ($cardnumber, $filename) = split $delim, $line; $cardnumber =~ s/[\"\r\n]//g; # remove offensive characters $filename =~ s/[\"\r\n\s]//g; warn "Cardnumber: $cardnumber Filename: $filename" if $DEBUG; if ($cardnumber && $filename) { + my %filerrors; warn "Source: $dir/$filename" if $DEBUG; - open (IMG, "$dir/$filename") or warn "Could not open $dir/$filename"; - #binmode (IMG); # Not sure if we need this or not -fbcit - my $imgfile; - while () { - $imgfile .= $_; + if (open (IMG, "$dir/$filename")) { + #binmode (IMG); # Not sure if we need this or not -fbcit + my $imgfile; + while () { + $imgfile .= $_; + } + my $mimetype = $mimemap->{lc ($1)} if $filename =~ m/\.([^.]+)$/i; + warn "$filename is mimetype \"$mimetype\"" if $DEBUG; + my $dberror = PutPatronImage($cardnumber,$mimetype, $imgfile) if $mimetype; + close (IMG); + 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 + $count{count}++; + push @{ $count{filenames} }, { source => $filename, cardnumber => $cardnumber }; + } elsif ( $dberror ) { + warn "Database returned error. We're not logging it because it most likely contains binary data which does unpleasent things to terminal windows and logs."; + $filerrors{'DBERR'} = 1; + push my @filerrors, \%filerrors; + push @{ $count{filenames} }, { filerrors => \@filerrors, source => $filename, cardnumber => $cardnumber }; + $template->param( ERRORS => 1 ); + } elsif ( !$mimetype ) { + warn "Unable to determine mime type of $filename. Please verify mimetype and add to \%mimemap if necessary."; + $filerrors{'MIMERR'} = 1; + push my @filerrors, \%filerrors; + push @{ $count{filenames} }, { filerrors => \@filerrors, source => $filename, cardnumber => $cardnumber }; + $template->param( ERRORS => 1 ); + } + } else { + warn "Opening $dir/$filename failed!"; + $filerrors{'OPNERR'} = 1; + push my @filerrors, \%filerrors; + push @{ $count{filenames} }, { filerrors => \@filerrors, source => $filename, cardnumber => $cardnumber }; + $template->param( ERRORS => 1 ); } - my $mimetype = $mimemap->{lc ($1)} if $filename =~ m/\.([^.]+)$/i; - warn "$filename is mimetype \"$mimetype\"" if $DEBUG; - my $dberror = PutPatronImage($cardnumber,$mimetype, $imgfile) if $mimetype; -# warn "Database says: $dberror" if $dberror; - close (IMG); - unless ( $dberror || !$mimetype ) { - $count{count}++; - push @{ $count{filenames} }, { source => $filename, dest => $cardnumber }; - } } } $count{source} = $dir; - $count{dest} = $destdir; push @counts, \%count; close FILE; + closedir ( $dirhandle ); return 1; } -- 2.39.5