From: Martin Mares Date: Wed, 26 Dec 2012 19:06:37 +0000 (+0100) Subject: Gallery2: More objectification X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=fc5a4dedc62fd4c5d0735f78117819f3d4161987;p=gallery.git Gallery2: More objectification --- diff --git a/gal2/UCW/Gallery.pm b/gal2/UCW/Gallery.pm index 8b4c3fa..d0fedb7 100644 --- a/gal2/UCW/Gallery.pm +++ b/gal2/UCW/Gallery.pm @@ -99,13 +99,11 @@ sub require_thumbnails($$$) { 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}, @@ -113,15 +111,15 @@ sub WriteList($$) { ($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 () { + open my $fh, '<', $file or return; + while (<$fh>) { chomp; /^$/ and next; /^#/ and next; @@ -130,24 +128,23 @@ sub ReadList($) { 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; } diff --git a/gal2/UCW/Gallery/Web.pm b/gal2/UCW/Gallery/Web.pm index 95532d8..63a7568 100644 --- a/gal2/UCW/Gallery/Web.pm +++ b/gal2/UCW/Gallery/Web.pm @@ -43,7 +43,7 @@ $theme_hextras $textras EOF - $UCW::CGI::error_hook = \&error; + $UCW::CGI::ErrorHandler::error_hook = \&error; } sub html_bot($) { @@ -55,10 +55,7 @@ sub show_img($) { my ($self) = @_; if ($show_img < 1 || $show_img > $self->{num_photos}) { - print "Status: 404\n"; - $self->html_top; - print "

No such photo

\n"; - $self->html_bot; + UCW::CGI::http_error(404, 'No such photo'); return; } @@ -108,7 +105,7 @@ sub show_list($) { 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 "") { diff --git a/gal2/gal-cache b/gal2/gal-cache index 9909507..7d581a6 100755 --- a/gal2/gal-cache +++ b/gal2/gal-cache @@ -7,7 +7,7 @@ use warnings; use FindBin; use lib $FindBin::Bin; -use UCW::Gallery qw(%CF); +use UCW::Gallery; use Image::Magick; use IO::Handle; @@ -16,18 +16,18 @@ use File::Path; 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); @@ -43,7 +43,7 @@ for my $f (@$orig_list) { $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 = {}; @@ -93,4 +93,4 @@ for my $thumb_fmt (keys %{$CF{'ThumbFormats'}}) { 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); diff --git a/gal2/gal-dump-meta b/gal2/gal-dump-meta index 78e3f85..28dbd93 100755 --- a/gal2/gal-dump-meta +++ b/gal2/gal-dump-meta @@ -12,6 +12,6 @@ use Data::Dumper; @ARGV == 1 or die "Usage: $0 \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); diff --git a/gal2/gal-gen b/gal2/gal-gen index 96bd65f..bc9c8b6 100755 --- a/gal2/gal-gen +++ b/gal2/gal-gen @@ -7,7 +7,7 @@ use warnings; use FindBin; use lib $FindBin::Bin; -use UCW::Gallery qw(%CF); +use UCW::Gallery; use Image::Magick; use IO::Handle; @@ -16,11 +16,11 @@ use File::Path; 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 { @@ -32,7 +32,7 @@ my $photo_meta = $photo_dir . "/gallery.meta"; 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' => {} }; @@ -44,19 +44,19 @@ for my $f (@$orig_list) { 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; } @@ -123,4 +123,4 @@ for my $f (@$orig_list) { } print "Writing meta-data\n"; -UCW::Gallery::WriteMeta($photo_meta, $meta); +$gal->write_meta($photo_meta, $meta); diff --git a/gal2/gal-scan b/gal2/gal-scan index 2cccfe1..88c06cc 100755 --- a/gal2/gal-scan +++ b/gal2/gal-scan @@ -7,7 +7,7 @@ use warnings; use FindBin; use lib $FindBin::Bin; -use UCW::Gallery qw(%CF); +use UCW::Gallery; use File::Spec; use Image::EXIF; @@ -47,8 +47,10 @@ if (@ARGV) { } } -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"; @@ -90,7 +92,7 @@ foreach my $f (@files) { } } -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; @@ -112,5 +114,5 @@ if ($old) { } } -UCW::Gallery::WriteList('gallery.list', \@images); +$gal->write_list('gallery.list', \@images); print "Written gallery.list (", (scalar @images), " items)\n";