You are here

public function AmazonS3StreamWrapper::_amazons3_rmdir in AmazonS3 7

Helper function for deleting directories.

The force flag will perform a recursive delete.

1 call to AmazonS3StreamWrapper::_amazons3_rmdir()
AmazonS3StreamWrapper::rmdir in ./AmazonS3StreamWrapper.inc
Support for rmdir().

File

./AmazonS3StreamWrapper.inc, line 827
Drupal stream wrapper implementation for Amazon S3

Class

AmazonS3StreamWrapper
@file Drupal stream wrapper implementation for Amazon S3

Code

public function _amazons3_rmdir($uri, $options, $force = FALSE) {
  $this
    ->assertConstructorCalled();
  $localpath = $this
    ->getLocalPath($uri);
  try {
    $s3 = $this
      ->getS3();
    if ($force) {
      $objects = $s3
        ->get_object_list($this->bucket, array(
        'prefix' => $localpath,
      ));
      if (gettype($objects) === 'array' && !empty($objects)) {
        $or = db_or();
        foreach ($objects as $object) {
          $s3
            ->batch()
            ->delete_object($this->bucket, $object);

          // Delete from cache.
          $object_uri = 's3://' . rtrim($object, '/');
          $or
            ->condition('uri', $object_uri, '=');
        }
        db_delete('amazons3_file')
          ->condition($or)
          ->execute();
        $responses = $s3
          ->batch()
          ->send();
        if ($responses
          ->areOK()) {
          return TRUE;
        }
      }
    }
    else {
      $objects = $s3
        ->get_object_list($this->bucket, array(
        'prefix' => $localpath,
        'max-keys' => 2,
      ));
      if (count($objects) === 1) {
        $response = $this
          ->getS3()
          ->delete_object($this->bucket, $localpath);
        $response2 = $this
          ->getS3()
          ->delete_object($this->bucket, $localpath . '/');
        if ($response
          ->isOK() || $response2
          ->isOK()) {

          // Delete from cache.
          db_delete('amazons3_file')
            ->condition('uri', $uri)
            ->execute();
          return TRUE;
        }
      }
      else {
        if (count($objects) === 0) {
          db_delete('amazons3_file')
            ->condition('uri', $uri)
            ->execute();
          return TRUE;
        }
      }
    }
  } catch (Exception $e) {
    watchdog('amazons3', 'Unable to delete directory @path', array(
      '@path' => $localpath,
    ), WATCHDOG_NOTICE);
  }
  return FALSE;
}