]> mj.ucw.cz Git - gallery.git/blob - gal2/bin/gal-scan
Gallery2: gal-scan --update supported
[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 use Getopt::Long;
13
14 if (@ARGV && $ARGV[0] eq '--help') {
15         die <<AMEN ;
16 Usage:  cat list | gal scan
17    or:  gal scan <files and directories>
18    or:  gal scan --update
19 AMEN
20 }
21
22 my $update;
23 GetOptions(
24         'update!' => \$update,
25 ) or die "Try gal scan --help\n";
26
27 STDOUT->autoflush(1);
28 my $gal = UCW::Gallery->load_config;
29 my $orig_prefix = $gal->get('OrigDir');
30 $orig_prefix =~ m{/$} or $orig_prefix .= '/';
31
32 my @source = ();
33 if ($update) {
34         print "Loading previous gallery.list\n";
35         my $pg = $gal->read_list('gallery.list') or die "Unable to load gallery.list\n";
36         @source = @{$pg};
37 } elsif (@ARGV) {
38         for my $in (@ARGV) {
39                 if (-f $in) {
40                         push @source, { file => $in };
41                 } elsif (-d $in) {
42                         opendir D, $in or die "Cannot scan directory $in: $!\n";
43                         my @p = ();
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) {
47                                         push @p, $f;
48                                 }
49                         }
50                         closedir D;
51                         push @source, map { { file => $_ } } sort @p;
52                 } else {
53                         die "$in is neither file nor directory\n";
54                 }
55         }
56 } else {
57         while (<STDIN>) {
58                 chomp;
59                 push @source, { file => $_ };
60         }
61 }
62
63 print "Scanning photos\n";
64 my @images = ();
65 foreach my $src (@source) {
66         my $name = $src->{file};
67         if ($name =~ m{^/}) {
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);
71                 }
72         }
73         print "\t$name:";
74
75         my $path = File::Spec->rel2abs($name, $gal->get('OrigDir'));
76         -f $path or die "Cannot find $path\n";
77
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);
82         }
83         print " id=", $src->{id};
84
85         if (!defined $src->{orientation} || $src->{orientation} eq '-') {
86                 my $e = new Image::EXIF($path);
87                 my $i = $e->get_all_info();
88                 if ($e->error) {
89                         print "EXIF error: ", $e->error, "\n";
90                         $src->{orientation} = '.';
91                 } else {
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"; }
98                         else {
99                                 print "Unrecognized orientation: $o\n";
100                                 $o = ".";
101                         }
102                         $src->{orientation} = $o;
103                 }
104         }
105         print " ori=", $src->{orientation};
106
107         defined $src->{xfrm} or $src->{xfrm} = $gal->get('ScanDefaultTransform');
108         print " xfrm=", $src->{xfrm};
109
110         push @images, $src;
111         print "\n";
112 }
113
114 if (!$update) {
115         my $old = $gal->read_list('gallery.list');
116         if ($old) {
117                 print "Updating gallery.list\n";
118                 my %old_by_id = map { $_->{id} => $_ } @$old;
119                 for my $i (@images) {
120                         my $id = $i->{id};
121                         my $o = $old_by_id{$id};
122                         if ($o) {
123                                 print "\t$id: updated\n";
124                                 $i->{orientation} = $o->{orientation};
125                                 $i->{xfrm} = $o->{xfrm};
126                                 $i->{title} = $o->{title};
127                         } else {
128                                 print "\t$id: new\n";
129                         }
130                         delete $old_by_id{$id};
131                 }
132                 for my $id (keys %old_by_id) {
133                         print "\t$id: removed\n";
134                 }
135         }
136 }
137
138 $gal->write_list('gallery.list', \@images);
139 print "Written gallery.list (", (scalar @images), " items)\n";