Advertisement
mikelieman

bin/image-db-build.pl

Dec 4th, 2014
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.39 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. # create a list of image files, mtime, and sha256sum for later analysis
  4.  
  5. use v5.12;    # 5.12+ gives strict and say...
  6. use warnings;
  7. use File::Find;
  8. use Digest::SHA;
  9. use Image::Info qw(image_type);
  10. use Term::ProgressBar::Simple;
  11. use Text::CSV;
  12.  
  13. my @search_dirs = @ARGV or die "Need at least one directory name:$!";
  14.  
  15. my $csv = Text::CSV->new( { binary => 1, eol => qq{\r\n} } )
  16.   or die "Cannot use CSV: " . Text::CSV->error_diag();
  17.  
  18. my $dbname = q{fileinfo.db};
  19. open my $FH, q{>:encoding(utf8)}, $dbname or die "$dbname: $!";
  20.  
  21. my $tot_files = 0;
  22. find( sub { $tot_files++ if -f $File::Find::name }, @search_dirs );
  23.  
  24. say "Scanning $tot_files files in "
  25.   . join( q{ }, @search_dirs )
  26.   . ".  Saving data to $dbname.";
  27.  
  28. my $progress = Term::ProgressBar::Simple->new($tot_files);
  29. find( \&process_file, @search_dirs );
  30.  
  31. sub process_file {
  32.  
  33.     return if not -f $File::Find::name;
  34.  
  35.     $progress++;
  36.  
  37.     if ( not image_type($File::Find::name)->{error} ) {
  38.  
  39.         my $sha = Digest::SHA->new(q{sha256});
  40.         $sha->addfile( $File::Find::name, q{b} );
  41.  
  42.         my (
  43.             $dev, $ino,  $mode, $nlink, $uid,
  44.             $gid, $rdev, $size, $atime, $mtime
  45.         ) = stat($File::Find::name);
  46.  
  47.         $csv->print( $FH,
  48.             [ $File::Find::name, $_, $mtime, $size, $sha->hexdigest ] );
  49.     }    # if it's an image
  50. }    # sub process_file
  51.  
  52. __END__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement