Skip to content

Add support for stream resources in FinfoMimeTypeDetector::detectMimeType() for patching #2

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
53 changes: 48 additions & 5 deletions src/FinfoMimeTypeDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace League\MimeTypeDetection;

use const FILEINFO_MIME_TYPE;

use const PATHINFO_EXTENSION;
use finfo;

Expand Down Expand Up @@ -39,6 +38,11 @@ class FinfoMimeTypeDetector implements MimeTypeDetector, ExtensionLookup
*/
private $inconclusiveMimetypes;

/**
* Buffer size to read from streams if no other bufferSampleSize is defined.
*/
private const STREAM_BUFFER_SAMPLE_SIZE_DEFAULT = 4100;

public function __construct(
string $magicFile = '',
ExtensionToMimeTypeMap $extensionMap = null,
Expand All @@ -53,9 +57,17 @@ public function __construct(

public function detectMimeType(string $path, $contents): ?string
{
$mimeType = is_string($contents)
? (@$this->finfo->buffer($this->takeSample($contents)) ?: null)
: null;
$mimeType = null;
if (is_string($contents)) {
$mimeType = @$this->finfo->buffer($this->takeSample($contents));
} elseif (
is_resource($contents)
&& get_resource_type($contents) === 'stream'
&& ($streamMetaData = stream_get_meta_data($contents))
&& !empty($streamMetaData['seekable'])
) {
$mimeType = @$this->finfo->buffer($this->takeResourceSample($contents));
}

if ($mimeType !== null && ! in_array($mimeType, $this->inconclusiveMimetypes)) {
return $mimeType;
Expand Down Expand Up @@ -90,6 +102,37 @@ private function takeSample(string $contents): string
return (string) substr($contents, 0, $this->bufferSampleSize);
}

/**
* Fetches a sample of a resource while maintaining its pointer.
*/
private function takeResourceSample($contents): string
{
if (is_resource($contents) && get_resource_type($contents) === 'stream') {
// Memory optimization: given a length stream_get_contents()
// immediately allocates an internal buffer.
// However, stream_copy_to_stream() reads up to the defined length
// without pre-allocating any extra buffer.
// Given the relatively large STREAM_BUFFER_SAMPLE_SIZE_DEFAULT this
// avoids unnecessary memory hogging.
$streamContentBuffer = fopen('php://temp/maxmemory:' . self::STREAM_BUFFER_SAMPLE_SIZE_DEFAULT, 'w+b');

$streamPosition = ftell($contents);
rewind($contents);
stream_copy_to_stream(
$contents,
$streamContentBuffer,
$this->bufferSampleSize ?? self::STREAM_BUFFER_SAMPLE_SIZE_DEFAULT,
0
);
rewind($streamContentBuffer);
$streamSample = stream_get_contents($streamContentBuffer);
fclose($streamContentBuffer);
fseek($contents, $streamPosition);
return $streamSample;
}
return '';
}

public function lookupExtension(string $mimetype): ?string
{
return $this->extensionMap instanceof ExtensionLookup
Expand All @@ -103,4 +146,4 @@ public function lookupAllExtensions(string $mimetype): array
? $this->extensionMap->lookupAllExtensions($mimetype)
: [];
}
}
}