You are here

protected function OEmbed::getLocalThumbnailUri in Drupal 9

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

Returns the local URI for a resource thumbnail.

If the thumbnail is not already locally stored, this method will attempt to download it.

@todo Determine whether or not oEmbed media thumbnails should be stored locally at all, and if so, whether that functionality should be toggle-able. See https://www.drupal.org/project/drupal/issues/2962751 for more information.

Parameters

\Drupal\media\OEmbed\Resource $resource: The oEmbed resource.

Return value

string|null The local thumbnail URI, or NULL if it could not be downloaded, or if the resource has no thumbnail at all.

1 call to OEmbed::getLocalThumbnailUri()
OEmbed::getMetadata in core/modules/media/src/Plugin/media/Source/OEmbed.php
Gets the value for a metadata attribute for a given media item.

File

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

Class

OEmbed
Provides a media source plugin for oEmbed resources.

Namespace

Drupal\media\Plugin\media\Source

Code

protected function getLocalThumbnailUri(Resource $resource) {

  // If there is no remote thumbnail, there's nothing for us to fetch here.
  $remote_thumbnail_url = $resource
    ->getThumbnailUrl();
  if (!$remote_thumbnail_url) {
    return NULL;
  }

  // Ensure that we can write to the local directory where thumbnails are
  // stored.
  $configuration = $this
    ->getConfiguration();
  $directory = $configuration['thumbnails_directory'];

  // The local thumbnail doesn't exist yet, so try to download it. First,
  // ensure that the destination directory is writable, and if it's not,
  // log an error and bail out.
  if (!$this->fileSystem
    ->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
    $this->logger
      ->warning('Could not prepare thumbnail destination directory @dir for oEmbed media.', [
      '@dir' => $directory,
    ]);
    return NULL;
  }

  // The local filename of the thumbnail is always a hash of its remote URL.
  // If a file with that name already exists in the thumbnails directory,
  // regardless of its extension, return its URI.
  $remote_thumbnail_url = $remote_thumbnail_url
    ->toString();
  $hash = Crypt::hashBase64($remote_thumbnail_url);
  $files = $this->fileSystem
    ->scanDirectory($directory, "/^{$hash}\\..*/");
  if (count($files) > 0) {
    return reset($files)->uri;
  }

  // The local thumbnail doesn't exist yet, so we need to download it.
  $local_thumbnail_uri = $directory . DIRECTORY_SEPARATOR . $hash . '.' . $this
    ->getThumbnailFileExtensionFromUrl($remote_thumbnail_url);
  try {
    $response = $this->httpClient
      ->request('GET', $remote_thumbnail_url);
    if ($response
      ->getStatusCode() === 200) {
      $this->fileSystem
        ->saveData((string) $response
        ->getBody(), $local_thumbnail_uri, FileSystemInterface::EXISTS_REPLACE);
      return $local_thumbnail_uri;
    }
  } catch (TransferException $e) {
    $this->logger
      ->warning($e
      ->getMessage());
  } catch (FileException $e) {
    $this->logger
      ->warning('Could not download remote thumbnail from {url}.', [
      'url' => $remote_thumbnail_url,
    ]);
  }
  return NULL;
}