WordPress\AiClient\Files\DTO

File::determineMimeTypeprivateWP 0.1.0

Determines the MIME type from various sources.

Метод класса: File{}

Хуков нет.

Возвращает

MimeType. The determined MIME type.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->determineMimeType( ?string $providedMimeType, ?string $extractedMimeType, ?string $pathOrUrl ): MimeType;
?string $providedMimeType(обязательный)
.
?string $extractedMimeType(обязательный)
.
?string $pathOrUrl(обязательный)
.

Список изменений

С версии 0.1.0 Введена.

Код File::determineMimeType() 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.');
}