]> mj.ucw.cz Git - gallery.git/commitdiff
Gallery2: More of upgrading machinery
authorMartin Mares <mj@ucw.cz>
Sat, 29 Dec 2012 14:12:46 +0000 (15:12 +0100)
committerMartin Mares <mj@ucw.cz>
Sun, 8 Feb 2015 20:14:16 +0000 (21:14 +0100)
gal2/bin/gal-mj-map [new file with mode: 0755]
gal2/bin/gal-mj-upgrade

diff --git a/gal2/bin/gal-mj-map b/gal2/bin/gal-mj-map
new file mode 100755 (executable)
index 0000000..b9f48d9
--- /dev/null
@@ -0,0 +1,26 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Digest::SHA;
+use File::Find;
+
+STDOUT->autoflush(1);
+
+find({
+       wanted => sub {
+               my $name = $File::Find::name;
+               $name =~ s{^\./}{};
+               $name =~ m{^\d} or return;
+               $name =~ m{\.jpe?g$}i or return;
+               -f $name or return;
+               print "$name\t";
+
+               my $sha = Digest::SHA->new(1);
+               $sha->addfile($name) or die "Cannot hash $name\n";
+               my $id = substr($sha->hexdigest, 0, 16);
+               print "$id\n";
+       },
+       no_chdir => 1,
+}, ".");
index 6b056bc3060803352ab8bde8bdd869807adfca32..6806012fd3ff68eef24c4afab75e2864e78caa19 100755 (executable)
 use strict;
 use warnings;
 
-use Cwd;
-
-system "gal", "mj-init";
-die if $?;
+use UCW::Gallery;
+use File::Spec;
 
 my $photos_root = $ENV{HOME} . '/photos';
 
-my $album = $ARGV[0];
-if (!defined $album) {
-       my $cwd = getcwd;
-       $cwd =~ m{/photos/(.*)} or die "Cannot identify album from current directory, need to specify maunally.\n";
-       $album = $1;
+my $album = $ARGV[0] // die "Usage: gal mj-upgrade <album>\n";
+print "### $album ###\n";
+
+### Scan photos.map ###
+
+my $year = $album;
+$year =~ s{/.*}{};
+if ($album eq '2009/Fireworks') { $year = 2008; }
+print "Loading map for $year\n";
+open M, "$photos_root/photos.map" or die "No map file found\n";
+my %map = ();
+while (<M>) {
+       chomp;
+       m{^$year} or next;
+       my ($path, $hash) = split /\t/;
+       my $name = $path;
+       $name =~ s{.*/}{};
+       if (defined $map{$name}) {
+               my $prev = $map{$name};
+               if ($prev->{hash} ne $hash) {
+                       print STDERR "Collision for $name: ", $prev->{path}, " vs. ", $path, "\n";
+               } else {
+                       # print STDERR "Harmless collision for $name: ", $prev->{path}, " vs. ", $path, "\n";
+               }
+       } else {
+               $map{$name} = { path => $path, hash => $hash };
+       }
+}
+close M;
+
+### Scan static list (if any) ###
+
+my @src = ();
+if (open S, "../static/photos/$album/x") {
+       print "Found static list\n";
+       while (<S>) {
+               chomp;
+               my @fields = split /\t/;
+               if (@fields == 4) {
+                       push @src, { name => $fields[0], rotate => $fields[2], xform => $fields[3] };
+               } elsif (@fields == 2) {
+                       push @src, { name => $fields[0], rotate => $fields[1], xform => '.' };
+               } else {
+                       die "Error parsing gallery list: $_\n";
+               }
+       }
+       close S;
 }
 
-open I, "index.cgi" or die;
-open S, "|-", "gal scan" or die;
+### Parse index.cgi and produce gallery.new ###
+
+open I, "$album/index.cgi" or die "Cannot find $album/index.cgi\n";
+open W, ">$album/gallery.new" or die "Cannot create $album/gallery.new\n";
+my %opt = ();
+my %found_dirs = ();
+my @items = ();
 while (<I>) {
-       /^img\(/ or next;
-       /# .*(IMG_.*\.JPG) / or die;
-       print S "$photos_root/$album/$1\n";
+       chomp;
+       if (/^\s+"(\w+)" => "(.*)",?$/) {
+               $opt{$1} = $2;
+               print "Option: $1 = $2\n";
+       } elsif (/^img\("([^"]+)\.jpe?g", "([^"]*)"\);\s*# (\S+)/) {
+               my $nr = $1;
+               my $title = $2;
+               my $file = $3;
+               $file =~ s{^.*/}{}g;
+
+               my $map = $map{$file};
+               if (!$map) {
+                       print STDERR "$album: No match for $file\n";
+                       next;
+               }
+               my $path = $map->{path};
+               my ($vv, $dd, $ff) = File::Spec->splitpath($path);
+               $found_dirs{$dd} = 1;
+
+               print "Image: $nr $path [$title]\n";
+               my $src;
+               if (@src) {
+                       if ($nr =~ m{^\d+$} && $nr <= @src) {
+                               $src = $src[$nr-1];
+                       } else {
+                               print STDERR "$album: Crooked refs ($nr)\n";
+                       }
+               }
+
+               print W join("\t",
+                       "$photos_root/$path",
+                       $map->{hash},
+                       ($src ? $src->{rotate} : '-'),
+                       ($src ? $src->{xform} : '.'),
+                       ($title ne "" ? $title : '-'),
+                       ), "\n";
+       } elsif (/^($|#|require|SetOptions|\)|Start|Finish)/) {
+               # Nothing important
+       } else {
+               print STDERR "$album/index.cgi: Parse error at line $.: $_\n";
+       }
 }
-close S or die;
+close W;
 close I;
+
+if (scalar keys %found_dirs != 1) {
+       print STDERR "$album: Photos in multiple directories\n";
+}
+
+### Create gallery.cf ###
+
+open CF, ">$album/gallery.cf" or die "Cannot create $album/gallery.cf";
+print CF <<'AMEN' ;
+use strict;
+use warnings;
+use utf8;
+
+my $gal = require '../../default.cf';
+$gal->set(
+AMEN
+
+for my $cf (reverse sort keys %opt) {
+       print CF "\t$cf => \"", $opt{$cf}, "\",\n";
+}
+
+print CF <<'AMEN' ;
+);
+
+return $gal;
+AMEN
+
+close CF;