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_dir = $gal->get('PhotoDir');
22 my $photo_meta = $gal->photo_meta_name;
23 print "Reading meta-data from $photo_meta\n";
24 -f $photo_meta or die "Cannot load $photo_meta\n";
25 my $meta = $gal->read_meta($photo_meta);
27 my $cache_dir = $gal->get('CacheDir');
29 print "Deleting old cache directory: $cache_dir\n";
30 File::Path::remove_tree($cache_dir);
32 print "Creating cache directory: $cache_dir\n";
33 File::Path::mkpath($cache_dir) or die "Unable to create $cache_dir: $!\n";
35 # Construct sequence and store photo titles
36 $meta->{sequence} = [];
37 for my $f (@$orig_list) {
38 push @{$meta->{sequence}}, $f->{id};
39 my $m = $meta->{photo}->{$f->{id}} or die;
40 $m->{title} = $f->{title};
43 for my $thumb_fmt (@{$gal->get('ThumbFormats')}) {
44 print "Generating $thumb_fmt thumbnails\n";
45 my ($tw, $th) = $gal->thumb_fmt_to_size($thumb_fmt);
47 $meta->{thumb}->{$thumb_fmt} = $thumb_meta;
48 my $thumb_dir = File::Spec->catfile($cache_dir, $thumb_fmt);
49 -d $thumb_dir or File::Path::mkpath($thumb_dir) or die "Unable to create $thumb_dir: $!\n";
51 for my $id (@{$meta->{sequence}}) {
52 my $m = $meta->{photo}->{$id} or die;
55 my $p = new Image::Magick;
56 my $photo = File::Spec->catfile($photo_dir, "$id.jpg");
58 $e = $p->Read($photo) and die "Error reading $photo: $e";
75 $p->Resize(width=>$w, height=>$h);
77 my $out = File::Spec->catfile($thumb_dir, "$id.jpg");
78 my $tmp = "$photo.new";
79 $e = $p->Write($tmp) and die "Unable to write $tmp: $e";
80 rename $tmp, $out or die "Unable to rename $tmp to $out: $!\n";
82 $thumb_meta->{$id} = {
91 my $cache_meta = $gal->cache_meta_name;
92 print "Writing meta-data to $cache_meta\n";
93 $gal->write_meta($cache_meta, $meta);