Skip to content

Commit

Permalink
Merge pull request #32 from tattersoftware/extension
Browse files Browse the repository at this point in the history
Improve extension handling
  • Loading branch information
MGatner authored Jun 4, 2021
2 parents ea8816f + 4af6dfa commit d5bc515
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
21 changes: 12 additions & 9 deletions src/Entities/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,24 @@ public function getPath(): string
*/
public function getExtension($method = ''): string
{
if (! $method || $method === 'type')
if ($this->attributes['type'] !== 'application/octet-stream')
{
if ($extension = Mimes::guessExtensionFromType($this->attributes['type']))
if (! $method || $method === 'type')
{
return $extension;
if ($extension = Mimes::guessExtensionFromType($this->attributes['type']))
{
return $extension;
}
}
}

if (! $method || $method === 'mime')
{
if ($file = $this->getObject())
if (! $method || $method === 'mime')
{
if ($extension = $file->guessExtension())
if ($file = $this->getObject())
{
return $extension;
if ($extension = $file->guessExtension())
{
return $extension;
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Models/FileModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use CodeIgniter\Files\File as CIFile;
use CodeIgniter\Model;
use Config\Mimes;
use Tatter\Files\Entities\File;
use Tatter\Files\Exceptions\FilesException;
use Tatter\Thumbnails\Exceptions\ThumbnailsException;
Expand Down Expand Up @@ -151,7 +152,7 @@ public function createFromFile(CIFile $file, array $data = []): File
'filename' => $file->getFilename(),
'localname' => $file->getRandomName(),
'clientname' => $file->getFilename(),
'type' => $file->getMimeType(),
'type' => Mimes::guessTypeFromExtension($file->getExtension()) ?? $file->getMimeType(),
'size' => $file->getSize(),
];

Expand Down

0 comments on commit d5bc515

Please sign in to comment.