Subject: | wish: file extension support |
Hello,
I'd like it if this module could also figure an appropriate file extension. Here's some code borrowed from Data::FormValidator::Constraints::Upload that does that. It tries to make a 'smart' decision based on the file type that was uploaded, and possible "MIME::Type' alternatives:
# figure out an extension
use MIME::Types;
my $mimetypes = MIME::Types->new;
my MIME::Type $t = $mimetypes->type($mt);
my @mt_exts = $t->extensions;
my ($uploaded_ext) = ($img =~ m/\.([\w\d]*)?$/);
my $ext;
if (scalar @mt_exts) {
# If the upload extension is one recognized by MIME::Type, use it.
if (grep {/^$uploaded_ext$/} @mt_exts) {
$ext = $uploaded_ext;
}
# otherwise, use one from MIME::Type, just to be safe
else {
$ext = $mt_exts[0];
}
}
else {
# If is a provided extension but no MIME::Type extension, use that.
# It's possible that there no extension uploaded or found)
$ext = $uploaded_ext;
}