You are here

function amazons3_file_entity_upload_destination_uri_alter in AmazonS3 7.2

Implements hook_file_entity_upload_destination_uri_alter().

File

./amazons3.module, line 482
Hook implementations for the AmazonS3 module.

Code

function amazons3_file_entity_upload_destination_uri_alter(&$result, array $params = array(), array $data = array()) {
  if ($params['uri_scheme'] == 's3') {
    try {
      $url = S3Url::factory($result);
      $bucket = $url
        ->getBucket();
      $s3 = S3Client::factory(array(), $bucket);
      S3Client::validateBucketExists($bucket, $s3, new \Drupal\amazons3\Cache());
    } catch (\InvalidArgumentException $e) {

      // We couldn't parse the URL, so check to see if it is bare.
      if ($result == 's3://') {
        $config = StreamWrapperConfiguration::fromDrupalVariables();
        $bucket = $config
          ->getBucket();
      }
      else {
        throw $e;
      }
    } catch (S3ConnectValidationException $e) {
      if (!empty($params['field']) && ($field = field_info_field($params['field']))) {
        $bucket = $field['settings']['amazons3_bucket'];
      }
      else {
        $config = StreamWrapperConfiguration::fromDrupalVariables();
        $bucket = $config
          ->getBucket();
      }
    }
    $result = amazons3_upload_location('s3', $bucket, $params['file_directory'], $data);
  }
}