ensure image filenames don't overflow their allotted columns
authorTony Cook <tony@develop-help.com>
Wed, 13 Apr 2011 13:24:45 +0000 (23:24 +1000)
committerTony Cook <tony@develop-help.com>
Wed, 13 Apr 2011 13:24:45 +0000 (23:24 +1000)
schema/bse.sql
site/cgi-bin/modules/BSE/Edit/Article.pm
site/util/mysql.str

index bbfdaa797262d6716b1b111354c84330d688fb16..c488bf28d7c38f983199e577db0417a9be9e8524 100644 (file)
@@ -136,7 +136,7 @@ DROP TABLE IF EXISTS image;
 CREATE TABLE image (
   id mediumint(8) unsigned NOT NULL auto_increment,
   articleId integer not null,
-  image varchar(64) DEFAULT '' NOT NULL,
+  image varchar(255) DEFAULT '' NOT NULL,
   alt varchar(255) DEFAULT '[Image]' NOT NULL,
   width smallint(5) unsigned,
   height smallint(5) unsigned,
index f79177d0764994bb79ed0fea8e8d816d0802b823..06bed009b2f62a4b3a326f5c096a31d46c92939a 100644 (file)
@@ -13,7 +13,7 @@ use BSE::Util::ContentType qw(content_type);
 use DevHelp::Date qw(dh_parse_date dh_parse_sql_date);
 use constant MAX_FILE_DISPLAYNAME_LENGTH => 255;
 
-our $VERSION = "1.004";
+our $VERSION = "1.005";
 
 =head1 NAME
 
@@ -3064,33 +3064,32 @@ sub do_add_image {
   $imagename =~ tr/ //d;
   $imagename =~ /([\w.-]+)$/ and $basename = $1;
 
-  # create a filename that we hope is unique
-  my $filename = time. '_'. $basename;
-
   # for the sysopen() constants
   use Fcntl;
 
   my $imagedir = cfg_image_dir($cfg);
-  # loop until we have a unique filename
-  my $counter="";
-  $filename = time. '_' . $counter . '_' . $basename 
-    until sysopen( OUTPUT, "$imagedir/$filename", O_WRONLY| O_CREAT| O_EXCL)
-      || ++$counter > 100;
-
-  fileno(OUTPUT) or die "Could not open image file: $!";
 
+  require DevHelp::FileUpload;
+  my $msg;
+  my ($filename, $fh) =
+    DevHelp::FileUpload->make_img_filename($imagedir, $basename, \$msg);
+  unless ($filename) {
+    $errors->{image} = $msg;
+    return;
+  }
+print STDERR "Gen filename '$filename'\n";
   # for OSs with special text line endings
-  binmode OUTPUT;
+  binmode $fh;
 
   my $buffer;
 
   no strict 'refs';
 
   # read the image in from the browser and output it to our output filehandle
-  print OUTPUT $buffer while read $image, $buffer, 1024;
+  print $fh $buffer while read $image, $buffer, 1024;
 
   # close and flush
-  close OUTPUT
+  close $fh
     or die "Could not close image file $filename: $!";
 
   use Image::Size;
index e567cf7a9282a3cd67175a93b3e24d1f7517eb2e..a58336036b0daa10af0125561c81afc5e7a1263f 100644 (file)
@@ -373,7 +373,7 @@ Index genEmail;1;[genEmail]
 Table image
 Column id;mediumint(8) unsigned;NO;NULL;auto_increment
 Column articleId;int(11);NO;NULL;
-Column image;varchar(64);NO;;
+Column image;varchar(255);NO;;
 Column alt;varchar(255);NO;[Image];
 Column width;smallint(5) unsigned;YES;NULL;
 Column height;smallint(5) unsigned;YES;NULL;