You are here

public function StreamWrapper::getExternalUrl in AmazonS3 7.2

File

src/StreamWrapper.php, line 209
Drupal stream wrapper implementation for Amazon S3

Class

StreamWrapper
@file Drupal stream wrapper implementation for Amazon S3

Namespace

Drupal\amazons3

Code

public function getExternalUrl() {
  if (!isset($this->uri)) {
    throw new \LogicException('A URI must be set before calling getExternalUrl().');
  }
  $path_segments = $this->uri
    ->getPathSegments();
  $args = array();

  // 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.
  if (!empty($path_segments) && $path_segments[0] === 'styles' && !file_exists((string) $this->uri)) {
    return $this
      ->url($this::stylesCallback . '/' . $this->uri
      ->getBucket() . $this->uri
      ->getPath(), array(
      'absolute' => TRUE,
    ));
  }

  // UI overrides.
  // Save as.
  $expiry = NULL;
  if ($this
    ->forceDownload()) {
    $args['ResponseContentDisposition'] = $this
      ->getContentDispositionAttachment();
    $expiry = time() + 60 * 60 * 24;
  }

  // Torrent URLs.
  $path = $this
    ->getLocalPath();
  if ($this
    ->useTorrent()) {
    $path .= '?torrent';
  }
  if ($presigned = $this
    ->usePresigned()) {
    $expiry = time() + $presigned
      ->getTimeout();
  }

  // @codeCoverageIgnoreStart
  if ($expiry && $this->config
    ->isCloudFront()) {
    $url = $this
      ->getCloudFrontUrl($path, $expiry);
  }
  else {
    $args['Scheme'] = $this->config
      ->getDomainScheme();

    // Generate a standard URL.
    $url = $this
      ->getS3Url($path, $expiry, $args);
  }
  return (string) $url;
}