]> mj.ucw.cz Git - gallery.git/blob - lib/UCW/Gallery/Hashes.pm
Title and SubTitle are now consistently interpreted as HTML
[gallery.git] / lib / UCW / Gallery / Hashes.pm
1 # Simple Photo Gallery: Image Hashes
2 # (c) 2015 Martin Mares <mj@ucw.cz>
3
4 package UCW::Gallery::Hashes;
5
6 use common::sense;
7
8 use File::stat ();
9 use Digest::SHA;
10
11 sub new {
12         my ($class, $gal) = @_;
13         my $self = { gal => $gal };
14
15         if ($gal->get('CacheHashes') && -f 'gallery.cache') {
16                 print "Loading gallery.cache\n";
17                 $self->{cache} = $gal->read_meta('gallery.cache');
18         } else {
19                 $self->{cache} = {};
20         }
21
22         return bless $self, $class;
23 }
24
25 sub write {
26         my ($self) = @_;
27         my $gal = $self->{gal};
28         if ($gal->get('CacheHashes')) {
29                 print "Writing gallery.cache\n";
30                 $gal->write_meta('gallery.cache', $self->{cache});
31         }
32 }
33
34 sub hash_image {
35         my ($self, $path) = @_;
36         my $cache = $self->{cache};
37
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);
41
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);
46         }
47         return $cache->{$key};
48 }
49
50 42;