You are here

public function AmazonS3StreamWrapper::getExternalUrl in AmazonS3 7

Returns a web accessible URL for the resource.

In the format http://mybucket.amazons3.com/myfile.jpg

Return value

string Returns a string containing a web accessible URL for the resource.

Overrides DrupalStreamWrapperInterface::getExternalUrl

File

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

Class

AmazonS3StreamWrapper
@file Drupal stream wrapper implementation for Amazon S3

Code

public function getExternalUrl() {
  global $is_https;

  // Image styles support
  // Delivers the first request to an image from the private file system
  // otherwise it returns an external URL to an image that has not been
  // created yet.
  $path = explode('/', $this
    ->getLocalPath());
  if ($path[0] === 'styles') {
    if (!$this
      ->_amazons3_get_object($this->uri, $this->caching)) {
      array_shift($path);
      return url('system/files/styles/' . implode('/', $path), array(
        'absolute' => TRUE,
      ));
    }
  }
  $local_path = $this
    ->getLocalPath();
  $info = array(
    'download_type' => 'http',
    'https' => FALSE,
    'presigned_url' => FALSE,
    'presigned_url_timeout' => 60,
    'response' => array(),
  );

  // Allow other modules to change the download link type.
  $info = array_merge($info, module_invoke_all('amazons3_url_info', $local_path, $info));

  // UI overrides.
  // HTTPS.
  foreach ($this->https as $path) {
    if ($path === '*' || preg_match('#' . strtr($path, '#', '\\#') . '#', $local_path)) {
      $info['https'] = TRUE;
      break;
    }
  }

  // Torrent URLs.
  foreach ($this->torrents as $path) {
    if ($path === '*' || preg_match('#' . strtr($path, '#', '\\#') . '#', $local_path)) {
      $info['download_type'] = 'torrent';
      break;
    }
  }

  // Presigned URLs.
  foreach ($this->presignedUrls as $path => $timeout) {
    if ($path === '*' || preg_match('#' . strtr($path, '#', '\\#') . '#', $local_path)) {
      $info['presigned_url'] = TRUE;
      $info['presigned_url_timeout'] = $timeout;
      break;
    }
  }

  // Save as.
  foreach ($this->saveAs as $path) {
    if ($path === '*' || preg_match('#' . strtr($path, '#', '\\#') . '#', $local_path)) {
      $info['response']['content-disposition'] = 'attachment; filename=' . basename($local_path);
      break;
    }
  }
  $timeout = $info['presigned_url'] ? time() + $info['presigned_url_timeout'] : 0;
  $torrent = $info['download_type'] === 'torrent' ? TRUE : FALSE;
  $response = is_array($info['response']) ? $info['response'] : array();

  // Generate the URL.
  $url = $this->domain . '/' . drupal_encode_path($local_path);
  if ($info['presigned_url'] && $this->cloudfront) {
    $url = $this
      ->getCF()
      ->get_private_object_url($this->domain, $local_path, $timeout, array(
      'Secure' => $info['https'],
    ));
  }
  elseif ($info['presigned_url'] || $info['download_type'] !== 'http' || !empty($info['response'])) {
    $url = $this
      ->getS3()
      ->get_object_url($this->bucket, $local_path, $timeout, array(
      'https' => $info['https'],
      'torrent' => $torrent,
      'response' => $response,
    ));
  }
  elseif ($info['https'] || $is_https) {
    $url = 'https://' . $url;
  }
  else {
    $url = 'http://' . $url;
  }
  return $url;
}