2 # UCW Gallery: Prepare cache
3 # (c) 2004--2012 Martin Mares <mj@ucw.cz>
16 my $gal = UCW::Gallery->load_config;
18 print "Reading gallery.list\n";
19 my $orig_list = $gal->read_list('gallery.list') or die "Cannot read gallery.list: $!\n";
21 my $photo_meta = $gal->photo_meta_name;
22 print "Reading meta-data from $photo_meta\n";
23 -f $photo_meta or die "Cannot load $photo_meta\n";
24 my $meta = $gal->read_meta($photo_meta);
26 my $cache_dir = $gal->get('CacheDir');
28 print "Deleting old cache directory: $cache_dir\n";
29 File::Path::remove_tree($cache_dir);
31 print "Creating cache directory: $cache_dir\n";
32 File::Path::mkpath($cache_dir) or die "Unable to create $cache_dir: $!\n";
34 # Construct sequence and store photo titles
35 $meta->{sequence} = [];
36 for my $f (@$orig_list) {
37 push @{$meta->{sequence}}, $f->{id};
38 my $m = $meta->{photo}->{$f->{id}} or die;
39 $m->{title} = $f->{title};
42 for my $thumb_fmt (@{$gal->get('ThumbFormats')}) {
43 print "Generating $thumb_fmt thumbnails\n";
44 my ($tw, $th) = $gal->thumb_fmt_to_size($thumb_fmt);
46 $meta->{thumb}->{$thumb_fmt} = $thumb_meta;
47 my $thumb_dir = File::Spec->catfile($cache_dir, $thumb_fmt);
48 -d $thumb_dir or File::Path::mkpath($thumb_dir) or die "Unable to create $thumb_dir: $!\n";
50 for my $id (@{$meta->{sequence}}) {
51 my $m = $meta->{photo}->{$id} or die;
54 my $p = new Image::Magick;
55 my $photo = $gal->photo_name($m, $id);
57 $e = $p->Read($photo) and die "Error reading $photo: $e";
74 $p->Resize(width=>$w, height=>$h);
76 my $out = File::Spec->catfile($thumb_dir, "$id.jpg");
77 my $tmp = "$photo.new";
78 $e = $p->Write($tmp) and die "Unable to write $tmp: $e";
79 rename $tmp, $out or die "Unable to rename $tmp to $out: $!\n";
81 $thumb_meta->{$id} = {
90 my $cache_meta = $gal->cache_meta_name;
91 print "Writing meta-data to $cache_meta\n";
92 $gal->write_meta($cache_meta, $meta);