You are here

public function S3fsStream::__construct in S3 File System 8.2

Same name and namespace in other branches
  1. 8.3 src/StreamWrapper/S3fsStream.php \Drupal\s3fs\StreamWrapper\S3fsStream::__construct()
  2. 4.0.x src/StreamWrapper/S3fsStream.php \Drupal\s3fs\StreamWrapper\S3fsStream::__construct()

S3fsStream constructor.

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

1 call to S3fsStream::__construct()
S3fsStream::_assert_constructor_called in src/StreamWrapper/S3fsStream.php
Call the constructor it it hasn't been called yet.

File

src/StreamWrapper/S3fsStream.php, line 124

Class

S3fsStream
Defines a Drupal s3fs (s3fs://) stream wrapper class.

Namespace

Drupal\s3fs\StreamWrapper

Code

public function __construct() {

  // Since S3fsStreamWrapper is always constructed with the same inputs (the
  // file URI is not part of construction), store the constructed settings
  // statically. This is important for performance because Drupal
  // re-constructs stream wrappers very often.
  $settings =& drupal_static('S3fsStream_constructed_settings');
  if ($settings !== NULL) {
    $this->config = $settings['config'];
    $this
      ->getClient();
    $this->domain = $settings['domain'];
    $this->torrents = $settings['torrents'];
    $this->presignedURLs = $settings['presignedURLs'];
    $this->saveas = $settings['saveas'];
    $this->constructed = TRUE;
    return;
  }
  $config = \Drupal::config('s3fs.settings');
  $this
    ->getClient();
  foreach ($config
    ->get() as $prop => $value) {
    $this->config[$prop] = $value;
  }
  if (empty($this->config['bucket'])) {
    $link = Link::fromTextAndUrl($this
      ->t('configuration page'), Url::fromRoute('s3fs.admin_settings'));
    \Drupal::logger('S3 File System')
      ->error('Your AmazonS3 bucket name is not configured. Please visit the @config_page.', [
      '@sconfig_page' => $link
        ->toString(),
    ]);
    throw new \Exception('Your AmazonS3 bucket name is not configured. Please visit the configuration page.');
  }

  // Get the S3 client object.
  $this
    ->getClient();

  // 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 = UrlHelper::filterBadProtocol($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($this
        ->t('The "Use CNAME" option is enabled, but no 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(PHP_EOL, $this->config['presigned_urls']) as $line) {
      $blob = trim($line);
      if ($blob) {
        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(PHP_EOL, $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('S3fsStream constructed.');
}