You are here

protected function S3fsStream::_write_cache in S3 File System 8.2

Write an object's (and its ancestor folders') metadata to the cache.

Parameters

array $metadata: An associative array of file metadata in this format: 'uri' => The full URI of the file, including the scheme. 'filesize' => The size of the file, in bytes. 'timestamp' => The file's create/update timestamp. 'dir' => A boolean indicating whether the object is a directory.

Throws

Exceptions which occur in the database call will percolate.

3 calls to S3fsStream::_write_cache()
S3fsStream::mkdir in src/StreamWrapper/S3fsStream.php
Support for mkdir().
S3fsStream::rename in src/StreamWrapper/S3fsStream.php
Support for rename().
S3fsStream::writeUriToCache in src/StreamWrapper/S3fsStream.php
Write the file at the given URI into the metadata cache.

File

src/StreamWrapper/S3fsStream.php, line 1288

Class

S3fsStream
Defines a Drupal s3fs (s3fs://) stream wrapper class.

Namespace

Drupal\s3fs\StreamWrapper

Code

protected function _write_cache($metadata) {
  $this
    ->_debug("_write_cache({$metadata['uri']}) called.", TRUE);

  // Since public:///blah.jpg and public://blah.jpg refer to the same file
  // (a file named blah.jpg at the root of the file system), we'll sometimes
  // receive files with a /// in their URI. This messes with our caching
  // scheme, though, so we need to remove the extra /.

  //@todo: Work this out if needed

  /*if (strpos($metadata['uri'], 'public:///') === 0) {
      $metadata['uri'] = preg_replace('^public://[/]+^', 'public://', $metadata['uri']);
    }
    else if (strpos($metadata['uri'], 'private:///') === 0) {
      $metadata['uri'] = preg_replace('^private://[/]+^', 'private://', $metadata['uri']);
    }*/
  \Drupal::database()
    ->merge('s3fs_file')
    ->key([
    'uri' => $metadata['uri'],
  ])
    ->fields($metadata)
    ->execute();

  // Clear this URI from the Drupal cache, to ensure the next read isn't
  // from a stale cache entry.
  //    $cid = S3FS_CACHE_PREFIX . $metadata['uri'];
  //    $cache = \Drupal::cache('S3FS_CACHE_BIN');
  //    $cache->delete($cid);
  $dirname = \Drupal::service('file_system')
    ->dirname($metadata['uri']);

  // If this file isn't in the root directory, also write this file's
  // ancestor folders to the cache.
  if (file_uri_target($dirname) != '') {
    $this
      ->mkdir($dirname, NULL, STREAM_MKDIR_RECURSIVE);
  }
}