return $fmt;
}
-### Subroutines (to be converted to methods later) ###
-
-sub WriteList($$) {
- my ($file, $images) = @_;
- open LIST, '>', "$file.new" or die "Cannot create $file.new: $!\n";
+sub write_list($$$) {
+ my ($self, $file, $images) = @_;
+ open my $fh, '>', "$file.new" or die "Cannot create $file.new: $!\n";
for my $i (@$images) {
- print LIST join("\t",
+ print $fh join("\t",
$i->{file},
$i->{id},
$i->{orientation},
($i->{title} eq '' ? '-' : $i->{title}),
), "\n";
}
- close LIST;
+ close $fh;
rename "$file.new", $file or die "Cannot rename $file.new to $file: $!\n";
}
-sub ReadList($) {
- my ($file) = @_;
+sub read_list($$) {
+ my ($self, $file) = @_;
my @images = ();
- open LIST, '<', $file or return;
- while (<LIST>) {
+ open my $fh, '<', $file or return;
+ while (<$fh>) {
chomp;
/^$/ and next;
/^#/ and next;
if ($i->{title} eq '-') { $i->{title} = ""; }
push @images, $i;
}
- close LIST;
+ close $fh;
return \@images;
}
-sub WriteMeta($$) {
- my ($file, $meta) = @_;
- open META, '>', "$file.new" or die "Cannot create $file.new: $!\n";
- Storable::nstore_fd($meta, \*META);
- close META;
+sub write_meta($$) {
+ my ($self, $file, $meta) = @_;
+ open my $fh, '>', "$file.new" or die "Cannot create $file.new: $!\n";
+ Storable::nstore_fd($meta, $fh);
+ close $fh;
rename "$file.new", $file or die "Cannot rename $file.new to $file: $!\n";
}
-sub ReadMeta($) {
- my ($file) = @_;
- # FIXME: open my META
- open META, '<', $file or die "Cannot read $file: $!\n";
- my $meta = Storable::fd_retrieve(\*META) or die "Cannot parse $file\n";
- close META;
+sub read_meta($) {
+ my ($self, $file) = @_;
+ open my $fh, '<', $file or die "Cannot read $file: $!\n";
+ my $meta = Storable::fd_retrieve($fh) or die "Cannot parse $file\n";
+ close $fh;
return $meta;
}
</head><body>
$textras
EOF
- $UCW::CGI::error_hook = \&error;
+ $UCW::CGI::ErrorHandler::error_hook = \&error;
}
sub html_bot($) {
my ($self) = @_;
if ($show_img < 1 || $show_img > $self->{num_photos}) {
- print "Status: 404\n";
- $self->html_top;
- print "<h1>No such photo</h1>\n";
- $self->html_bot;
+ UCW::CGI::http_error(404, 'No such photo');
return;
}
sub dispatch($) {
my ($self) = @_;
UCW::CGI::parse_args(\%args);
- $self->{meta} = UCW::Gallery::ReadMeta(File::Spec->catfile($self->get('CacheDir'), 'cache.meta'));
+ $self->{meta} = $self->{gal}->read_meta(File::Spec->catfile($self->get('CacheDir'), 'cache.meta'));
$self->{num_photos} = scalar @{$self->{meta}->{sequence}};
if ($show_img ne "") {
use FindBin;
use lib $FindBin::Bin;
-use UCW::Gallery qw(%CF);
+use UCW::Gallery;
use Image::Magick;
use IO::Handle;
STDOUT->autoflush(1);
-UCW::Gallery::LoadConfig;
+my $gal = UCW::Gallery->load_config;
print "Reading gallery.list\n";
-my $orig_list = UCW::Gallery::ReadList('gallery.list') or die "Cannot read gallery.list: $!\n";
+my $orig_list = $gal->read_list('gallery.list') or die "Cannot read gallery.list: $!\n";
-my $photo_dir = $CF{'PhotoDir'};
+my $photo_dir = $gal->get('PhotoDir');
my $photo_meta = File::Spec->catfile($photo_dir, 'gallery.meta');
print "Reading meta-data from $photo_meta\n";
-f $photo_meta or die "Cannot load $photo_meta\n";
-my $meta = UCW::Gallery::ReadMeta($photo_meta);
+my $meta = $gal->read_meta($photo_meta);
-my $cache_dir = $CF{'CacheDir'};
+my $cache_dir = $gal->get('CacheDir');
if (-d $cache_dir) {
print "Deleting old cache directory: $cache_dir\n";
File::Path::remove_tree($cache_dir);
$m->{title} = $f->{title};
}
-for my $thumb_fmt (keys %{$CF{'ThumbFormats'}}) {
+for my $thumb_fmt (keys %{$gal->get('ThumbFormats')}) {
my ($tw, $th) = ($thumb_fmt =~ m{^(\d+)x(\d+)$}) or die "Cannot parse thumbnail format $thumb_fmt\n";
print "Generating $thumb_fmt thumbnails\n";
my $thumb_meta = {};
my $cache_meta = File::Spec->catfile($cache_dir, 'cache.meta');
print "Writing meta-data to $cache_meta\n";
-UCW::Gallery::WriteMeta($cache_meta, $meta);
+$gal->write_meta($cache_meta, $meta);
@ARGV == 1 or die "Usage: $0 <meta-file>\n";
-UCW::Gallery::LoadConfig;
-my $meta = UCW::Gallery::ReadMeta($ARGV[0]);
+my $gal = UCW::Gallery->load_config;
+my $meta = $gal->read_meta($ARGV[0]);
print Dumper($meta);
use FindBin;
use lib $FindBin::Bin;
-use UCW::Gallery qw(%CF);
+use UCW::Gallery;
use Image::Magick;
use IO::Handle;
STDOUT->autoflush(1);
-UCW::Gallery::LoadConfig;
+my $gal = UCW::Gallery->load_config;
-my $orig_list = UCW::Gallery::ReadList('gallery.list') or die "Cannot read gallery.list: $!\n";
+my $orig_list = $gal->read_list('gallery.list') or die "Cannot read gallery.list: $!\n";
-my $photo_dir = $CF{'PhotoDir'};
+my $photo_dir = $gal->get('PhotoDir');
if (-d $photo_dir) {
print "Using existing output directory: $photo_dir\n";
} else {
my $old_meta = {};
if (-f $photo_meta) {
print "Reading old meta-data\n";
- $old_meta = UCW::Gallery::ReadMeta($photo_meta);
+ $old_meta = $gal->read_meta($photo_meta);
# use Data::Dumper; print "Read old meta: ", Dumper($old_meta), "\n";
}
my $meta = { 'photo' => {} };
print "$id: ";
my $p = new Image::Magick;
- my $orig = File::Spec->rel2abs($f->{file}, $CF{'OrigDir'});
+ my $orig = File::Spec->rel2abs($f->{file}, $gal->get('OrigDir'));
my ($orig_w, $orig_h, $orig_size, $orig_format) = $p->PingImage($orig) or die "Error reading $orig\n";
print "${orig_w}x${orig_h} ";
my ($w0, $h0) = ($rotate eq "l" || $rotate eq "r") ? ($orig_h, $orig_w) : ($orig_w, $orig_h);
my ($w, $h) = ($w0, $h0);
- if ($w > $CF{'PhotoMaxWidth'}) {
- my $s = $CF{'PhotoMaxWidth'} / $w;
+ if ($w > $gal->get('PhotoMaxWidth')) {
+ my $s = $gal->get('PhotoMaxWidth') / $w;
$w = $w * $s;
$h = $h * $s;
}
- if ($h > $CF{'PhotoMaxHeight'}) {
- my $s = $CF{'PhotoMaxHeight'} / $h;
+ if ($h > $gal->get('PhotoMaxHeight')) {
+ my $s = $gal->get('PhotoMaxHeight') / $h;
$w = $w * $s;
$h = $h * $s;
}
}
print "Writing meta-data\n";
-UCW::Gallery::WriteMeta($photo_meta, $meta);
+$gal->write_meta($photo_meta, $meta);
use FindBin;
use lib $FindBin::Bin;
-use UCW::Gallery qw(%CF);
+use UCW::Gallery;
use File::Spec;
use Image::EXIF;
}
}
-UCW::Gallery::LoadConfig;
-my $orig_prefix = $CF{'OrigDir'};
+STDOUT->autoflush(1);
+
+my $gal = UCW::Gallery->load_config;
+my $orig_prefix = $gal->get('OrigDir');
$orig_prefix =~ m{/$} or $orig_prefix .= '/';
print "Scanning photos\n";
}
}
-my $old = UCW::Gallery::ReadList('gallery.list');
+my $old = $gal->read_list('gallery.list');
if ($old) {
print "Updating gallery.list\n";
my %old_by_id = map { $_->{id} => $_ } @$old;
}
}
-UCW::Gallery::WriteList('gallery.list', \@images);
+$gal->write_list('gallery.list', \@images);
print "Written gallery.list (", (scalar @images), " items)\n";