]> mj.ucw.cz Git - gallery.git/blob - gal2/gal-scan
Gallery2: Delete old theme "nrt"
[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;
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 STDOUT->autoflush(1);
51
52 my $gal = UCW::Gallery->load_config;
53 my $orig_prefix = $gal->get('OrigDir');
54 $orig_prefix =~ m{/$} or $orig_prefix .= '/';
55
56 print "Scanning photos\n";
57 my @images = ();
58 foreach my $f (@files) {
59         my $rel_name = $f;
60         if (substr($rel_name, 0, length $orig_prefix) eq $orig_prefix) {
61                 $rel_name = substr($rel_name, length $orig_prefix);
62         }
63         print "\t$rel_name: ";
64
65         my $sha = Digest::SHA->new(1);
66         $sha->addfile($f) or die "Cannot hash $f\n";
67         my $id = substr($sha->hexdigest, 0, 16);
68         print "id=$id ";
69
70         my $e = new Image::EXIF($f);
71         my $i = $e->get_all_info();
72         if ($e->error) { print "EXIF error: ", $e->error, "\n"; }
73         else {
74                 # print STDERR Dumper($i), "\n";
75                 my $o = $i->{'image'}->{'Image Orientation'} || "Top, Left-Hand";
76                 if ($o eq "Top, Left-Hand") { $o = "."; }
77                 elsif ($o eq "Right-Hand, Top") { $o = "r"; }
78                 elsif ($o eq "Left-Hand, Bottom") { $o = "l"; }
79                 elsif ($o eq "Bottom, Right-Hand") { $o = "d"; }
80                 else {
81                         print "Unrecognized orientation: $o\n";
82                         $o = ".";
83                 }
84                 push @images, {
85                         file => $rel_name,
86                         id => $id,
87                         orientation => $o,
88                         xfrm => $gal->get('ScanDefaultTransform'),
89                         title => '',
90                 };
91                 print "ori=$o\n";
92         }
93 }
94
95 my $old = $gal->read_list('gallery.list');
96 if ($old) {
97         print "Updating gallery.list\n";
98         my %old_by_id = map { $_->{id} => $_ } @$old;
99         for my $i (@images) {
100                 my $id = $i->{id};
101                 my $o = $old_by_id{$id};
102                 if ($o) {
103                         print "\t$id: updated\n";
104                         $i->{orientation} = $o->{orientation};
105                         $i->{xfrm} = $o->{xfrm};
106                         $i->{title} = $o->{title};
107                 } else {
108                         print "\t$id: new\n";
109                 }
110                 delete $old_by_id{$id};
111         }
112         for my $id (keys %old_by_id) {
113                 print "\t$id: removed\n";
114         }
115 }
116
117 $gal->write_list('gallery.list', \@images);
118 print "Written gallery.list (", (scalar @images), " items)\n";