Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UploadedFile->guessExtenstion() & Config/Mimes::guessExtensionFromType() improvements. #1368

Merged
merged 3 commits into from
Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion application/Config/Mimes.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,22 @@ public static function guessTypeFromExtension(string $extension)
* Attempts to determine the best file extension for a given mime type.
*
* @param string $type
* @param string $proposed_extension - default extension (in case there is more than one with the same mime type)
*
* @return string|null The extension determined, or null if unable to match.
*/
public static function guessExtensionFromType(string $type)
public static function guessExtensionFromType(string $type, ?string $proposed_extension = null)
{

$type = trim(strtolower($type), '. ');

$proposed_extension = trim(strtolower($proposed_extension));

if(!is_null($proposed_extension) && array_key_exists($proposed_extension, self::$mimes) && in_array($type, self::$mimes[$proposed_extension]))
{
return $proposed_extension;
}

foreach (self::$mimes as $ext => $types)
{
if (is_string($types) && $types == $type)
Expand All @@ -324,6 +333,7 @@ public static function guessExtensionFromType(string $type)
return null;
}


//--------------------------------------------------------------------


Expand Down
11 changes: 10 additions & 1 deletion system/HTTP/Files/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,21 @@ public function getTempName(): string
*
* Is simply an alias for guessExtension for a safer method
* than simply relying on the provided extension.
* Additionaly it will return clientExtension in case if there are
* other extensions withe the same mime type.
*/
public function getExtension()
public function getExtension() : string
{
return $this->guessExtension();
}

public function guessExtension(): string
{
return \Config\Mimes::guessExtensionFromType($this->getMimeType(), $this->getClientExtension());
}

//--------------------------------------------------------------------

/**
* Returns the original file extension, based on the file name that
* was uploaded. This is NOT a trusted source.
Expand Down