public function S3fsStream::__construct in S3 File System 4.0.x
Same name and namespace in other branches
- 8.3 src/StreamWrapper/S3fsStream.php \Drupal\s3fs\StreamWrapper\S3fsStream::__construct()
- 8.2 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.
Throws
\Drupal\s3fs\S3fsException Any exception raised.
File
- src/
StreamWrapper/ S3fsStream.php, line 154
Class
- S3fsStream
- Defines a Drupal s3 (s3://) stream wrapper class.
Namespace
Drupal\s3fs\StreamWrapperCode
public function __construct() {
$this->streamWrapperManager = \Drupal::service('stream_wrapper_manager');
// 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('S3fsStream_constructed_settings');
if ($settings !== NULL) {
$this->config = $settings['config'];
$this->cname = $settings['cname'];
$this->torrents = $settings['torrents'];
$this->presignedURLs = $settings['presignedURLs'];
$this->saveas = $settings['saveas'];
$this->s3fs = $settings['s3fs'];
$this->s3 = $settings['s3'];
$this->uploadAsPrivate = $settings['upload_as_private'];
$this
->register($this->s3);
return;
}
$this->s3fs = \Drupal::service('s3fs');
$config = \Drupal::config('s3fs.settings');
foreach ($config
->get() as $prop => $value) {
$this->config[$prop] = $value;
}
$this->s3 = $this
->getClient();
$this
->register($this->s3);
$this->context = stream_context_get_default();
stream_context_set_option($this->context, 's3', 'seekable', TRUE);
// Always use HTTPS when the page is being served via HTTPS, to avoid
// complaints from the browser about insecure content.
$request = \Drupal::request();
$is_https = !empty($request) && $request
->isSecure();
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;
}
// CNAME support for customizing S3 URLs.
// If use_cname is not enabled, file URLs do not use $this->cname.
if (!empty($this->config['use_cname']) && !empty($this->config['domain'])) {
$domain = $this->config['domain'];
if ($domain) {
$domainpart = explode(':', $domain, 2);
if (!empty($domainpart[1])) {
$this->cname['port'] = (int) $domainpart[1];
}
$this->cname['host'] = $domainpart[0];
$this->cname['scheme'] = !empty($this->config['use_https']) ? 'https' : 'http';
}
else {
// Throw an error if the user overrode use_cname without setting a
// domain in settings.php.
throw new S3fsException($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
// [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;
}
}
}
$this->uploadAsPrivate = Settings::get('s3fs.upload_as_private');
// Save all the work we just did, so that subsequent S3fsStreamWrapper
// constructions don't have to repeat it.
$settings['config'] = $this->config;
$settings['cname'] = $this->cname;
$settings['torrents'] = $this->torrents;
$settings['presignedURLs'] = $this->presignedURLs;
$settings['saveas'] = $this->saveas;
$settings['s3fs'] = $this->s3fs;
$settings['s3'] = $this->s3;
$settings['upload_as_private'] = $this->uploadAsPrivate;
}