You are here

public function S3fsStream::stream_flush in S3 File System 8.2

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

Support for fflush(). Flush current cached stream data to a file in S3.

Return value

bool TRUE if data was successfully stored in S3.

Overrides PhpStreamWrapperInterface::stream_flush

See also

http://php.net/manual/en/streamwrapper.stream-flush.php

File

src/StreamWrapper/S3fsStream.php, line 667

Class

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

Namespace

Drupal\s3fs\StreamWrapper

Code

public function stream_flush() {
  $this
    ->_debug("stream_flush() called for {$this->uri}.");
  if ($this->access_mode == 'r') {
    return FALSE;
  }
  if ($this->body
    ->isSeekable()) {
    $this->body
      ->seek(0);
  }
  $params = $this->params;
  $params['Body'] = $this->body;
  $params['ContentType'] = \Drupal::service('file.mime_type.guesser')
    ->guess($params['Key']);
  if (!empty($this->config['saveas'])) {
    foreach (explode(PHP_EOL, $this->config['saveas']) as $line) {
      $blob = trim($line);
      if ($blob && preg_match("^{$blob}^", $this->uri)) {
        $params['ContentType'] = 'application/zip';
      }
    }
  }
  if (\Drupal::service('file_system')
    ->uriScheme($this->uri) != 'private') {

    // All non-private files uploaded to S3 must be set to public-read, or users' browsers
    // will get PermissionDenied errors, and torrent URLs won't work.
    $params['ACL'] = 'public-read';
  }

  // Set the Cache-Control header, if the user specified one.
  if (!empty($this->config['cache_control_header'])) {
    $params['CacheControl'] = $this->config['cache_control_header'];
  }
  if (!empty($this->config['encryption'])) {
    $params['ServerSideEncryption'] = $this->config['encryption'];
  }
  try {
    $this->s3
      ->putObject($params);
    $this
      ->writeUriToCache($this->uri);
  } catch (\Exception $e) {
    $this
      ->_debug($e
      ->getMessage());
    return $this
      ->_trigger_error($e
      ->getMessage());
  }
  return TRUE;
}