allow the new files objects to make thumbnails
authorTony Cook <tony@develop-help.com>
Mon, 2 May 2011 04:18:40 +0000 (14:18 +1000)
committerTony Cook <tony@develop-help.com>
Wed, 4 May 2011 05:56:33 +0000 (15:56 +1000)
MANIFEST
schema/bse.sql
site/cgi-bin/modules/BSE/ImageHandler/Img.pm
site/cgi-bin/modules/BSE/TB/File.pm
site/cgi-bin/modules/BSE/TB/Image.pm
site/cgi-bin/modules/BSE/TB/Images.pm
site/cgi-bin/modules/BSE/ThumbCommon.pm
site/cgi-bin/modules/BSE/UI/Thumb.pm
site/cgi-bin/modules/BSE/Util/Thumb.pm
site/util/mysql.str

index 6cf93ddf77633b76249928ce07a6975fa97a0900..646fa4279a736d0664f4119c259ae1c9134a71b0 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -210,6 +210,7 @@ site/cgi-bin/modules/BSE/Template.pm
 site/cgi-bin/modules/BSE/Thumb/Imager.pm
 site/cgi-bin/modules/BSE/Thumb/Imager/Colourize.pm
 site/cgi-bin/modules/BSE/Thumb/Imager/RandomCrop.pm
+site/cgi-bin/modules/BSE/ThumbCommon.pm
 site/cgi-bin/modules/BSE/ThumbLow.pm
 site/cgi-bin/modules/BSE/UI.pm
 site/cgi-bin/modules/BSE/UI/AdminAudit.pm
index 0483e6d909a71ccf12c6e50236d94a03743c3663..ac76632312810d68b02401857da9fac96b74dce6 100644 (file)
@@ -1209,6 +1209,8 @@ create table bse_files (
 
   description text not null,
 
+  ftype varchar(20) not null default 'img',
+
   index owner(file_type, owner_id)
 );
 
index 9559fc79bd654ce16b9e33015a107bb8844a046a..bf84cc78f819344fe5d8590c40aeffc10bcde6e0 100644 (file)
@@ -4,11 +4,7 @@ use base 'BSE::ImageHandler::Base';
 use Carp qw(confess);
 use BSE::Util::HTML;
 
-our $VERSION = "1.000";
-
-sub thumb_base_url {
-  '/cgi-bin/thumb.pl';
-}
+our $VERSION = "1.001";
 
 sub format {
   my ($self, %opts) = @_;
@@ -109,7 +105,6 @@ sub _make_thumb_hash {
     or return ( undef, "* invalid geometry string: $error *" );
 
   my %im = map { $_ => $im->{$_} } $im->columns;
-  my $base = $self->thumb_base_url;
 
   @im{qw/width height type original/} = 
     $thumbs->thumb_dimensions_sized($geometry, @$im{qw/width height/});
@@ -128,7 +123,10 @@ sub _make_thumb_hash {
       and print STDERR "  Generated $im{image}\n";
   }
   unless ($im{image}) {
-    $im{image} = "$base?g=$geo_id&page=$im->{articleId}&image=$im->{id}";
+    $im{image} = $im->dynamic_thumb_url
+      (
+       geo => $geo_id
+      );
 
     if (defined $im{type}) {
       $im{image} .= "&type.$im{type}";
index 98b021fdbd483e21c3a2685a7b0fa8d6c62f4362..cc0782fd5a051b64ce709f22f45041df65457694 100644 (file)
@@ -1,16 +1,17 @@
 package BSE::TB::File;
 use strict;
 use Squirrel::Row;
+use BSE::ThumbCommon;
 use vars qw/@ISA/;
-@ISA = qw/Squirrel::Row/;
+@ISA = qw/Squirrel::Row BSE::ThumbCommon/;
 use Carp 'confess';
 
-our $VERSION = "1.001";
+our $VERSION = "1.002";
 
 sub columns {
   return qw/id file_type owner_id filename display_name content_type
             size_in_bytes is_public name display_order src category
-            alt width height url description/;
+            alt width height url description ftype/;
 }
 
 sub table {
@@ -32,6 +33,7 @@ sub defaults {
      width => 0,
      height => 0,
      url => '',
+     ftype => "img",
     );
 }
 
@@ -121,4 +123,13 @@ sub json_data {
   return $data;
 }
 
+sub dynamic_thumb_url {
+  my ($self, %opts) = @_;
+
+  my $geo = delete $opts{geo}
+    or Carp::confess("Missing geo parameter");
+
+  return "/cgi-bin/thumb.pl?s=file&g=$geo&image=$self->{id}";
+}
+
 1;
index f91fe71d66ef38fae6b7be1937bde79236f940ff..ddd3ce69e5915df5a27a693933ad83891d9a8c57 100644 (file)
@@ -2,27 +2,18 @@ package BSE::TB::Image;
 use strict;
 # represents an image from the database
 use Squirrel::Row;
+use BSE::ThumbCommon;
 use vars qw/@ISA/;
-@ISA = qw/Squirrel::Row/;
+@ISA = qw/Squirrel::Row BSE::ThumbCommon/;
 use Carp qw(confess);
-use BSE::Util::HTML qw(escape_html);
 
-our $VERSION = "1.001";
+our $VERSION = "1.002";
 
 sub columns {
   return qw/id articleId image alt width height url displayOrder name
             storage src ftype/;
 }
 
-sub _handler_object {
-  my ($im, $cfg) = @_;
-
-  my $module = "BSE::ImageHandler::" . ucfirst($im->ftype);
-  (my $file = $module . ".pm") =~ s(::)(/)g;
-  require $file;
-  my $handler = $module->new(cfg => $cfg);
-}
-
 sub formatted {
   my ($self, %opts) = @_;
 
@@ -53,21 +44,6 @@ sub inline {
     );
 }
 
-sub thumb {
-  my ($im, %opts) = @_;
-
-  my $cfg = delete $opts{cfg}
-    or confess "Missing cfg parameter";
-
-  my $handler = $im->_handler_object($cfg);
-
-  return $handler->thumb
-    (
-     image => $im,
-     %opts,
-    );
-}
-
 sub popimage {
   my ($im, %opts) = @_;
 
@@ -98,4 +74,31 @@ sub json_data {
   return $data;
 }
 
+sub dynamic_thumb_url {
+  my ($self, %opts) = @_;
+
+  my $geo = delete $opts{geo}
+    or Carp::confess("missing geo option");
+
+  return $self->thumb_base_url
+    . "?g=$geo&page=$self->{articleId}&image=$self->{id}";
+}
+
+sub thumb_base_url {
+  '/cgi-bin/thumb.pl';
+}
+
+sub full_filename {
+  my ($self) = @_;
+
+  return BSE::TB::Images->image_dir() . "/" . $self->image;
+}
+
+# compatibility with BSE::TB::File
+sub filename {
+  my ($self) = @_;
+
+  return $self->image;
+}
+
 1;
index 019a154d5e1ce758e3f793179a546a286ecc8b0d..28c992b2bafe7bdd80a4aaed370b413d67125a4b 100644 (file)
@@ -5,7 +5,7 @@ use vars qw(@ISA $VERSION);
 @ISA = qw(Squirrel::Table);
 use BSE::TB::Image;
 
-our $VERSION = "1.000";
+our $VERSION = "1.001";
 
 sub rowClass {
   return 'BSE::TB::Image';
@@ -15,4 +15,10 @@ sub image_storages {
   return map [ $_->{image}, $_->{storage}, $_ ], BSE::TB::Images->all;
 }
 
+sub image_dir {
+  require BSE::CfgInfo;
+
+  return BSE::CfgInfo::cfg_image_dir(BSE::Cfg->single);
+}
+
 1;
index 9bb100ffb23f8c84f6b3eef818bcd538fb3d03cb..f74a0e3325e87a289b7d57bc95cfb6f276f5feee 100644 (file)
@@ -1,11 +1,16 @@
 package BSE::ThumbCommon;
 use strict;
+use Carp ();
+
+our $VERSION = "1.000";
 
 # common code between article images and BSE::TB::File
 
 sub _handler_object {
   my ($im, $cfg) = @_;
 
+  $cfg ||= BSE::Cfg->single;
+
   my $module = "BSE::ImageHandler::" . ucfirst($im->ftype);
   (my $file = $module . ".pm") =~ s(::)(/)g;
   require $file;
@@ -15,8 +20,7 @@ sub _handler_object {
 sub thumb {
   my ($im, %opts) = @_;
 
-  my $cfg = delete $opts{cfg}
-    or confess "Missing cfg parameter";
+  my $cfg = delete $opts{cfg};
 
   my $handler = $im->_handler_object($cfg);
 
index 014e2ae17e6b69d870e3005125e41b0589c04703..5649ee154ea5c228d3fdd4e3052d477f1735e405 100644 (file)
@@ -1,11 +1,10 @@
 package BSE::UI::Thumb;
 use strict;
 use base 'BSE::UI::Dispatch'; # for error
-use BSE::TB::Images;
 use BSE::CfgInfo qw(cfg_image_dir);
 use BSE::Util::Thumb;
 
-our $VERSION = "1.001";
+our $VERSION = "1.003";
 
 sub dispatch {
   my ($class, $req) = @_;
@@ -22,15 +21,25 @@ sub dispatch {
   my $geometry_id = $cgi->param('g');
   my $image_id = $cgi->param('image');
   my $article_id = $cgi->param('page');
+  my $source = $cgi->param("s") || "article";
   my $error;
   my $geometry = $cfg->entry('thumb geometries', $geometry_id)
     or return $class->error($req, "** cannot find thumb geometry $geometry_id **");
   $thumbs->validate_geometry($geometry, \$error)
     or return $class->error($req, "invalid geometry string: $error");
   
-  my $image = BSE::TB::Images->getByPkey($image_id);
-  $image && $image->{articleId} == $article_id
-    or return $class->error($req, "image not found");
+  my $image;
+  if ($source eq "article") {
+    require BSE::TB::Images;
+    $image = BSE::TB::Images->getByPkey($image_id);
+    $image && $image->{articleId} == $article_id
+      or return $class->error($req, "image not found");
+  }
+  elsif ($source eq "file") {
+    require BSE::TB::Files;
+    $image = BSE::TB::Files->getByPkey($image_id)
+      or return $class->error($req, "image not found");
+  }
 
   my $do_cache = $cfg->entry('basic', 'cache_thumbnails', 1);
   my $image_dir = cfg_image_dir($cfg);
@@ -38,7 +47,7 @@ sub dispatch {
   my $cache_dir = $cfg->entry('paths', 'scalecache', "$image_dir/scaled");
   my $cache_base_url = $cfg->entry('paths', 'scalecacheurl', '/images/scaled');
 
-  my ($start_type) = $image->{image} =~ /\.(\w+)$/;
+  my ($start_type) = $image->filename =~ /\.(\w+)$/;
   $start_type ||= 'png';
 
   my ($width, $height, $type) = 
@@ -79,7 +88,7 @@ sub dispatch {
   else {
     # not caching, just generate it
 
-    my $filename = "$image_dir/$image->{image}";
+    my $filename = $image->full_filename;
     -e $filename 
       or return $class->error($req, "image file missing");
     
index 887218577b662c82f21224756dc37a994f729b8c..e46d703518d5e7a09c9952eda4bbdfa77c3723e4 100644 (file)
@@ -4,7 +4,7 @@ use BSE::TB::Images;
 use BSE::CfgInfo qw(cfg_image_dir);
 use BSE::StorageMgr::Thumbs;
 
-our $VERSION = "1.000";
+our $VERSION = "1.001";
 
 # returns a list of $url, $filename, $basename
 sub generate_thumb {
@@ -20,13 +20,13 @@ sub generate_thumb {
   my $cache_dir = $cfg->entry('paths', 'scalecache', "$image_dir/scaled");
   my $cache_base_url = $cfg->entry('paths', 'scalecacheurl', '/images/scaled');
 
-  my ($start_type) = $image->{image} =~ /\.(\w+)$/;
+  my ($start_type) = $image->filename =~ /\.(\w+)$/;
   $start_type ||= 'png';
 
   my ($width, $height, $type) = 
     $thumbs->thumb_dimensions_sized($geometry, @$image{qw/width height/}, $start_type);
   
-  my $basename = "$geometry_id-$image->{image}";
+  my $basename = "$geometry_id-" . $image->filename;
 
   if ($type eq 'jpeg' && $basename !~ /\.jpe?g$/i) {
     $basename .= ".jpg";
@@ -44,10 +44,10 @@ sub generate_thumb {
     $cache_url = $storage->url($basename);
   }
   else {
-    my $image_filename = "$image_dir/$image->{image}";
+    my $image_filename = $image->full_filename;
     unless (-e $image_filename) {
       warn "Image file $image_filename missing\n";
-      die "Image file $image->{image} missing\n";
+      die "Image file ", $image->filename, " missing\n";
     }
 
     my $error;
index afbadf0721aace022cd6deddbc6c9864bc73eec7..b6645f3dabb168aa23110c3697e87f4d92c191b1 100644 (file)
@@ -207,6 +207,7 @@ Column width;int(11);YES;NULL;
 Column height;int(11);YES;NULL;
 Column url;varchar(255);YES;NULL;
 Column description;text;NO;NULL;
+Column ftype;varchar(20);NO;img;
 Index PRIMARY;1;[id]
 Index owner;0;[file_type;owner_id]
 Table bse_locations