Fixed invalid detection of MIME type with upper case file extensions. #33

Closed
wants to merge 5 commits into
from

2 participants

@bradydoll

When using the S3->create_object() function with the 'fileUpload' option without manually setting the 'contentType', if the file path passed with this option has an extension with either all caps or mixed case, then the file will always be detected as the 'application/octect-stream' MIME type. This is due to this block of code in the s3.class.php file (line 735):

$extension = explode('.', $opt['fileUpload']);
$extension = array_pop($extension);
$mime_type = CFMimeTypes::get_mimetype($extension);
$headers['Content-Type'] = $mime_type;

The extension is pulled straight from the fileUpload option and passed to the CFMimeTypes::get_mimetype() function.

That function is relatively simple:

public static function get_mimetype($ext)
{
    return (isset(self::$mime_types[$ext]) ? self::$mime_types[$ext] : 'application/octet-stream');
}

Just check if the extension exists in the $mime_types array. However, all of the array keys are lower case so an upper or mixed case extension will never match.

A simple fix, included in my pull request, is to change the function CFMimeTypes::get_mimetype() to:

public static function get_mimetype($ext)
{
    $ext = strtolower( $ext );  // Make sure the passed extension is lowercase
    return (isset(self::$mime_types[$ext]) ? self::$mime_types[$ext] : 'application/octet-stream');
}

This will ensure that the check happens with a lowercase extension and resolves the MIME detection error.

I've also included the files _samples/s3_mime_error.php and _samples/test_files/plain2.TXT (which is just a copy of plain.txt), which demonstrate the issue.

@bradydoll

I wanted to also point out that this can't be resolve by simply doing:

s3->create_object( $bucket, $filename, array( 'fileUpload' => strtolower( $file ) ) );

This would not be the same file on case sensitive file systems (although this would work on Windows).

@jeremeamia

Makes sense, I'll look into it and see if we can get into an upcoming version. We can't accept pull requests directly right now, but we'll make sure you are added to the contributors document.

@jeremeamia

I've made the changes in my local copy and have added you to the CONTRIBUTORS doc. Your changes will appear in the next version of the SDK. Thanks again!

@jeremeamia jeremeamia closed this Sep 20, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment