You are here

public function S3fsStream::rename in S3 File System 4.0.x

Same name and namespace in other branches
  1. 8.3 src/StreamWrapper/S3fsStream.php \Drupal\s3fs\StreamWrapper\S3fsStream::rename()
  2. 8.2 src/StreamWrapper/S3fsStream.php \Drupal\s3fs\StreamWrapper\S3fsStream::rename()

Support for rename().

If $to_uri exists, this file will be overwritten. This behavior is identical to the PHP rename() function.

Parameters

string $from_uri: The uri of the file to be renamed.

string $to_uri: The new uri for the file.

Return value

bool TRUE if file was successfully renamed. Otherwise, FALSE.

Overrides PhpStreamWrapperInterface::rename

See also

http://php.net/manual/en/streamwrapper.rename.php

File

src/StreamWrapper/S3fsStream.php, line 867

Class

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

Namespace

Drupal\s3fs\StreamWrapper

Code

public function rename($from_uri, $to_uri) {
  if ($this
    ->isDir($from_uri)) {

    // AWS SDK doesn't support moving 'directories'.
    return FALSE;
  }
  if (mb_strlen($to_uri) > S3fsServiceInterface::MAX_URI_LENGTH) {
    return FALSE;
  }

  // Set access for new item in stream context.
  if (!$this->uploadAsPrivate && StreamWrapperManager::getScheme($to_uri) !== 'private') {
    stream_context_set_option($this->context, 's3', 'ACL', 'public-read');
  }
  $from_key = $this
    ->convertUriToKeyedPath($from_uri);
  $to_key = $this
    ->convertUriToKeyedPath($to_uri);
  $rename_context = [
    'from_key' => $from_key,
    'to_key' => $to_key,
  ];
  $options = $this
    ->getOptions();

  // Allow other modules to alter the rename params.
  \Drupal::moduleHandler()
    ->alter('s3fs_copy_params', $options[$this->protocol], $rename_context);
  stream_context_set_option($this->context, $options);

  // If parent succeeds in renaming, updated local metadata and cache.
  if (parent::rename($from_key, $to_key)) {
    $metadata = $this
      ->readCache($from_uri);
    $metadata['uri'] = $to_uri;
    $this
      ->writeCache($metadata);
    $this
      ->deleteCache($from_uri);
    clearstatcache(TRUE, $from_uri);
    clearstatcache(TRUE, $to_uri);
    return TRUE;
  }
  return FALSE;
}