function OEmbed::getLocalThumbnailUri

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

\Drupal\media\MediaInterface $media: The media entity that contains the 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 403

Class

OEmbed
Provides a media source plugin for oEmbed resources.

Namespace

Drupal\media\Plugin\media\Source

Code

protected function getLocalThumbnailUri(Resource $resource, MediaInterface $media) {
    $token_data = [
        'date' => $media->getCreatedTime(),
    ];
    // 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;
    }
    // Use the configured directory to store thumbnails. The directory can
    // contain basic (i.e., global) tokens. If any of the replaced tokens
    // contain HTML, the tags will be removed and XML entities will be decoded.
    $configuration = $this->getConfiguration();
    $directory = $configuration['thumbnails_directory'];
    // The thumbnail directory might contain a date token, so we pass in the
    // creation date of the media entity so that the token won't rely on the
    // current request time, making the current request have a max-age of 0.
    // @see system_tokens() for $type == 'date'.
    $directory = $this->token
        ->replace($directory, $token_data);
    $directory = PlainTextOutput::renderFromHtml($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.
    try {
        $response = $this->httpClient
            ->request('GET', $remote_thumbnail_url);
        if ($response->getStatusCode() === 200) {
            $local_thumbnail_uri = $directory . DIRECTORY_SEPARATOR . $hash . '.' . $this->getThumbnailFileExtensionFromUrl($remote_thumbnail_url, $response);
            $this->fileSystem
                ->saveData((string) $response->getBody(), $local_thumbnail_uri, FileExists::Replace);
            return $local_thumbnail_uri;
        }
    } catch (ClientExceptionInterface $e) {
        $this->logger
            ->warning('Failed to download remote thumbnail file due to "%error".', [
            '%error' => $e->getMessage(),
        ]);
    } catch (FileException) {
        $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.