]> mj.ucw.cz Git - gallery.git/commitdiff
Gallery2: More objectification
authorMartin Mares <mj@ucw.cz>
Wed, 26 Dec 2012 19:06:37 +0000 (20:06 +0100)
committerMartin Mares <mj@ucw.cz>
Sun, 8 Feb 2015 20:14:09 +0000 (21:14 +0100)
gal2/UCW/Gallery.pm
gal2/UCW/Gallery/Web.pm
gal2/gal-cache
gal2/gal-dump-meta
gal2/gal-gen
gal2/gal-scan

index 8b4c3fac2c3686222852e1dfb2b9c2f8357944df..d0fedb7f3554a21bf0351c33ef95d4102da8a672 100644 (file)
@@ -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 (<LIST>) {
+       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;
 }
 
index 95532d885620138499b87e334c7d71c9aa7ddafb..63a7568fe2f544c5b6502f6808f74b2248273e39 100644 (file)
@@ -43,7 +43,7 @@ $theme_hextras
 </head><body>
 $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 "<h1>No such photo</h1>\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 "") {
index 99095075e1b216f81f1993ca550d11045086a3b3..7d581a632dd21d8dd2a29dab4bd7f90b1a76345d 100755 (executable)
@@ -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);
index 78e3f8593306d8998400bd5d222b405571b4d664..28dbd93dfb4eae162eabb077ab5a12aacc0f73f8 100755 (executable)
@@ -12,6 +12,6 @@ use Data::Dumper;
 
 @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);
index 96bd65f36942725f14113e2b9d592e2662bdfc34..bc9c8b61258f45e5823fe320bfeb8f5d7b2b6c03 100755 (executable)
@@ -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);
index 2cccfe13c82c6f4e2cdc373cef03eb146c497bbb..88c06cc270e7d7e4f5cbf8c9f973507eda243729 100755 (executable)
@@ -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";