#!/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";
}
}
} 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) {
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 '-') {
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 = ".";
}
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;
my $old = $gal->read_list('gallery.list');
if ($old) {
print "Updating gallery.list\n";
- my %old_by_id = map { $_->{id} => $_ } @$old;
- for my $i (@images) {
- my $id = $i->{id};
- my $o = $old_by_id{$id};
- if ($o) {
- print "\t$id: updated\n";
- $i->{orientation} = $o->{orientation};
- $i->{xfrm} = $o->{xfrm};
- $i->{title} = $o->{title};
+ my %new_by_id = map { $_->{id} => $_ } @images;
+ my @result = ();
+ for my $o (@$old) {
+ my $id = $o->{id};
+ my $i = $new_by_id{$id};
+ if (!$i) {
+ if ($add) {
+ print "\t$id: kept\n";
+ push @result, $o;
+ } else {
+ print "\t$id: removed\n";
+ }
} else {
- print "\t$id: new\n";
+ print "\t$id: kept\n";
+ push @result, $o;
+ delete $new_by_id{$id};
}
- delete $old_by_id{$id};
}
- for my $id (keys %old_by_id) {
- print "\t$id: removed\n";
+ for my $i (@images) {
+ my $id = $i->{id};
+ $new_by_id{$id} or next;
+ print "\t$id: new\n";
+ push @result, $i;
}
+ @images = @result;
}
}
$gal->write_list('gallery.list', \@images);
print "Written gallery.list (", (scalar @images), " items)\n";
+$hashes->write;