Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Mime] Cache finfo objects to reduce open file handles and optimize perf #58233

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

Merged
merged 1 commit into from
Sep 13, 2024
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
14 changes: 11 additions & 3 deletions 14 src/Symfony/Component/Mime/FileinfoMimeTypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Mime\Exception\InvalidArgumentException;
use Symfony\Component\Mime\Exception\LogicException;
use Symfony\Component\Mime\Exception\RuntimeException;

/**
* Guesses the MIME type using the PECL extension FileInfo.
Expand All @@ -21,6 +22,11 @@
*/
class FileinfoMimeTypeGuesser implements MimeTypeGuesserInterface
{
/**
* @var array<string, \finfo>
*/
private static $finfoCache = [];
nicolas-grekas marked this conversation as resolved.
Show resolved Hide resolved

/**
* @param string|null $magicFile A magic file to use with the finfo instance
*
Expand All @@ -46,10 +52,12 @@ public function guessMimeType(string $path): ?string
throw new LogicException(\sprintf('The "%s" guesser is not supported.', __CLASS__));
}

if (false === $finfo = new \finfo(\FILEINFO_MIME_TYPE, $this->magicFile)) {
return null;
try {
$finfo = self::$finfoCache[$this->magicFile] ??= new \finfo(\FILEINFO_MIME_TYPE, $this->magicFile);
} catch (\Exception $e) {
throw new RuntimeException($e->getMessage());
}
$mimeType = $finfo->file($path);
$mimeType = $finfo->file($path) ?: null;

if ($mimeType && 0 === (\strlen($mimeType) % 2)) {
$mimeStart = substr($mimeType, 0, \strlen($mimeType) >> 1);
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.