You are here

protected function S3FileOriginLocator::getRemoteFile in Acquia Content Hub 8.2

Returns the remote file by the uri if there is one, NULL otherwise.

Parameters

string $uri: The uri to identify the file.

Return value

object|null An object with the following fields:

Throws

\Exception

See also

\Drupal\acquia_contenthub_s3\S3FileMap::getFileByUuid()

1 call to S3FileOriginLocator::getRemoteFile()
S3FileOriginLocator::getS3FileSource in modules/acquia_contenthub_s3/src/S3FileOriginLocator.php
Returns the source of the s3 file.

File

modules/acquia_contenthub_s3/src/S3FileOriginLocator.php, line 115

Class

S3FileOriginLocator
Responsible for locating the s3 source of the given file url.

Namespace

Drupal\acquia_contenthub_s3

Code

protected function getRemoteFile(string $uri) : ?\stdClass {
  $files = $this->fileEntityStorage
    ->loadByProperties([
    'uri' => $uri,
  ]);
  if (!$files) {
    return NULL;
  }

  // Use the tracker to avoid unnecessary requests to Content Hub.
  $container = \Drupal::getContainer();
  $tracker = $container
    ->has('acquia_contenthub_publisher.tracker') ? $container
    ->get('acquia_contenthub_publisher.tracker') : NULL;

  // Due to the current implementation of s3fs it is possible that a site
  // stores multiple file entities with the same uri. We pick and store the
  // one that was actually exported and can be located in ContentHub.
  foreach ($files as $file) {
    if (!is_null($tracker)) {

      // Do not initiate entity retrieval since it is already in the export
      // table.
      $tracked_file = $tracker
        ->get($file
        ->uuid());
      if ($tracked_file) {
        continue;
      }
    }
    $remote_file = $this->commonActions
      ->getRemoteEntity($file
      ->uuid());
    if ($remote_file) {
      $this->s3FileMap
        ->record($remote_file
        ->getUuid(), $this
        ->getAttributeValue($remote_file, 'ach_s3_bucket'), $this
        ->getAttributeValue($remote_file, 'ach_s3_source'), $remote_file
        ->getOrigin());
      return $this->s3FileMap
        ->getFileByUuid($remote_file
        ->getUuid());
    }
  }
  return NULL;
}