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