You are here

public function S3fsStreamWrapper::rename in S3 File System 7.2

Same name and namespace in other branches
  1. 7.3 S3fsStreamWrapper.inc \S3fsStreamWrapper::rename()
  2. 7 S3fsStreamWrapper.inc \S3fsStreamWrapper::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 StreamWrapperInterface::rename

See also

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

File

./S3fsStreamWrapper.inc, line 895
Drupal stream wrapper implementation for S3 File System.

Class

S3fsStreamWrapper
The stream wrapper class.

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 (file_uri_scheme($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());
  }
}