1 # Simple Photo Gallery: Image Hashes
2 # (c) 2015 Martin Mares <mj@ucw.cz>
4 package UCW::Gallery::Hashes;
12 my ($class, $gal) = @_;
13 my $self = { gal => $gal };
15 if ($gal->get('CacheHashes') && -f 'gallery.cache') {
16 print "Loading gallery.cache\n";
17 $self->{cache} = $gal->read_meta('gallery.cache');
22 return bless $self, $class;
27 my $gal = $self->{gal};
28 if ($gal->get('CacheHashes')) {
29 print "Writing gallery.cache\n";
30 $gal->write_meta('gallery.cache', $self->{cache});
35 my ($self, $path) = @_;
36 my $cache = $self->{cache};
38 my $st = File::stat::stat($path) or die "Cannot access $path: $!\n";
39 my $key_text = join(":", $path, $st->dev, $st->ino, $st->mtime);
40 my $key = Digest::SHA->sha1_base64($key_text);
42 if (!exists $cache->{$key}) {
43 my $sha = Digest::SHA->new(1);
44 $sha->addfile($path) or die "Cannot hash $path\n";
45 $cache->{$key} = substr($sha->hexdigest, 0, 16);
47 return $cache->{$key};