Skip to content

Commit

Permalink
Item12502: using mime-magic as a fallback in case file extensions don…
Browse files Browse the repository at this point in the history
…'t unveil the mime-type

git-svn-id: http://svn.foswiki.org/trunk/XSendFileContrib@16742 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
MichaelDaum authored and MichaelDaum committed May 22, 2013
1 parent 7f8353c commit fd31b79
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
1 change: 1 addition & 0 deletions data/System/XSendFileContrib.txt
Expand Up @@ -113,6 +113,7 @@ One line description, required for extensions repository catalog.
| Dependencies: | %$DEPENDENCIES% |
| Version: | %$VERSION% |
| Change History: | <!-- versions below in reverse order -->&nbsp; |
| 22 May 2013 | using mime-magic as a fallback in case file extensions don't unveil the mime-type |
| 28 Mar 2013 | implemented {<nop>AccessRules} to allow any kind of access control list for attachments |
| 1.00: | Initial version |
| Home: | http://foswiki.org/Extensions/%TOPIC% |
Expand Down
50 changes: 37 additions & 13 deletions lib/Foswiki/Contrib/XSendFileContrib.pm
Expand Up @@ -21,10 +21,13 @@ use warnings;
use Encode ();
use Foswiki::Sandbox ();
use Foswiki::Func ();
use File::MMagic ();

our $VERSION = '2.00';
our $RELEASE = '2.00';
our $VERSION = '2.10';
our $RELEASE = '2.10';
our $SHORTDESCRIPTION = 'A viewfile replacement to send static files efficiently';
our $mimeTypeInfo;
our $mmagic;

sub xsendfile {

Expand Down Expand Up @@ -92,6 +95,13 @@ sub xsendfile {
$fileName = Encode::decode_utf8($fileName);
$fileName = Foswiki::Sandbox::untaint($fileName, \&Foswiki::Sandbox::validateAttachmentName);

# invalid
unless (defined $fileName) {
$response->status(404);
$response->print("404 - file not valid\n");
return;
}

my $topicObject = Foswiki::Meta->new($session, $web, $topic);

# not found
Expand Down Expand Up @@ -122,11 +132,15 @@ sub xsendfile {
# ok
my $headerName = $Foswiki::cfg{XSendFileContrib}{Header} || 'X-LIGHTTPD-send-file';

$fileName = Encode::encode_utf8($fileName);
$filePath = Encode::encode_utf8($filePath);

#print STDERR "fileName=$fileName\n";
#print STDERR "filePath=$filePath\n";

$response->header(
-status => 200,
-type => suffixToMimeType($fileName),
-type => mimeTypeOfFile($filePath),
-content_disposition => "$dispositionMode; filename=\"$fileName\"",
$headerName => $filePath,
);
Expand Down Expand Up @@ -160,22 +174,32 @@ sub checkAccess {
return $topicObject->haveAccess("VIEW", $user);
}

my $types; # cache content of MimeTypesFileName

sub suffixToMimeType {
my ($attachment) = @_;
sub mimeTypeOfFile {
my $fileName = shift;

my $mimeType = 'application/octet-stream';
if ($attachment && $attachment =~ /\.([^.]+)$/) {
if ($fileName && $fileName =~ /\.([^.]+)$/) {
my $suffix = $1;
$types = Foswiki::Func::readFile($Foswiki::cfg{MimeTypesFileName}) unless defined $types;

if ($types =~ /^([^#]\S*).*?\s$suffix(?:\s|$)/im) {
$mimeType = $1;
$mimeTypeInfo = Foswiki::Func::readFile($Foswiki::cfg{MimeTypesFileName})
unless defined $mimeTypeInfo;

if ($mimeTypeInfo =~ /^([^#]\S*).*?\s$suffix(?:\s|$)/im) {
return $1;
}
}

return $mimeType;
$mmagic = File::MMagic->new() unless defined $mmagic;

my $mimeType = $mmagic->checktype_filename($fileName);

if (defined $mimeType && $mimeType ne "x-system/x-error") {
#print STDERR "mmagic says $mimeType to $fileName\n";
return $mimeType;
}

#print STDERR "unknown mime type of $fileName\n";

return 'application/octet-stream';
}

1;
1 change: 1 addition & 0 deletions lib/Foswiki/Contrib/XSendFileContrib/DEPENDENCIES
@@ -1 +1,2 @@
Foswiki::Contrib::FastCGIEngineContrib,>=0.9.5,perl,Optional.
File::MMagic,>0,cpan,Required.

0 comments on commit fd31b79

Please sign in to comment.