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

Handle 'common' web file MIME types #168

Merged
merged 1 commit into from
Jan 17, 2025
Merged
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
20 changes: 14 additions & 6 deletions src/Http/Middleware/ServeStaticAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,20 @@ public function handle(Request $request, Closure $next)
protected function getMimeType(string $file)
{
$mimeType = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $file);
$mimeType = strstr($mimeType, ';', true);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Work on this initially arose due to an issue with this line, the returned MIME type does not look to include a ; and as such returns false. This results in the MIME type always being empty and the 'Content-Type' subsequently being empty in the response.

If the need for this change is not deemed nessary, I would be happy to put together another MR which fixes this issue. We could either:

  • Remove the line, as upon testing I did not noticed any ; (i.e. including the charset) in the MIME type response
  • Appending a ; to the MIME type output so it will always be found and the correct part of the MIME type is returned.


return str_replace(
['image/vnd.microsoft.icon'],
['image/x-icon'],
$mimeType
);
if ($mimeType === 'image/vnd.microsoft.icon') {
return 'image/x-icon';
}

if ($mimeType !== 'text/plain') {
return $mimeType;
}

return match (pathinfo($file, PATHINFO_EXTENSION)) {
'js' => 'text/javascript',
'css' => 'text/css',
'html' => 'text/html',
default => 'text/plain',
};
}
}