]> mj.ucw.cz Git - gallery.git/blobdiff - gal/bin/gal-scan
gal-mj-digikam imports GPS coordinates
[gallery.git] / gal / bin / gal-scan
index 41e2d3a607bd5183ae1f032230544a786b1c1ab5..a976d5d9ce4e33b480359d968ab665bcd2c76627 100755 (executable)
@@ -1,26 +1,31 @@
 #!/usr/bin/perl
 # UCW Gallery: Scan images and generate image list
-# (c) 2004--2012 Martin Mares <mj@ucw.cz>
+# (c) 2004--2015 Martin Mares <mj@ucw.cz>
 
 use strict;
 use warnings;
 
 use UCW::Gallery;
+use UCW::Gallery::Hashes;
 use File::Spec;
 use Image::EXIF;
-use Digest::SHA;
 use Getopt::Long;
 
 if (@ARGV && $ARGV[0] eq '--help') {
        die <<AMEN ;
-Usage: cat list | gal scan
-   or: gal scan <files and directories>
-   or:  gal scan --update
+Usage: cat list | gal scan <options>
+   or: gal scan <options> <files and directories>
+   or: gal scan <options> --update
+
+Options:
+--add          Keep existing images and add new ones
 AMEN
 }
 
+my $add;
 my $update;
 GetOptions(
+       'add!' => \$add,
        'update!' => \$update,
 ) or die "Try gal scan --help\n";
 
@@ -54,12 +59,12 @@ if ($update) {
                }
        }
 } else {
-       while (<STDIN>) {
-               chomp;
-               push @source, { file => $_ };
-       }
+       binmode STDIN, ':utf8';
+       @source = @{$gal->read_list_fh(\*STDIN)};
 }
 
+my $hashes = UCW::Gallery::Hashes->new($gal);
+
 print "Scanning photos\n";
 my @images = ();
 foreach my $src (@source) {
@@ -75,11 +80,7 @@ foreach my $src (@source) {
        my $path = File::Spec->rel2abs($name, $gal->get('OrigDir'));
        -f $path or die "Cannot find $path\n";
 
-       if (!defined $src->{id}) {
-               my $sha = Digest::SHA->new(1);
-               $sha->addfile($path) or die "Cannot hash $path\n";
-               $src->{id} = substr($sha->hexdigest, 0, 16);
-       }
+       $src->{id} = $hashes->hash_image($path);
        print " id=", $src->{id};
 
        if (!defined $src->{orientation} || $src->{orientation} eq '-') {
@@ -94,7 +95,7 @@ foreach my $src (@source) {
                        if ($o eq "Top, Left-Hand") { $o = "."; }
                        elsif ($o eq "Right-Hand, Top") { $o = "r"; }
                        elsif ($o eq "Left-Hand, Bottom") { $o = "l"; }
-                       elsif ($o eq "Bottom, Right-Hand") { $o = "d"; }
+                       elsif ($o eq "Bottom, Right-Hand") { $o = "u"; }
                        else {
                                print "Unrecognized orientation: $o\n";
                                $o = ".";
@@ -104,8 +105,8 @@ foreach my $src (@source) {
        }
        print " ori=", $src->{orientation};
 
-       defined $src->{xfrm} or $src->{xfrm} = $gal->get('ScanDefaultTransform');
-       print " xfrm=", $src->{xfrm};
+       defined $src->{xf} or $src->{xf} = $gal->get('ScanDefaultTransform');
+       print " xfrm=", $src->{xf};
 
        $src->{title} //= '';
        push @images, $src;
@@ -122,12 +123,17 @@ if (!$update) {
                        my $id = $o->{id};
                        my $i = $new_by_id{$id};
                        if (!$i) {
-                               print "\t$id: removed\n";
-                               next;
+                               if ($add) {
+                                       print "\t$id: kept\n";
+                                       push @result, $o;
+                               } else {
+                                       print "\t$id: removed\n";
+                               }
+                       } else {
+                               print "\t$id: kept\n";
+                               push @result, $o;
+                               delete $new_by_id{$id};
                        }
-                       print "\t$id: updated\n";
-                       push @result, $o;
-                       delete $new_by_id{$id};
                }
                for my $i (@images) {
                        my $id = $i->{id};
@@ -141,3 +147,4 @@ if (!$update) {
 
 $gal->write_list('gallery.list', \@images);
 print "Written gallery.list (", (scalar @images), " items)\n";
+$hashes->write;