From: Martin Mares Date: Sun, 23 Dec 2012 14:26:15 +0000 (+0100) Subject: Gallery2: First bits of scanning and updating X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=929e3ca5d5af812fe382abe5bd88ab306a3b496a;p=gallery.git Gallery2: First bits of scanning and updating GQview collections handled by a separate script. --- diff --git a/gal2/UCW/Gallery.pm b/gal2/UCW/Gallery.pm new file mode 100644 index 0000000..ad0f35d --- /dev/null +++ b/gal2/UCW/Gallery.pm @@ -0,0 +1,102 @@ +# Simple Photo Gallery +# (c) 2003--2012 Martin Mares + +package UCW::Gallery; + +use strict; +use warnings; + +BEGIN { + # Standard Perl module stuff + use Exporter(); + our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS); + $VERSION = 1.00; + @ISA = qw(Exporter); + @EXPORT = qw(&SetOptions); + %EXPORT_TAGS = (); + @EXPORT_OK = qw(%CF); +} + +our %CF; +our $th; + +BEGIN { + $CF{"Title"} = "An Unnamed Gallery", + $CF{"HeadExtras"} = "", + $CF{"TopExtras"} = "", + $CF{"BotExtras"} = "", + $CF{"ParentURL"} = "../", + $CF{"BackURL"} = "", + $CF{"FwdURL"} = "", + $CF{"ImageSubpages"} = 1, + $CF{"AllowArchives"} = 1, + $CF{"PhotoUrlPrefix"} = "", + $CF{"ThumbUrlPrefix"} = "", + $CF{"MetaDataDir"} = ".", + $CF{"PhotoDir"} = ".", + + $CF{'ScanDefaultTransform'} = 'n'; + $CF{'OrigDir'} = '.'; + $CF{'PhotoDir'} = 'photo'; + $CF{"CacheDir"} = "cache", +} + +sub LoadConfig() { + my $cfg = "./gallery.cf"; + unless (defined do $cfg) { + if ($@) { + die "Error parsing $cfg: $@"; + } elsif ($!) { + die "Cannot load $cfg: $!\n"; + } else { + die "Cannot load $cfg, check that it returns true\n"; + } + } +} + +sub SetOptions(@) { + while (my $o = shift @_) { + my $v = shift @_; + $CF{$o} = $v; + if ($o eq "Theme") { + require $CF{"GalDir"} . "/$v/theme.pm"; + Gallery::Theme::Init($CF{"GalURL"} . "/$v"); + } + } +} + +sub WriteList($$) { + my ($file, $images) = @_; + open LIST, '>', "$file.new" or die "Cannot create $file.new: $!\n"; + print LIST "# Image\tID\tRotate\tXform\tTitle\n"; + for my $i (@$images) { + print LIST join("\t", + $i->{file}, + $i->{id}, + $i->{orientation}, + $i->{xfrm}, + ($i->{title} eq '' ? '-' : $i->{title}), + ), "\n"; + } + close LIST; + rename "$file.new", $file or die "Cannot rename $file.new to $file: $!\n"; +} + +sub ReadList($) { + my ($file) = @_; + my @images = (); + open LIST, '<', $file or return; + while () { + chomp; + /^$/ and next; + /^#/ and next; + my $i = {}; + ($i->{file}, $i->{id}, $i->{orientation}, $i->{xfrm}, $i->{title}) = split /\t/; + if ($i->{title} eq '-') { $i->{title} = ""; } + push @images, $i; + } + close LIST; + return \@images; +} + +1; diff --git a/gal2/gal-cat-gqview b/gal2/gal-cat-gqview new file mode 100755 index 0000000..138af5a --- /dev/null +++ b/gal2/gal-cat-gqview @@ -0,0 +1,17 @@ +#!/usr/bin/perl +# UCW Gallery: Extract image list from GQview collection +# (c) 2004--2012 Martin Mares + +use strict; +use warnings; + +while (<>) { + chomp; + /^#/ && next; + /^$/ && next; + if (/^"(.*)"$/) { + print "$1\n"; + } else { + die "Error parsing collection: $_"; + } +} diff --git a/gal2/gal-scan b/gal2/gal-scan new file mode 100755 index 0000000..c1b70d0 --- /dev/null +++ b/gal2/gal-scan @@ -0,0 +1,110 @@ +#!/usr/bin/perl +# UCW Gallery: Scan images and generate image list +# (c) 2004--2012 Martin Mares + +use strict; +use warnings; + +use lib '/home/mj/web/gal2'; +use UCW::Gallery; + +use File::Spec; +use Image::EXIF; +use Digest::SHA; + +if (@ARGV && $ARGV[0] eq '--help') { + die < +AMEN +} + +my @files = (); +if (@ARGV) { + for my $in (@ARGV) { + if (-f $in) { + push @files, $in; + } elsif (-d $in) { + opendir D, $in or die "Cannot scan directory $in: $!\n"; + my @p = (); + while (my $e = readdir D) { + my $f = File::Spec->canonpath(File::Spec->catfile($in, $e)); + if ($f =~ m{\.jpe?g$}i) { + push @p, $f; + } + } + closedir D; + push @files, sort @p; + } else { + die "$in is neither file nor directory\n"; + } + } +} else { + while () { + chomp; + push @files, $_; + } +} + +UCW::Gallery::LoadConfig; + +print "Scanning photos\n"; +my @images = (); +foreach my $f (@files) { + my ($fv, $fd, $ff) = File::Spec->splitpath($f); + print "\t$ff: "; + + my $sha = Digest::SHA->new(1); + $sha->addfile($f) or die "Cannot hash $f\n"; + my $id = substr($sha->hexdigest, 0, 16); + print "id=$id "; + + my $e = new Image::EXIF($f); + my $i = $e->get_all_info(); + if ($e->error) { print "EXIF error: ", $e->error, "\n"; } + else { + # print STDERR Dumper($i), "\n"; + my $o = $i->{'image'}->{'Image Orientation'} || "Top, Left-Hand"; + if ($o eq "Top, Left-Hand") { $o = "."; } + elsif ($o eq "Right-Hand, Top") { $o = "r"; } + elsif ($o eq "Left-Hand, Bottom") { $o = "l"; } + elsif ($o eq "Bottom, Right-Hand") { $o = "d"; } + else { + print "Unrecognized orientation: $o\n"; + $o = "."; + } + push @images, { + file => $f, + id => $id, + orientation => $o, + xfrm => $UCW::Gallery::CF{'ScanDefaultTransform'}, + title => '', + }; + print "ori=$o\n"; + } +} + +my $old = UCW::Gallery::ReadList('gallery.list'); +if ($old) { + print "Updating gallery.list\n"; + my %old_by_id = map { $_->{id} => $_ } @$old; + for my $i (@images) { + my $id = $i->{id}; + my $o = $old_by_id{$id}; + if ($o) { + print "\t$id: updated\n"; + $i->{orientation} = $o->{orientation}; + $i->{xfrm} = $o->{xfrm}; + $i->{title} = $o->{title}; + } else { + print "\t$id: new\n"; + } + delete $old_by_id{$id}; + } + for my $id (keys %old_by_id) { + print "\t$id: removed\n"; + } +} + +UCW::Gallery::WriteList('gallery.list', \@images); +print "Written gallery.list (", (scalar @images), " items)\n";