2 # (c) 2003--2015 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
35 CacheHashes => 1, # Let gal-scan cache file hashes
37 # Titles and navigation
38 Title => 'An Unnamed Gallery',
47 return bless $self, $class;
51 my $cfg = "./gallery.cf";
53 unless (defined $self) {
55 die "Error parsing $cfg: $@";
57 die "Cannot load $cfg: $!\n";
59 die "Cannot load $cfg, check that it returns true\n";
65 ### Object methods ###
68 my ($self, $key) = @_;
69 if (exists $self->{cfg}->{$key}) {
70 my $val = $self->{cfg}->{$key};
71 defined $val or warn "Gallery: Config item $key is not set\n";
74 warn "Gallery: Config item $key does not exist\n";
80 my ($self, $key) = @_;
81 return $self->{cfg}->{$key};
86 while (my $key = shift @_) {
88 $self->{cfg}->{$key} //= $val;
94 while (my $key = shift @_) {
95 $self->{cfg}->{$key} = shift @_;
99 sub get_config_keys($) {
101 return keys %{$self->{cfg}};
105 'file' => 0, # 0 = not permitted as extended attribute
107 'orientation' => 1, # 1 = aliases for normal attributes
109 'xf' => 2, # 2 = ... and propagated to gal-gen
110 'lat' => 3, # 3 = normal extended attributes, propagated to gal-gen
116 sub write_list($$$) {
117 my ($self, $file, $images) = @_;
118 open my $fh, '>:utf8', "$file.new" or die "Cannot create $file.new: $!\n";
119 for my $i (@$images) {
125 ($i->{title} eq '' ? '-' : $i->{title}),
127 for my $k (keys %$i) {
128 print $fh "\t$k=", $i->{$k}, "\n" if $list_attrs{$k} >= 3;
132 rename "$file.new", $file or die "Cannot rename $file.new to $file: $!\n";
135 sub read_list_fh($$) {
136 my ($self, $fh) = @_;
143 @images or die "Misplaced continuation line before first image\n";
144 if (my ($k, $v) = /^\t+(.*?)=(.*)/) {
145 # Continutation of previous line
147 if ($list_attrs{$k}) {
150 print STDERR "Ignoring unknown attribute $k for ", $i->{file}, "\n";
153 die "Invalid continuation line. Expecting 'key=value'.\n";
157 ($i->{file}, $i->{id}, $i->{orientation}, $i->{xf}, $i->{title}) = split /\t/;
158 if (!defined $i->{title} || $i->{title} eq '-') { $i->{title} = ""; }
166 my ($self, $file) = @_;
167 open my $fh, '<:utf8', $file or return;
168 my $list = $self->read_list_fh($fh);
174 my ($self, $file, $meta) = @_;
175 open my $fh, '>', "$file.new" or die "Cannot create $file.new: $!\n";
176 Storable::nstore_fd($meta, $fh);
178 rename "$file.new", $file or die "Cannot rename $file.new to $file: $!\n";
182 my ($self, $file) = @_;
183 open my $fh, '<', $file or die "Cannot read $file: $!\n";
184 my $meta = Storable::fd_retrieve($fh) or die "Cannot parse $file\n";
189 sub photo_meta_name($) {
191 return File::Spec->catfile($self->get('PhotoDir'), 'gallery.meta');
194 sub cache_meta_name($) {
196 return File::Spec->catfile($self->get('CacheDir'), 'cache.meta');
199 sub thumb_fmt_to_size($$) {
200 my ($self, $fmt) = @_;
201 my ($tw, $th) = ($fmt =~ m{^(\d+)x(\d+)$}) or die "Cannot parse thumbnail format $fmt\n";
205 sub photo_file_name($$$) {
206 my ($self, $photo_meta, $id) = @_;
207 return $id . '.' . ($photo_meta->{fmt} // 'jpg');
210 sub photo_name($$$) {
211 my ($self, $photo_meta, $id) = @_;
212 return File::Spec->catfile($self->get('PhotoDir'), $self->photo_file_name($photo_meta, $id));