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
AMEN
}
-my @files = ();
-if (@ARGV) {
+my $update;
+GetOptions(
+ 'update!' => \$update,
+) or die "Try gal scan --help\n";
+
+STDOUT->autoflush(1);
+my $gal = UCW::Gallery->load_config;
+my $orig_prefix = $gal->get('OrigDir');
+$orig_prefix =~ m{/$} or $orig_prefix .= '/';
+
+my @source = ();
+if ($update) {
+ print "Loading previous gallery.list\n";
+ my $pg = $gal->read_list('gallery.list') or die "Unable to load gallery.list\n";
+ @source = @{$pg};
+} elsif (@ARGV) {
for my $in (@ARGV) {
if (-f $in) {
- push @files, $in;
+ push @source, { file => $in };
} elsif (-d $in) {
opendir D, $in or die "Cannot scan directory $in: $!\n";
my @p = ();
}
}
closedir D;
- push @files, sort @p;
+ push @source, map { { file => $_ } } sort @p;
} else {
die "$in is neither file nor directory\n";
}
} else {
while (<STDIN>) {
chomp;
- push @files, $_;
+ push @source, { file => $_ };
}
}
-STDOUT->autoflush(1);
-
-my $gal = UCW::Gallery->load_config;
-my $orig_prefix = $gal->get('OrigDir');
-$orig_prefix =~ m{/$} or $orig_prefix .= '/';
-
print "Scanning photos\n";
my @images = ();
-foreach my $f (@files) {
- my $rel_name = $f;
- if (substr($rel_name, 0, length $orig_prefix) eq $orig_prefix) {
- $rel_name = substr($rel_name, length $orig_prefix);
+foreach my $src (@source) {
+ my $name = $src->{file};
+ if ($name =~ m{^/}) {
+ # Try to relativize to OrigDir
+ if (substr($name, 0, length $orig_prefix) eq $orig_prefix) {
+ $src->{file} = $name = substr($name, length $orig_prefix);
+ }
}
- print "\t$rel_name: ";
+ print "\t$name:";
- my $sha = Digest::SHA->new(1);
- $sha->addfile($f) or die "Cannot hash $f\n";
- my $id = substr($sha->hexdigest, 0, 16);
- print "id=$id ";
+ my $path = File::Spec->rel2abs($name, $gal->get('OrigDir'));
+ -f $path or die "Cannot find $path\n";
- my $e = new Image::EXIF($f);
- my $i = $e->get_all_info();
- if ($e->error) { print "EXIF error: ", $e->error, "\n"; }
- else {
- # print STDERR Dumper($i), "\n";
- my $o = $i->{'image'}->{'Image Orientation'} || "Top, Left-Hand";
- 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"; }
- else {
- print "Unrecognized orientation: $o\n";
- $o = ".";
- }
- push @images, {
- file => $rel_name,
- id => $id,
- orientation => $o,
- xfrm => $gal->get('ScanDefaultTransform'),
- title => '',
- };
- print "ori=$o\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);
}
-}
+ print " id=", $src->{id};
-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};
+ if (!defined $src->{orientation} || $src->{orientation} eq '-') {
+ my $e = new Image::EXIF($path);
+ my $i = $e->get_all_info();
+ if ($e->error) {
+ print "EXIF error: ", $e->error, "\n";
+ $src->{orientation} = '.';
} else {
- print "\t$id: new\n";
+ # print STDERR Dumper($i), "\n";
+ my $o = $i->{'image'}->{'Image Orientation'} || "Top, Left-Hand";
+ 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"; }
+ else {
+ print "Unrecognized orientation: $o\n";
+ $o = ".";
+ }
+ $src->{orientation} = $o;
}
- delete $old_by_id{$id};
}
- for my $id (keys %old_by_id) {
- print "\t$id: removed\n";
+ print " ori=", $src->{orientation};
+
+ defined $src->{xfrm} or $src->{xfrm} = $gal->get('ScanDefaultTransform');
+ print " xfrm=", $src->{xfrm};
+
+ push @images, $src;
+ print "\n";
+}
+
+if (!$update) {
+ 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};
+ } else {
+ print "\t$id: new\n";
+ }
+ delete $old_by_id{$id};
+ }
+ for my $id (keys %old_by_id) {
+ print "\t$id: removed\n";
+ }
}
}