function OEmbed::getLocalThumbnailUri

Same name and namespace in other branches
  1. 9 core/modules/media/src/Plugin/media/Source/OEmbed.php \Drupal\media\Plugin\media\Source\OEmbed::getLocalThumbnailUri()
  2. 10 core/modules/media/src/Plugin/media/Source/OEmbed.php \Drupal\media\Plugin\media\Source\OEmbed::getLocalThumbnailUri()
  3. 11.x 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 389

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;
    }
    $remote_thumbnail_url = $remote_thumbnail_url->toString();
    // Remove the query string, since we do not want to include it in the local
    // thumbnail URI.
    $local_thumbnail_url = parse_url($remote_thumbnail_url, PHP_URL_PATH);
    // Compute the local thumbnail URI, regardless of whether or not it exists.
    $configuration = $this->getConfiguration();
    $directory = $configuration['thumbnails_directory'];
    $local_thumbnail_uri = "{$directory}/" . Crypt::hashBase64($local_thumbnail_url) . '.' . pathinfo($local_thumbnail_url, PATHINFO_EXTENSION);
    // If the local thumbnail already exists, return its URI.
    if (file_exists($local_thumbnail_uri)) {
        return $local_thumbnail_uri;
    }
    // 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;
    }
    try {
        $response = $this->httpClient
            ->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 (RequestException $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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.