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