You are here

public function S3fsService::getObjectMetadata in S3 File System 4.0.x

Same name and namespace in other branches
  1. 8.3 src/S3fsService.php \Drupal\s3fs\S3fsService::getObjectMetadata()

Cache object meta data.

Parameters

array $file_metadata_list: The list of files.

array $folders: The list of folders.

array $s3_metadata: The individual list object result.

array $config: The S3 bucket configuration.

Overrides S3fsServiceInterface::getObjectMetadata

1 call to S3fsService::getObjectMetadata()
S3fsService::refreshCache in src/S3fsService.php
Refreshes the metadata cache.

File

src/S3fsService.php, line 478

Class

S3fsService
Defines a S3fsService service.

Namespace

Drupal\s3fs

Code

public function getObjectMetadata(array &$file_metadata_list, array &$folders, array $s3_metadata, array $config) {
  $key = $s3_metadata['Key'];

  // The root folder is an implementation detail that only appears on S3.
  // Files' URIs are not aware of it, so we need to remove it beforehand.
  if (!empty($config['root_folder'])) {
    $key = substr_replace($key, '', 0, strlen($config['root_folder']) + 1);
  }

  // Figure out the scheme based on the key's folder prefix.
  $public_folder_name = !empty($config['public_folder']) ? $config['public_folder'] : 's3fs-public';
  $private_folder_name = !empty($config['private_folder']) ? $config['private_folder'] : 's3fs-private';
  if (strpos($key, "{$public_folder_name}/") === 0) {

    // Much like the root folder, the public folder name must be removed
    // from URIs.
    $key = substr_replace($key, '', 0, strlen($public_folder_name) + 1);
    $uri = "public://{$key}";
  }
  elseif (strpos($key, "{$private_folder_name}/") === 0) {
    $key = substr_replace($key, '', 0, strlen($private_folder_name) + 1);
    $uri = "private://{$key}";
  }
  else {

    // No special prefix means it's an s3:// file.
    $uri = "s3://{$key}";
  }
  if (mb_strlen(rtrim($uri, '/')) > S3fsServiceInterface::MAX_URI_LENGTH) {
    return;
  }
  if ($uri[strlen($uri) - 1] == '/') {

    // Treat objects in S3 whose filenames end in a '/' as folders.
    // But don't store the '/' itself as part of the folder's uri.
    $folders[rtrim($uri, '/')] = TRUE;
  }
  else {

    // Only store the metadata for the latest version of the file.
    if (isset($s3_metadata['IsLatest']) && !$s3_metadata['IsLatest']) {
      return;
    }

    // Files with no StorageClass are actually from the DeleteMarkers list,
    // rather then the Versions list. They represent a file which has been
    // deleted, so don't cache them.
    if (!isset($s3_metadata['StorageClass'])) {
      return;
    }

    // Buckets with Versioning disabled set all files' VersionIds to "null".
    // If we see that, unset VersionId to prevent "null" from being written
    // to the DB.
    if (isset($s3_metadata['VersionId']) && $s3_metadata['VersionId'] == 'null') {
      unset($s3_metadata['VersionId']);
    }
    $file_metadata_list[] = $this
      ->convertMetadata($uri, $s3_metadata);
  }
}