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