]> mj.ucw.cz Git - gallery.git/blob - gal2/gal-scan
Gallery2: Use FindBin to find libraries
[gallery.git] / gal2 / gal-scan
1 #!/usr/bin/perl
2 # UCW Gallery: Scan images and generate image list
3 # (c) 2004--2012 Martin Mares <mj@ucw.cz>
4
5 use strict;
6 use warnings;
7
8 use FindBin;
9 use lib $FindBin::Bin;
10 use UCW::Gallery qw(%CF);
11
12 use File::Spec;
13 use Image::EXIF;
14 use Digest::SHA;
15
16 if (@ARGV && $ARGV[0] eq '--help') {
17         die <<AMEN ;
18 Usage:  cat list | $0
19    or:  $0 <files and directories>
20 AMEN
21 }
22
23 my @files = ();
24 if (@ARGV) {
25         for my $in (@ARGV) {
26                 if (-f $in) {
27                         push @files, $in;
28                 } elsif (-d $in) {
29                         opendir D, $in or die "Cannot scan directory $in: $!\n";
30                         my @p = ();
31                         while (my $e = readdir D) {
32                                 my $f = File::Spec->canonpath(File::Spec->catfile($in, $e));
33                                 if ($f =~ m{\.(jpe?g|png)$}i) {
34                                         push @p, $f;
35                                 }
36                         }
37                         closedir D;
38                         push @files, sort @p;
39                 } else {
40                         die "$in is neither file nor directory\n";
41                 }
42         }
43 } else {
44         while (<STDIN>) {
45                 chomp;
46                 push @files, $_;
47         }
48 }
49
50 UCW::Gallery::LoadConfig;
51 my $orig_prefix = $CF{'OrigDir'};
52 $orig_prefix =~ m{/$} or $orig_prefix .= '/';
53
54 print "Scanning photos\n";
55 my @images = ();
56 foreach my $f (@files) {
57         my $rel_name = $f;
58         if (substr($rel_name, 0, length $orig_prefix) eq $orig_prefix) {
59                 $rel_name = substr($rel_name, length $orig_prefix);
60         }
61         print "\t$rel_name: ";
62
63         my $sha = Digest::SHA->new(1);
64         $sha->addfile($f) or die "Cannot hash $f\n";
65         my $id = substr($sha->hexdigest, 0, 16);
66         print "id=$id ";
67
68         my $e = new Image::EXIF($f);
69         my $i = $e->get_all_info();
70         if ($e->error) { print "EXIF error: ", $e->error, "\n"; }
71         else {
72                 # print STDERR Dumper($i), "\n";
73                 my $o = $i->{'image'}->{'Image Orientation'} || "Top, Left-Hand";
74                 if ($o eq "Top, Left-Hand") { $o = "."; }
75                 elsif ($o eq "Right-Hand, Top") { $o = "r"; }
76                 elsif ($o eq "Left-Hand, Bottom") { $o = "l"; }
77                 elsif ($o eq "Bottom, Right-Hand") { $o = "d"; }
78                 else {
79                         print "Unrecognized orientation: $o\n";
80                         $o = ".";
81                 }
82                 push @images, {
83                         file => $rel_name,
84                         id => $id,
85                         orientation => $o,
86                         xfrm => $CF{'ScanDefaultTransform'},
87                         title => '',
88                 };
89                 print "ori=$o\n";
90         }
91 }
92
93 my $old = UCW::Gallery::ReadList('gallery.list');
94 if ($old) {
95         print "Updating gallery.list\n";
96         my %old_by_id = map { $_->{id} => $_ } @$old;
97         for my $i (@images) {
98                 my $id = $i->{id};
99                 my $o = $old_by_id{$id};
100                 if ($o) {
101                         print "\t$id: updated\n";
102                         $i->{orientation} = $o->{orientation};
103                         $i->{xfrm} = $o->{xfrm};
104                         $i->{title} = $o->{title};
105                 } else {
106                         print "\t$id: new\n";
107                 }
108                 delete $old_by_id{$id};
109         }
110         for my $id (keys %old_by_id) {
111                 print "\t$id: removed\n";
112         }
113 }
114
115 UCW::Gallery::WriteList('gallery.list', \@images);
116 print "Written gallery.list (", (scalar @images), " items)\n";