You are here

public function S3fsStream::rename in S3 File System 8.2

Same name and namespace in other branches
  1. 8.3 src/StreamWrapper/S3fsStream.php \Drupal\s3fs\StreamWrapper\S3fsStream::rename()
  2. 4.0.x 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 859

Class

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

Namespace

Drupal\s3fs\StreamWrapper

Code

public function rename($from_uri, $to_uri) {
  $this
    ->_assert_constructor_called();
  $this
    ->_debug("rename({$from_uri}, {$to_uri}) called.");
  $from_params = $this
    ->_get_params($from_uri);
  $to_params = $this
    ->_get_params($to_uri);
  clearstatcache(TRUE, $from_uri);
  clearstatcache(TRUE, $to_uri);

  // Add the copyObject() parameters.
  $to_params['CopySource'] = "/{$from_params['Bucket']}/" . rawurlencode($from_params['Key']);
  $to_params['MetadataDirective'] = 'COPY';
  if (\Drupal::service('file_system')
    ->uriScheme($from_uri) != 'private') {
    $to_params['ACL'] = 'public-read';
  }
  try {

    // Copy the original object to the specified destination.
    $this->s3
      ->copyObject($to_params);

    // Copy the original object's metadata.
    $metadata = $this
      ->_read_cache($from_uri);
    $metadata['uri'] = $to_uri;
    $this
      ->_write_cache($metadata);
    $this
      ->waitUntilFileExists($to_uri);

    // Now that we know the new object is there, delete the old one.
    return $this
      ->unlink($from_uri);
  } catch (\Exception $e) {
    $this
      ->_debug($e
      ->getMessage());
    return $this
      ->_trigger_error($e
      ->getMessage());
  }
}