File::determineMimeType( string|null $providedMimeType, string|null $extractedMimeType, string|null $pathOrUrl ): WordPressAiClientFilesValueObjectsMimeType

In this article

This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only by core. It is listed here for completeness.

Determines the MIME type from various sources.

Parameters

$providedMimeTypestring|nullrequired
The explicitly provided MIME type.
$extractedMimeTypestring|nullrequired
The MIME type extracted from data URI.
$pathOrUrlstring|nullrequired
The file path or URL to extract extension from.

Return

WordPressAiClientFilesValueObjectsMimeType The determined MIME type.

Source

private function determineMimeType(?string $providedMimeType, ?string $extractedMimeType, ?string $pathOrUrl): MimeType
{
    // Prefer explicitly provided MIME type
    if ($providedMimeType !== null) {
        return new MimeType($providedMimeType);
    }
    // Use extracted MIME type from data URI
    if ($extractedMimeType !== null) {
        return new MimeType($extractedMimeType);
    }
    // Try to determine from file extension
    if ($pathOrUrl !== null) {
        $parsedUrl = parse_url($pathOrUrl);
        $path = $parsedUrl['path'] ?? $pathOrUrl;
        // Remove query string and fragment if present
        $cleanPath = strtok($path, '?#');
        if ($cleanPath === \false) {
            $cleanPath = $path;
        }
        $extension = pathinfo($cleanPath, \PATHINFO_EXTENSION);
        if (!empty($extension)) {
            try {
                return MimeType::fromExtension($extension);
            } catch (InvalidArgumentException $e) {
                // Extension not recognized, continue to error
                unset($e);
            }
        }
    }
    throw new InvalidArgumentException('Unable to determine MIME type. Please provide it explicitly.');
}

Changelog

VersionDescription
0.1.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.