WordPress\AiClient\Files\DTO

File::determineMimeTypeprivateWP 0.1.0

Determines the MIME type from various sources.

Method of the class: File{}

No Hooks.

Returns

MimeType. The determined MIME type.

Usage

// private - for code of main (parent) class only
$result = $this->determineMimeType( ?string $providedMimeType, ?string $extractedMimeType, ?string $pathOrUrl ): MimeType;
?string $providedMimeType(required)
.
?string $extractedMimeType(required)
.
?string $pathOrUrl(required)
.

Changelog

Since 0.1.0 Introduced.

File::determineMimeType() code WP 7.0

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.');
}