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