You are here

public function S3fsStreamWrapper::__construct in S3 File System 7.3

Same name and namespace in other branches
  1. 7 S3fsStreamWrapper.inc \S3fsStreamWrapper::__construct()
  2. 7.2 S3fsStreamWrapper.inc \S3fsStreamWrapper::__construct()

Stream wrapper constructor.

Creates the Aws\S3\S3Client client object and activates the options specified on the S3 File System Settings page.

File

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

Class

S3fsStreamWrapper
The stream wrapper class.

Code

public function __construct() {

  // Since S3fsStreamWrapper is always constructed with the same inputs (the
  // file uri is not part of construction), we store the constructed settings
  // statically. This is important for performance because the way Drupal's
  // APIs are used causes stream wrappers to be frequently re-constructed.
  // Get the S3 client object and register the stream wrapper again so it is
  // configured as needed.
  $settings =& drupal_static('S3fsStreamWrapper_constructed_settings');
  if ($settings !== NULL) {
    $this->config = $settings['config'];
    $this->domain = $settings['domain'];
    $this->torrents = $settings['torrents'];
    $this->presignedURLs = $settings['presignedURLs'];
    $this->saveas = $settings['saveas'];
    $this->s3 = _s3fs_get_amazons3_client($this->config);
    $this
      ->register($this->s3);
    return;
  }

  // Begin construction if not cached.
  $this->config = _s3fs_get_config();
  $this->s3 = _s3fs_get_amazons3_client($this->config);
  $this
    ->register($this->s3);
  $this->context = stream_context_get_default();
  stream_context_set_option($this->context, 's3', 'seekable', TRUE);

  // Check if bucket is configured.
  if (empty($this->config['bucket'])) {
    $msg = t('Your AmazonS3 bucket name is not configured. Please visit the !settings_page.', array(
      '!settings_page' => l(t('configuration page'), '/admin/config/media/s3fs/settings'),
    ));
    watchdog('S3 File System', $msg, array(), WATCHDOG_ERROR);
    throw new Exception($msg);
  }

  // Always use HTTPS when the page is being served via HTTPS, to avoid
  // complaints from the browser about insecure content.
  global $is_https;
  if ($is_https) {

    // We change the config itself, rather than simply using $is_https in
    // the following if condition, because $this->config['use_https'] gets
    // used again later.
    $this->config['use_https'] = TRUE;
  }
  $scheme = !empty($this->config['use_https']) ? 'https' : 'http';

  // CNAME support for customizing S3 URLs.
  // If use_cname is not enabled, file URLs do not use $this->domain.
  if (!empty($this->config['use_cname']) && !empty($this->config['domain'])) {
    $domain = check_url($this->config['domain']);
    if ($domain) {

      // If domain is set to a root-relative path, add the hostname back in.
      if (strpos($domain, '/') === 0) {
        $domain = $_SERVER['HTTP_HOST'] . $domain;
      }
      $this->domain = "{$scheme}://{$domain}";
    }
    else {

      // Due to the config form's validation, this shouldn't ever happen.
      throw new Exception(t('The "Use a CNAME" option is enabled, but no CDN Domain Name has been set.'));
    }
  }

  // Convert the torrents string to an array.
  if (!empty($this->config['torrents'])) {
    foreach (explode("\n", $this->config['torrents']) as $line) {
      $blob = trim($line);
      if ($blob) {
        $this->torrents[] = $blob;
      }
    }
  }

  // Convert the presigned URLs string to an associative array like
  // array(blob => timeout).
  if (!empty($this->config['presigned_urls'])) {
    foreach (explode("\n", $this->config['presigned_urls']) as $line) {
      $blob = trim($line);
      if ($blob) {
        $matches = array();
        if (preg_match('/(.*)\\|(.*)/', $blob, $matches)) {
          $blob = $matches[2];
          $timeout = $matches[1];
          $this->presignedURLs[$blob] = $timeout;
        }
        else {
          $this->presignedURLs[$blob] = 60;
        }
      }
    }
  }

  // Convert the forced save-as string to an array.
  if (!empty($this->config['saveas'])) {
    foreach (explode("\n", $this->config['saveas']) as $line) {
      $blob = trim($line);
      if ($blob) {
        $this->saveas[] = $blob;
      }
    }
  }

  // Save all the work we just did, so that subsequent S3fsStreamWrapper
  // constructions don't have to repeat it.
  $settings['config'] = $this->config;
  $settings['domain'] = $this->domain;
  $settings['torrents'] = $this->torrents;
  $settings['presignedURLs'] = $this->presignedURLs;
  $settings['saveas'] = $this->saveas;
}