]> mj.ucw.cz Git - gallery.git/commitdiff
Added caching of image hashes (optional)
authorMartin Mares <mj@ucw.cz>
Sun, 8 Feb 2015 18:14:15 +0000 (19:14 +0100)
committerMartin Mares <mj@ucw.cz>
Sun, 8 Feb 2015 20:14:18 +0000 (21:14 +0100)
gal/Makefile
gal/UCW/Gallery.pm
gal/UCW/Gallery/Hashes.pm [new file with mode: 0644]
gal/bin/gal-scan

index fcab4134318bc1ac027cef4e83aa2fc75e6d4f14..5c6a23bdc03052781ae2b706aebd64d22c10ff2a 100644 (file)
@@ -1,7 +1,7 @@
 $(eval $(dir-setup))
 
 $(call lib-copy, UCW/Gallery.pm)
-$(call lib-copy, $(addprefix UCW/Gallery/, Web.pm Archive.pm))
+$(call lib-copy, $(addprefix UCW/Gallery/, Web.pm HashCache.pm Archive.pm))
 $(call lib-copy, $(addprefix UCW/Gallery/Web/, Plain.pm NrtBlue.pm HighSlide.pm))
 
 $(call copy, $(addprefix nrt-blue/,back.png bot.png left.png next.png prev.png right.png top.png style.css))
index c5d5c9b5b09483f1aa24cf6561fe78d0548bc9ef..1e7bba320a733b566b255dedf6a67f501e96b3e8 100644 (file)
@@ -32,6 +32,7 @@ sub new($) {
                ThumbFormats => [ "114x94" ],   # Built-in themes use the first size,
                                                # but more can be generated
                CacheExif => 0,                 # Cache selected EXIF meta-data
+               CacheHashes => 1,               # Let gal-scan cache file hashes
 
                # Titles and navigation
                Title => 'An Unnamed Gallery',
diff --git a/gal/UCW/Gallery/Hashes.pm b/gal/UCW/Gallery/Hashes.pm
new file mode 100644 (file)
index 0000000..d0414e9
--- /dev/null
@@ -0,0 +1,51 @@
+# Simple Photo Gallery: Image Hashes
+# (c) 2015 Martin Mares <mj@ucw.cz>
+
+package UCW::Gallery::Hashes;
+
+use strict;
+use warnings;
+
+use File::stat ();
+use Digest::SHA;
+
+sub new {
+       my ($class, $gal) = @_;
+       my $self = { gal => $gal };
+
+       if ($gal->get('CacheHashes') && -f 'gallery.cache') {
+               print "Loading gallery.cache\n";
+               $self->{cache} = $gal->read_meta('gallery.cache');
+       } else {
+               $self->{cache} = {};
+       }
+
+       return bless $self, $class;
+}
+
+sub write {
+       my ($self) = @_;
+       my $gal = $self->{gal};
+       if ($gal->get('CacheHashes')) {
+               print "Writing gallery.cache\n";
+               $gal->write_meta('gallery.cache', $self->{cache});
+       }
+}
+
+sub hash_image {
+       my ($self, $path) = @_;
+       my $cache = $self->{cache};
+
+       my $st = File::stat::stat($path) or die "Cannot access $path: $!\n";
+       my $key_text = join(":", $path, $st->dev, $st->ino, $st->mtime);
+       my $key = Digest::SHA->sha1_base64($key_text);
+
+       if (!exists $cache->{$key}) {
+               my $sha = Digest::SHA->new(1);
+               $sha->addfile($path) or die "Cannot hash $path\n";
+               $cache->{$key} = substr($sha->hexdigest, 0, 16);
+       }
+       return $cache->{$key};
+}
+
+42;
index a1f5e5b440c1f18607b91a723982e5b30c3169a5..4424cc3732b78a987b610ab776876e8be9913f87 100755 (executable)
@@ -1,14 +1,14 @@
 #!/usr/bin/perl
 # UCW Gallery: Scan images and generate image list
-# (c) 2004--2012 Martin Mares <mj@ucw.cz>
+# (c) 2004--2015 Martin Mares <mj@ucw.cz>
 
 use strict;
 use warnings;
 
 use UCW::Gallery;
+use UCW::Gallery::Hashes;
 use File::Spec;
 use Image::EXIF;
-use Digest::SHA;
 use Getopt::Long;
 
 if (@ARGV && $ARGV[0] eq '--help') {
@@ -60,6 +60,8 @@ if ($update) {
        }
 }
 
+my $hashes = UCW::Gallery::Hashes->new($gal);
+
 print "Scanning photos\n";
 my @images = ();
 foreach my $src (@source) {
@@ -75,11 +77,7 @@ foreach my $src (@source) {
        my $path = File::Spec->rel2abs($name, $gal->get('OrigDir'));
        -f $path or die "Cannot find $path\n";
 
-       if (!defined $src->{id}) {
-               my $sha = Digest::SHA->new(1);
-               $sha->addfile($path) or die "Cannot hash $path\n";
-               $src->{id} = substr($sha->hexdigest, 0, 16);
-       }
+       $src->{id} = $hashes->hash_image($path);
        print " id=", $src->{id};
 
        if (!defined $src->{orientation} || $src->{orientation} eq '-') {
@@ -141,3 +139,4 @@ if (!$update) {
 
 $gal->write_list('gallery.list', \@images);
 print "Written gallery.list (", (scalar @images), " items)\n";
+$hashes->write;