You are here

protected function OEmbed::getThumbnailFileExtensionFromUrl in Drupal 9

Same name and namespace in other branches
  1. 10 core/modules/media/src/Plugin/media/Source/OEmbed.php \Drupal\media\Plugin\media\Source\OEmbed::getThumbnailFileExtensionFromUrl()

Tries to determine the file extension of a thumbnail.

Parameters

string $thumbnail_url: The remote URL of the thumbnail.

Return value

string|null The file extension, or NULL if it could not be determined.

1 call to OEmbed::getThumbnailFileExtensionFromUrl()
OEmbed::getLocalThumbnailUri in core/modules/media/src/Plugin/media/Source/OEmbed.php
Returns the local URI for a resource thumbnail.

File

core/modules/media/src/Plugin/media/Source/OEmbed.php, line 447

Class

OEmbed
Provides a media source plugin for oEmbed resources.

Namespace

Drupal\media\Plugin\media\Source

Code

protected function getThumbnailFileExtensionFromUrl(string $thumbnail_url) : ?string {

  // First, try to glean the extension from the URL path.
  $path = parse_url($thumbnail_url, PHP_URL_PATH);
  if ($path) {
    $extension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
    if ($extension) {
      return $extension;
    }
  }

  // If the URL didn't give us any clues about the file extension, make a HEAD
  // request to the thumbnail URL and see if the headers will give us a MIME
  // type.
  try {
    $content_type = $this->httpClient
      ->request('HEAD', $thumbnail_url)
      ->getHeader('Content-Type');
  } catch (TransferException $e) {
    $this->logger
      ->warning($e
      ->getMessage());
  }

  // If there was no Content-Type header, there's nothing else we can do.
  if (empty($content_type)) {
    return NULL;
  }
  $extensions = MimeTypes::getDefault()
    ->getExtensions(reset($content_type));
  if ($extensions) {
    return reset($extensions);
  }

  // If no file extension could be determined from the Content-Type header,
  // we're stumped.
  return NULL;
}