Skip Menu |

This queue is for tickets about the CGI-Uploader CPAN distribution.

Report information
The Basics
Id: 13562
Status: resolved
Priority: 0/
Queue: CGI-Uploader

People
Owner: MARKSTOS [...] cpan.org
Requestors: william [...] knowmad.com
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: (no value)
Fixed in: (no value)



Subject: Updates to 1.1_1
Hey Mark, Here's my tweaks to the dev release of C::U. It's somewhat extensive but here's the gist: * Add support for SQLite * Add name_scheme support * Add support for parsing uploaded filename * Add get_upload_record() method * Add more error checking around store_file * Do not require the third parameter ($ext) to build_loc * Modifications to cleanup warning messages I've run the tests but many of them fail b/c I'm too tired to setup a DSN. Perhaps you have an environment configured for testing this module and can confirm that all is still working properly. Sorry but I have not added tests for any of the new functionality yet. Let me know what you think. I look forward to making these features part of the stable release as I keep finding more uses for this module. Thanks, William PS: I have written Apache::Test tests which can do upload test verification. Might be something to consider adding to the test suite. I think there are ways to check for A::T being installed or not and skip the tests or allow the user to install and setup A::T. We'd prob need to check that before adding these type of tests to a CPAN module.
--- lib/CGI/Uploader.pm.orig 2005-04-02 13:31:50.000000000 -0500 +++ lib/CGI/Uploader.pm 2005-07-07 03:21:31.000000000 -0400 @@ -12,7 +12,7 @@ require Exporter; use vars qw($VERSION); -$VERSION = '1.1_1'; +$VERSION = '1.1_2'; =head1 NAME @@ -248,13 +248,17 @@ regex => qr/^simple|md5$/, default => 'simple', }, + name_scheme => { + regex => qr/^id|file_name$/, + default => 'id', + }, }); $in{db_driver} = $in{dbh}->{Driver}->{Name}; # Support PostgreSQL via ODBC $in{db_driver} = 'Pg' if $in{dbh}->get_info(17) eq 'PostgreSQL'; - unless (($in{db_driver} eq 'mysql') or ($in{db_driver} eq 'Pg')) { - die "only mysql and Pg drivers are supported at this time. "; + unless (($in{db_driver} eq 'mysql') or ($in{db_driver} eq 'Pg') or ($in{db_driver} eq 'SQLite')) { + die "only mysql and Pg drivers are supported at this time. You are trying to use $in{db_driver}."; } unless ($in{query}) { @@ -655,7 +659,11 @@ uploaded_mt => { type => SCALAR }, file_name => { type => SCALAR | GLOBREF }, shared_meta => { type => HASHREF | UNDEF, default => {} }, - id_to_update => { regex => qr/^\d*$/, optional => 1 }, + # This line is causing a warning to be printed to the Apache error + # log by Params::Validate if id_to_update is not set; looks like + # an error since optional is set - WLM 2005-07-07 + #id_to_update => { regex => qr/^\d*$/, optional => 1 }, + id_to_update => { type => SCALAR | UNDEF, optional => 1 }, }); my ( @@ -679,7 +687,7 @@ my $meta = $self->extract_meta($tmp_filename,$file_name,$uploaded_mt); $shared_meta ||= {}; - my $all_meta = { %$meta, %$shared_meta }; + my $all_meta = { %$meta, %$shared_meta }; my $id; # If it's an update @@ -694,15 +702,14 @@ $all_meta, $id ); - $self->store_file($file_field,$id,$meta->{extension},$tmp_filename); my %ids = $self->create_store_gen_files( - file_field => $file_field, - meta => $all_meta, - src_file => $tmp_filename, - gen_from_id => $id, - ); + file_field => $file_field, + meta => $all_meta, + src_file => $tmp_filename, + gen_from_id => $id, + ) || (); return (%ids, $file_field.'_id' => $id); @@ -863,7 +870,21 @@ my $uploaded_mt = shift || ''; - + # Determine and set the appropriate file system parsing routines for the + # uploaded path name based upon the HTTP client header information. + use HTTP::BrowserDetect; + my $client_os = $^O; + my $browser = HTTP::BrowserDetect->new; + $client_os = 'MSWin32' if $browser->windows; + $client_os = 'MacOS' if $browser->mac; + require File::Basename; + File::Basename::fileparse_set_fstype($client_os); + $file_name = File::Basename::fileparse($file_name,[]); + $file_name =~ s/\s+/_/g; + + ## Extract only the basename of the uploaded file + #require File::Basename; + #my $file_name = fileparse($file_name,[]); require File::MMagic; my $mm = File::MMagic->new; @@ -913,7 +934,7 @@ # Now get the image dimensions if it's an image my ($width,$height) = imgsize($tmp_filename); - return { + return { file_name => $file_name, mime_type => $mt, extension => $ext, @@ -937,7 +958,7 @@ - file field name - - A hashref of key/value pairs to be store. Only the key names defined by the + - A hashref of key/value pairs to be stored. Only the key names defined by the C<up_table_map> in C<new()> will be used. Other values in the hash will be ignored. @@ -999,6 +1020,9 @@ if (!$is_update && $self->{db_driver} eq 'mysql') { $id = $DBH->{'mysql_insertid'}; } + if (!$is_update && $self->{db_driver} eq 'SQLite') { + $id = $DBH->func('last_insert_rowid') + } return $id; } @@ -1125,14 +1149,28 @@ validate_pos(@_,1,1,1,1,1); my $self = shift; my ($file_field,$id,$ext,$tmp_file) = @_; - assert($ext, 'have extension'); - assert($id,'have id'); - assert(-f $tmp_file,'tmp file exists'); + assert($ext, 'have extension'); + assert($id,'have id'); + assert(-f $tmp_file,'tmp file exists'); + assert(-d $self->{updir_path},'updir_path is a directory'); + assert(-w $self->{updir_path},'updir_path is writeable'); - require File::Copy; + require File::Copy; import File::Copy; - copy($tmp_file, File::Spec->catdir($self->{updir_path},$self->build_loc($id,$ext)) ) - || die "Unexpected error occured when uploading the image: $!"; + my $method = $self->{name_scheme}; + if ($method eq 'id') { + my $dest = File::Spec->catdir($self->{updir_path},$self->build_loc($id,$ext)); + copy($tmp_file, $dest ) + || die "Unexpected error occured when copying the image to $dest: $!. Check your permissions."; + } + elsif ($method eq 'file_name') { + my $dest = File::Spec->catdir($self->{updir_path},$self->build_loc($id)); + copy($tmp_file, $dest ) + || die "Unexpected error occured when copying the image to $dest: $!. Check your permissions."; + } + else { + die "Unknown name_scheme - $method"; + } } @@ -1185,15 +1223,18 @@ =cut sub build_loc { - validate_pos(@_,1,1,1); + validate_pos(@_,1,1,0); my ($self,$id,$ext) = @_; my $scheme = $self->{file_scheme}; + my $method = $self->{name_scheme}; my $loc; if ($scheme eq 'simple') { - $loc = "$id$ext"; - } + return "$id$ext" if $method eq 'id'; + my $meta = $self->get_upload_record($id); + return $meta->{file_name}; + } elsif ($scheme eq 'md5') { require Digest::MD5; import Digest::MD5 qw/md5_hex/; @@ -1205,9 +1246,35 @@ mkpath($full_path); } - $loc = "$md5_path/$id$ext"; + return "$md5_path/$id$ext" if $method eq 'id'; + my $meta = $self->get_upload_record($id); + return "$md5_path/" . $meta->{file_name}; } } + + +=head2 get_upload_record() + + my $upload_data = $self->get_file_name($id); + +Returns a hashref of data stored in the uploads database for the requested file id. + +=cut + +sub get_upload_record { + validate_pos(@_,1,1); + my ($self,$id) = @_; + my $DBH = $self->{dbh}; + + my @file_fields = $self->upload_field_names(); + + my $map = $self->{up_table_map}; + my $h = $DBH->selectrow_hashref( + qq!SELECT * FROM ! . $self->{up_table} . + qq! WHERE ( $map->{upload_id} = ! . $DBH->quote($id) . qq! )!); +} + + =head2 upload_field_names() # As a class method
Date: Thu, 7 Jul 2005 09:14:33 -0500
From: Mark Stosberg <mark [...] summersault.com>
To: Guest via RT <bug-CGI-Uploader [...] rt.cpan.org>
Subject: Re: [cpan #13562] Updates to 1.1_1
RT-Send-Cc:
On Thu, Jul 07, 2005 at 03:41:24AM -0400, Guest via RT wrote: Show quoted text
> > This message about CGI-Uploader was sent to you by guest <> via rt.cpan.org > > Full context and any attached attachments can be found at: > <URL: https://rt.cpan.org/Ticket/Display.html?id=13562 > > > Hey Mark, > > Here's my tweaks to the dev release of C::U. It's somewhat extensive but here's the gist: > > * Add support for SQLite > * Add name_scheme support > * Add support for parsing uploaded filename > * Add get_upload_record() method > * Add more error checking around store_file > * Do not require the third parameter ($ext) to build_loc > * Modifications to cleanup warning messages > > I've run the tests but many of them fail b/c I'm too tired to setup a > DSN. Perhaps you have an environment configured for testing this > module and can confirm that all is still working properly. Sorry but I > have not added tests for any of the new functionality yet. Let me know > what you think. I look forward to making these features part of the > stable release as I keep finding more uses for this module.
Thanks for the contribution William. I'd prefer you send a patch with no tests than no patch at all! Show quoted text
> PS: I have written Apache::Test tests which can do upload test > verification. Might be something to consider adding to the test suite. > I think there are ways to check for A::T being installed or not and > skip the tests or allow the user to install and setup A::T. We'd prob > need to check that before adding these type of tests to a CPAN module.
I would very much like to experiment with Apache test, so please send that code along as well. I like the idea of using it conditional on Apache::Test being installed. My original scheme for testing uploads was a SuperHack. CGI::Upload did something a little more elegant, but I never merged it. Mark
[guest - Thu Jul 7 03:41:24 2005]: Show quoted text
> Hey Mark, > > Here's my tweaks to the dev release of C::U. It's somewhat extensive > but here's the gist: > > * Add support for SQLite > * Add name_scheme support > * Add support for parsing uploaded filename > * Add get_upload_record() method > * Add more error checking around store_file > * Do not require the third parameter ($ext) to build_loc > * Modifications to cleanup warning messages > > I've run the tests but many of them fail b/c I'm too tired to setup a > DSN. Perhaps you have an environment configured for testing this > module and can confirm that all is still working properly. Sorry > but I have not added tests for any of the new functionality yet. > Let me know what you think. I look forward to making these
features Show quoted text
> part of the stable release as I keep finding more uses for this > module.
William, Thanks again for these contributions. I'm marking this ticket as resolved now because it's too much to handle through one bug tracking ticket. Some of this improvements made it in, some didn't. Notably: the test suite has been restructured and uses SQLite by default now. It's easier to add new tests and easier to run the tests we have. You may be interested in merging your changes with the 2.1 release which just came out. Besides some bug fixes, I think you'll like the API improvements I made. (Using darcs may make it easier to keep my changes and yours until our trees are sync'ed). Let's chat some more name scheme support either by direct e-mail or maybe on the CGI::App list if they'll let us. Mark