You are here

public function S3fsStreamWrapper::__construct in S3 File System 7.2

Same name and namespace in other branches
  1. 7.3 S3fsStreamWrapper.inc \S3fsStreamWrapper::__construct()
  2. 7 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.

1 call to S3fsStreamWrapper::__construct()
S3fsStreamWrapper::_assert_constructor_called in ./S3fsStreamWrapper.inc
Call the constructor it it hasn't been called yet.

File

./S3fsStreamWrapper.inc, line 109
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.
  $settings =& drupal_static('S3fsStreamWrapper_constructed_settings');
  if ($settings !== NULL) {
    $this->config = $settings['config'];
    $this->s3 = _s3fs_get_amazons3_client($this->config);
    $this->domain = $settings['domain'];
    $this->torrents = $settings['torrents'];
    $this->presignedURLs = $settings['presignedURLs'];
    $this->saveas = $settings['saveas'];
    $this->constructed = TRUE;
    return;
  }

  // Begin uncached construction.
  $this->config = _s3fs_get_config();
  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);
  }

  // Get the S3 client object.
  $this->s3 = _s3fs_get_amazons3_client($this->config);

  // 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;
  }
  if (!empty($this->config['use_https'])) {
    $scheme = 'https';
    $this
      ->_debug('Using HTTPS.');
  }
  else {
    $scheme = 'http';
    $this
      ->_debug('Using 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;
  $this->constructed = TRUE;
  $this
    ->_debug('S3fsStreamWrapper constructed.');
}