2 # UCW Gallery: Prepare cache
3 # (c) 2004--2012 Martin Mares <mj@ucw.cz>
19 my $gal = UCW::Gallery->load_config;
21 print "Reading gallery.list\n";
22 my $orig_list = $gal->read_list('gallery.list') or die "Cannot read gallery.list: $!\n";
24 my $photo_dir = $gal->get('PhotoDir');
25 my $photo_meta = File::Spec->catfile($photo_dir, 'gallery.meta');
26 print "Reading meta-data from $photo_meta\n";
27 -f $photo_meta or die "Cannot load $photo_meta\n";
28 my $meta = $gal->read_meta($photo_meta);
30 my $cache_dir = $gal->get('CacheDir');
32 print "Deleting old cache directory: $cache_dir\n";
33 File::Path::remove_tree($cache_dir);
35 print "Creating cache directory: $cache_dir\n";
36 File::Path::mkpath($cache_dir) or die "Unable to create $cache_dir: $!\n";
38 # Construct sequence and store photo titles
39 $meta->{sequence} = [];
40 for my $f (@$orig_list) {
41 push @{$meta->{sequence}}, $f->{id};
42 my $m = $meta->{photo}->{$f->{id}} or die;
43 $m->{title} = $f->{title};
46 for my $thumb_fmt (keys %{$gal->get('ThumbFormats')}) {
47 my ($tw, $th) = ($thumb_fmt =~ m{^(\d+)x(\d+)$}) or die "Cannot parse thumbnail format $thumb_fmt\n";
48 print "Generating $thumb_fmt thumbnails\n";
50 $meta->{thumb}->{$thumb_fmt} = $thumb_meta;
51 my $thumb_dir = File::Spec->catfile($cache_dir, $thumb_fmt);
52 -d $thumb_dir or File::Path::mkpath($thumb_dir) or die "Unable to create $thumb_dir: $!\n";
54 for my $id (@{$meta->{sequence}}) {
55 my $m = $meta->{photo}->{$id} or die;
58 my $p = new Image::Magick;
59 my $photo = File::Spec->catfile($photo_dir, "$id.jpg");
61 $e = $p->Read($photo) and die "Error reading $photo: $e";
78 $p->Resize(width=>$w, height=>$h);
80 my $out = File::Spec->catfile($thumb_dir, "$id.jpg");
81 my $tmp = "$photo.new";
82 $e = $p->Write($tmp) and die "Unable to write $tmp: $e";
83 rename $tmp, $out or die "Unable to rename $tmp to $out: $!\n";
85 $thumb_meta->{$id} = {
94 my $cache_meta = File::Spec->catfile($cache_dir, 'cache.meta');
95 print "Writing meta-data to $cache_meta\n";
96 $gal->write_meta($cache_meta, $meta);