2 # (c) 2003--2014 Martin Mares <mj@ucw.cz>
19 OrigDir => '.', # Original images
20 PhotoDir => 'photo', # Scaled-down photos for web
21 CacheDir => 'cache', # Cache with meta-data and thumbnails
24 PhotoUrlPrefix => 'photo/',
25 ThumbUrlPrefix => 'thumb/',
26 ThemeUrlPrefix => 'gal/',
28 # Processing machinery settings
29 ScanDefaultTransform => 's',
30 PhotoMaxWidth => 1024,
31 PhotoMaxHeight => 1024,
32 ThumbFormats => [ "114x94" ], # Built-in themes use the first size,
33 # but more can be generated
34 CacheExif => 0, # Cache selected EXIF meta-data
36 # Titles and navigation
37 Title => 'An Unnamed Gallery',
46 return bless $self, $class;
50 my $cfg = "./gallery.cf";
52 unless (defined $self) {
54 die "Error parsing $cfg: $@";
56 die "Cannot load $cfg: $!\n";
58 die "Cannot load $cfg, check that it returns true\n";
64 ### Object methods ###
67 my ($self, $key) = @_;
68 if (exists $self->{cfg}->{$key}) {
69 my $val = $self->{cfg}->{$key};
70 defined $val or warn "Gallery: Config item $key is not set\n";
73 warn "Gallery: Config item $key does not exist\n";
79 my ($self, $key) = @_;
80 return $self->{cfg}->{$key};
85 while (my $key = shift @_) {
87 $self->{cfg}->{$key} //= $val;
93 while (my $key = shift @_) {
94 $self->{cfg}->{$key} = shift @_;
98 sub get_config_keys($) {
100 return keys %{$self->{cfg}};
103 sub write_list($$$) {
104 my ($self, $file, $images) = @_;
105 open my $fh, '>:utf8', "$file.new" or die "Cannot create $file.new: $!\n";
106 for my $i (@$images) {
112 ($i->{title} eq '' ? '-' : $i->{title}),
116 rename "$file.new", $file or die "Cannot rename $file.new to $file: $!\n";
120 my ($self, $file) = @_;
122 open my $fh, '<:utf8', $file or return;
128 ($i->{file}, $i->{id}, $i->{orientation}, $i->{xfrm}, $i->{title}) = split /\t/;
129 if ($i->{title} eq '-') { $i->{title} = ""; }
137 my ($self, $file, $meta) = @_;
138 open my $fh, '>', "$file.new" or die "Cannot create $file.new: $!\n";
139 Storable::nstore_fd($meta, $fh);
141 rename "$file.new", $file or die "Cannot rename $file.new to $file: $!\n";
145 my ($self, $file) = @_;
146 open my $fh, '<', $file or die "Cannot read $file: $!\n";
147 my $meta = Storable::fd_retrieve($fh) or die "Cannot parse $file\n";
152 sub photo_meta_name($) {
154 return File::Spec->catfile($self->get('PhotoDir'), 'gallery.meta');
157 sub cache_meta_name($) {
159 return File::Spec->catfile($self->get('CacheDir'), 'cache.meta');
162 sub thumb_fmt_to_size($$) {
163 my ($self, $fmt) = @_;
164 my ($tw, $th) = ($fmt =~ m{^(\d+)x(\d+)$}) or die "Cannot parse thumbnail format $fmt\n";
168 sub photo_file_name($$$) {
169 my ($self, $photo_meta, $id) = @_;
170 return $id . '.' . ($photo_meta->{fmt} // 'jpg');
173 sub photo_name($$$) {
174 my ($self, $photo_meta, $id) = @_;
175 return File::Spec->catfile($self->get('PhotoDir'), $self->photo_file_name($photo_meta, $id));