]> mj.ucw.cz Git - gallery.git/blob - gal/UCW/Gallery.pm
Unify photo_name
[gallery.git] / gal / UCW / Gallery.pm
1 # Simple Photo Gallery
2 # (c) 2003--2012 Martin Mares <mj@ucw.cz>
3
4 package UCW::Gallery;
5
6 use strict;
7 use warnings;
8
9 use Storable;
10
11 ### Class methods ###
12
13 sub new($) {
14         my ($class) = @_;
15         my $self = { };
16         $self->{cfg} = {
17                 # Directories
18                 OrigDir => '.',                 # Original images
19                 PhotoDir => 'photo',            # Scaled-down photos for web
20                 CacheDir => 'cache',            # Cache with meta-data and thumbnails
21
22                 # URL prefixes
23                 PhotoUrlPrefix => 'photo/',
24                 ThumbUrlPrefix => 'thumb/',
25                 ThemeUrlPrefix => 'gal/',
26
27                 # Processing machinery settings
28                 ScanDefaultTransform => 's',
29                 PhotoMaxWidth => 1024,
30                 PhotoMaxHeight => 1024,
31                 ThumbFormats => [ "114x94" ],   # Built-in themes use the first size,
32                                                 # but more can be generated
33
34                 # Titles and navigation
35                 Title => 'An Unnamed Gallery',
36                 SubTitle => "",
37                 ParentURL => '../',
38                 BackURL => "",
39                 FwdURL => "",
40         };
41         return bless $self, $class;
42 }
43
44 sub load_config($) {
45         my $cfg = "./gallery.cf";
46         my $self = do $cfg;
47         unless (defined $self) {
48                 if ($@) {
49                         die "Error parsing $cfg: $@";
50                 } elsif ($!) {
51                         die "Cannot load $cfg: $!\n";
52                 } else {
53                         die "Cannot load $cfg, check that it returns true\n";
54                 }
55         }
56         return $self;
57 }
58
59 ### Object methods ###
60
61 sub get($$) {
62         my ($self, $key) = @_;
63         if (exists $self->{cfg}->{$key}) {
64                 my $val = $self->{cfg}->{$key};
65                 defined $val or warn "Gallery: Config item $key is not set\n";
66                 return $val;
67         } else {
68                 warn "Gallery: Config item $key does not exist\n";
69                 return;
70         }
71 }
72
73 sub try_get($$) {
74         my ($self, $key) = @_;
75         return $self->{cfg}->{$key};
76 }
77
78 sub def($@) {
79         my $self = shift;
80         while (my $key = shift @_) {
81                 my $val = shift @_;
82                 $self->{cfg}->{$key} //= $val;
83         }
84 }
85
86 sub set($@) {
87         my $self = shift;
88         while (my $key = shift @_) {
89                 $self->{cfg}->{$key} = shift @_;
90         }
91 }
92
93 sub get_config_keys($) {
94         my ($self) = @_;
95         return keys %{$self->{cfg}};
96 }
97
98 sub write_list($$$) {
99         my ($self, $file, $images) = @_;
100         open my $fh, '>:utf8', "$file.new" or die "Cannot create $file.new: $!\n";
101         for my $i (@$images) {
102                 print $fh join("\t",
103                         $i->{file},
104                         $i->{id},
105                         $i->{orientation},
106                         $i->{xfrm},
107                         ($i->{title} eq '' ? '-' : $i->{title}),
108                 ), "\n";
109         }
110         close $fh;
111         rename "$file.new", $file or die "Cannot rename $file.new to $file: $!\n";
112 }
113
114 sub read_list($$) {
115         my ($self, $file) = @_;
116         my @images = ();
117         open my $fh, '<:utf8', $file or return;
118         while (<$fh>) {
119                 chomp;
120                 /^$/ and next;
121                 /^#/ and next;
122                 my $i = {};
123                 ($i->{file}, $i->{id}, $i->{orientation}, $i->{xfrm}, $i->{title}) = split /\t/;
124                 if ($i->{title} eq '-') { $i->{title} = ""; }
125                 push @images, $i;
126         }
127         close $fh;
128         return \@images;
129 }
130
131 sub write_meta($$) {
132         my ($self, $file, $meta) = @_;
133         open my $fh, '>', "$file.new" or die "Cannot create $file.new: $!\n";
134         Storable::nstore_fd($meta, $fh);
135         close $fh;
136         rename "$file.new", $file or die "Cannot rename $file.new to $file: $!\n";
137 }
138
139 sub read_meta($) {
140         my ($self, $file) = @_;
141         open my $fh, '<', $file or die "Cannot read $file: $!\n";
142         my $meta = Storable::fd_retrieve($fh) or die "Cannot parse $file\n";
143         close $fh;
144         return $meta;
145 }
146
147 sub photo_meta_name($) {
148         my ($self) = @_;
149         return File::Spec->catfile($self->get('PhotoDir'), 'gallery.meta');
150 }
151
152 sub cache_meta_name($) {
153         my ($self) = @_;
154         return File::Spec->catfile($self->get('CacheDir'), 'cache.meta');
155 }
156
157 sub thumb_fmt_to_size($$) {
158         my ($self, $fmt) = @_;
159         my ($tw, $th) = ($fmt =~ m{^(\d+)x(\d+)$}) or die "Cannot parse thumbnail format $fmt\n";
160         return ($tw, $th);
161 }
162
163 sub photo_file_name($$$) {
164         my ($self, $photo_meta, $id) = @_;
165         return $id . '.' . ($photo_meta->{fmt} // 'jpg');
166 }
167
168 sub photo_name($$$) {
169         my ($self, $photo_meta, $id) = @_;
170         return File::Spec->catfile($self->get('PhotoDir'), $self->photo_file_name($photo_meta, $id));
171 }
172
173
174 1;