12 my $DEBUG = ($ENV{DEBUG}) ? 1 : 0;
16 my ($template, $loggedinuser, $cookie)
17 = get_template_and_user({template_name => "tools/picture-upload.tmpl",
21 flagsrequired => {management => 1, tools => 1},
25 my $uploadfilename = $input->param( 'uploadfile' );
26 my $uploadfile = $input->upload( 'uploadfile' );
27 my ( $total, $handled, @counts );
30 my $dirname = File::Temp::tempdir( CLEANUP => 1);
31 warn "dirname = $dirname" if $DEBUG;
32 my ( $tfh, $tempfile ) = File::Temp::tempfile( SUFFIX => '.zip', UNLINK => 1 );
33 warn "tempfile = $tempfile" if $DEBUG;
34 my ( @directories, $errors );
36 $errors{'NOTZIP'} = 1 unless ( $uploadfilename =~ /\.zip$/i );
37 $errors{'NOWRITETEMP'} = 1 unless ( -w $dirname );
38 $errors{'EMPTYUPLOAD'} = 1 unless ( length( $uploadfile ) > 0 );
41 $template->param( ERRORS => [ \%errors ] );
43 while ( <$uploadfile> ) {
49 unless (system("unzip $tempfile -d $dirname") == 0) {
50 $errors{'UZIPFAIL'} = $uploadfilename;
51 $template->param( ERRORS => [ \%errors ] );
52 output_html_with_http_headers $input, $cookie, $template->output; # This error is fatal to the import, so bail out here
55 push @directories, "$dirname";
56 foreach $recursive_dir ( @directories ) {
57 opendir $dir, $recursive_dir;
58 while ( my $entry = readdir $dir ) {
59 push @directories, "$recursive_dir/$entry" if ( -d "$recursive_dir/$entry" and $entry !~ /^\./ );
60 warn "$recursive_dir/$entry" if $DEBUG;
65 foreach my $dir ( @directories ) {
66 $results = handle_dir( $dir );
67 $handled++ if $results == 1;
70 if ( %$results || %errors ) {
71 $template->param( ERRORS => [ \%$results ] );
73 $total = scalar @directories;
74 warn "Total files processed: $total" if $DEBUG;
75 warn "Errors in \$errors." if $errors;
80 TCOUNTS => scalar(@counts),
86 output_html_with_http_headers $input, $cookie, $template->output;
89 warn "Entering sub handle_dir" if $DEBUG;
93 $count{filenames} = ();
97 "jpg" => "image/jpeg",
98 "jpeg" => "image/jpeg",
102 opendir my $dirhandle, $dir;
103 while ( my $filename = readdir $dirhandle ) {
104 $file = "$dir/$filename" if ($filename =~ m/datalink\.txt/i || $filename =~ m/idlink\.txt/i);
106 unless (open (FILE, $file)) {
107 warn "Opening $dir/$file failed!";
108 $errors{'OPNLINK'} = $file;
109 return $errors; # This error is fatal to the import of this directory contents, so bail and return the error to the caller
112 while (my $line = <FILE>) {
113 warn "Reading contents of $file" if $DEBUG;
115 warn "Examining line: $line" if $DEBUG;
116 my ( $filename, $cardnumber );
117 my $delim = ($line =~ /\t/) ? "\t" : ($line =~ /,/) ? "," : "";
118 warn "Delimeter is \'$delim\'" if $DEBUG;
119 unless ( $delim eq "," || $delim eq "\t" ) {
120 warn "Unrecognized or missing field delimeter. Please verify that you are using either a ',' or a 'tab'";
121 $errors{'DELERR'} = 1; # This error is fatal to the import of this directory contents, so bail and return the error to the caller
124 ($cardnumber, $filename) = split $delim, $line;
125 $cardnumber =~ s/[\"\r\n]//g; # remove offensive characters
126 $filename =~ s/[\"\r\n\s]//g;
127 warn "Cardnumber: $cardnumber Filename: $filename" if $DEBUG;
128 if ($cardnumber && $filename) {
130 warn "Source: $dir/$filename" if $DEBUG;
131 if (open (IMG, "$dir/$filename")) {
132 #binmode (IMG); # Not sure if we need this or not -fbcit
137 my $mimetype = $mimemap->{lc ($1)} if $filename =~ m/\.([^.]+)$/i;
138 warn "$filename is mimetype \"$mimetype\"" if $DEBUG;
139 my $dberror = PutPatronImage($cardnumber,$mimetype, $imgfile) if $mimetype;
141 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
143 push @{ $count{filenames} }, { source => $filename, cardnumber => $cardnumber };
144 } elsif ( $dberror ) {
145 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.";
146 $filerrors{'DBERR'} = 1;
147 push my @filerrors, \%filerrors;
148 push @{ $count{filenames} }, { filerrors => \@filerrors, source => $filename, cardnumber => $cardnumber };
149 $template->param( ERRORS => 1 );
150 } elsif ( !$mimetype ) {
151 warn "Unable to determine mime type of $filename. Please verify mimetype and add to \%mimemap if necessary.";
152 $filerrors{'MIMERR'} = 1;
153 push my @filerrors, \%filerrors;
154 push @{ $count{filenames} }, { filerrors => \@filerrors, source => $filename, cardnumber => $cardnumber };
155 $template->param( ERRORS => 1 );
158 warn "Opening $dir/$filename failed!";
159 $filerrors{'OPNERR'} = 1;
160 push my @filerrors, \%filerrors;
161 push @{ $count{filenames} }, { filerrors => \@filerrors, source => $filename, cardnumber => $cardnumber };
162 $template->param( ERRORS => 1 );
166 $count{source} = $dir;
167 push @counts, \%count;
169 closedir ( $dirhandle );