2 # UCW Gallery: Scan images and generate image list
3 # (c) 2004--2012 Martin Mares <mj@ucw.cz>
14 if (@ARGV && $ARGV[0] eq '--help') {
16 Usage: cat list | gal scan
17 or: gal scan <files and directories>
24 'update!' => \$update,
25 ) or die "Try gal scan --help\n";
28 my $gal = UCW::Gallery->load_config;
29 my $orig_prefix = $gal->get('OrigDir');
30 $orig_prefix =~ m{/$} or $orig_prefix .= '/';
34 print "Loading previous gallery.list\n";
35 my $pg = $gal->read_list('gallery.list') or die "Unable to load gallery.list\n";
40 push @source, { file => $in };
42 opendir D, $in or die "Cannot scan directory $in: $!\n";
44 while (my $e = readdir D) {
45 my $f = File::Spec->canonpath(File::Spec->catfile($in, $e));
46 if ($f =~ m{\.(jpe?g|png)$}i) {
51 push @source, map { { file => $_ } } sort @p;
53 die "$in is neither file nor directory\n";
59 push @source, { file => $_ };
63 print "Scanning photos\n";
65 foreach my $src (@source) {
66 my $name = $src->{file};
68 # Try to relativize to OrigDir
69 if (substr($name, 0, length $orig_prefix) eq $orig_prefix) {
70 $src->{file} = $name = substr($name, length $orig_prefix);
75 my $path = File::Spec->rel2abs($name, $gal->get('OrigDir'));
76 -f $path or die "Cannot find $path\n";
78 if (!defined $src->{id}) {
79 my $sha = Digest::SHA->new(1);
80 $sha->addfile($path) or die "Cannot hash $path\n";
81 $src->{id} = substr($sha->hexdigest, 0, 16);
83 print " id=", $src->{id};
85 if (!defined $src->{orientation} || $src->{orientation} eq '-') {
86 my $e = new Image::EXIF($path);
87 my $i = $e->get_all_info();
89 print "EXIF error: ", $e->error, "\n";
90 $src->{orientation} = '.';
92 # print STDERR Dumper($i), "\n";
93 my $o = $i->{'image'}->{'Image Orientation'} || "Top, Left-Hand";
94 if ($o eq "Top, Left-Hand") { $o = "."; }
95 elsif ($o eq "Right-Hand, Top") { $o = "r"; }
96 elsif ($o eq "Left-Hand, Bottom") { $o = "l"; }
97 elsif ($o eq "Bottom, Right-Hand") { $o = "d"; }
99 print "Unrecognized orientation: $o\n";
102 $src->{orientation} = $o;
105 print " ori=", $src->{orientation};
107 defined $src->{xfrm} or $src->{xfrm} = $gal->get('ScanDefaultTransform');
108 print " xfrm=", $src->{xfrm};
110 $src->{title} //= '';
116 my $old = $gal->read_list('gallery.list');
118 print "Updating gallery.list\n";
119 my %old_by_id = map { $_->{id} => $_ } @$old;
120 for my $i (@images) {
122 my $o = $old_by_id{$id};
124 print "\t$id: updated\n";
125 $i->{orientation} = $o->{orientation};
126 $i->{xfrm} = $o->{xfrm};
127 $i->{title} = $o->{title};
129 print "\t$id: new\n";
131 delete $old_by_id{$id};
133 for my $id (keys %old_by_id) {
134 print "\t$id: removed\n";
139 $gal->write_list('gallery.list', \@images);
140 print "Written gallery.list (", (scalar @images), " items)\n";