]> mj.ucw.cz Git - gallery.git/blob - gal2/UCW/Gallery.pm
Gallery2: gen-cache
[gallery.git] / gal2 / 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 BEGIN {
12         # Standard Perl module stuff
13         use Exporter();
14         our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
15         $VERSION = 1.00;
16         @ISA = qw(Exporter);
17         @EXPORT = qw(&SetOptions);
18         %EXPORT_TAGS = ();
19         @EXPORT_OK = qw(%CF);
20 }
21
22 our %CF;
23 our $th;
24
25 BEGIN {
26         $CF{'Title'} = 'An Unnamed Gallery',
27         $CF{'HeadExtras'} = "",
28         $CF{'TopExtras'} = "",
29         $CF{'BotExtras'} = "",
30         $CF{'ParentURL'} = '../',
31         $CF{'BackURL'} = "",
32         $CF{'FwdURL'} = "",
33         $CF{'ImageSubpages'} = 1,
34         $CF{'AllowArchives'} = 1,
35         $CF{'PhotoUrlPrefix'} = "",
36         $CF{'ThumbUrlPrefix'} = "",
37         $CF{'MetaDataDir'} = '.',
38         $CF{'PhotoDir'} = '.',
39
40         $CF{'ScanDefaultTransform'} = 's';
41         $CF{'OrigDir'} = '.';
42         $CF{'PhotoDir'} = 'photo';
43         $CF{'CacheDir'} = 'cache',
44         $CF{'PhotoMaxWidth'} = 1024,
45         $CF{'PhotoMaxHeight'} = 1024,
46         # FIXME: ThumbSizes should be set by themes
47         $CF{'ThumbSizes'} = [ [114,94], [256,256] ],
48 }
49
50 sub LoadConfig() {
51         my $cfg = "./gallery.cf";
52         unless (defined do $cfg) {
53                 if ($@) {
54                         die "Error parsing $cfg: $@";
55                 } elsif ($!) {
56                         die "Cannot load $cfg: $!\n";
57                 } else {
58                         die "Cannot load $cfg, check that it returns true\n";
59                 }
60         }
61 }
62
63 sub SetOptions(@) {
64         while (my $o = shift @_) {
65                 my $v = shift @_;
66                 $CF{$o} = $v;
67                 if ($o eq "Theme") {
68                         require $CF{"GalDir"} . "/$v/theme.pm";
69                         Gallery::Theme::Init($CF{"GalURL"} . "/$v");
70                 }
71         }
72 }
73
74 sub WriteList($$) {
75         my ($file, $images) = @_;
76         open LIST, '>', "$file.new" or die "Cannot create $file.new: $!\n";
77         print LIST "# Image\tID\tRotate\tXform\tTitle\n";
78         for my $i (@$images) {
79                 print LIST join("\t",
80                         $i->{file},
81                         $i->{id},
82                         $i->{orientation},
83                         $i->{xfrm},
84                         ($i->{title} eq '' ? '-' : $i->{title}),
85                 ), "\n";
86         }
87         close LIST;
88         rename "$file.new", $file or die "Cannot rename $file.new to $file: $!\n";
89 }
90
91 sub ReadList($) {
92         my ($file) = @_;
93         my @images = ();
94         open LIST, '<', $file or return;
95         while (<LIST>) {
96                 chomp;
97                 /^$/ and next;
98                 /^#/ and next;
99                 my $i = {};
100                 ($i->{file}, $i->{id}, $i->{orientation}, $i->{xfrm}, $i->{title}) = split /\t/;
101                 if ($i->{title} eq '-') { $i->{title} = ""; }
102                 push @images, $i;
103         }
104         close LIST;
105         return \@images;
106 }
107
108 sub WriteMeta($$) {
109         my ($file, $meta) = @_;
110         open META, '>', "$file.new" or die "Cannot create $file.new: $!\n";
111         Storable::nstore_fd($meta, \*META);
112         close META;
113         rename "$file.new", $file or die "Cannot rename $file.new to $file: $!\n";
114 }
115
116 sub ReadMeta($) {
117         my ($file) = @_;
118         open META, '<', $file or die "Cannot read $file: $!\n";
119         my $meta = Storable::fd_retrieve(\*META) or die "Cannot parse $file\n";
120         close META;
121         return $meta;
122 }
123
124 1;