You are here

public function S3fsStreamWrapper::stream_flush in S3 File System 7.2

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

See also

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

File

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

Class

S3fsStreamWrapper
The stream wrapper class.

Code

public function stream_flush() {
  $this
    ->_debug("stream_flush() called for {$this->uri}.");
  if ($this->access_mode == 'r') {
    return FALSE;
  }

  // Prep the upload parameters.
  $this->body
    ->rewind();
  $upload_params = $this->params;
  $upload_params['Body'] = $this->body;
  $upload_params['ContentType'] = S3fsStreamWrapper::getMimeType($this->uri);

  // 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. The one exception to
  // this is when all content is being routed through an edge service and access via S3
  // should be blocked.
  if (!empty($this->config['use_cname']) && !empty($this->config['domain']) && !empty($this->config['domain_s3_private'])) {
    $upload_params['ACL'] = 'private';
  }
  elseif (file_uri_scheme($this->uri) != 'private') {
    $upload_params['ACL'] = 'public-read';
  }

  // Set the Cache-Control header, if the user specified one.
  if (!empty($this->config['cache_control_header'])) {
    $upload_params['CacheControl'] = $this->config['cache_control_header'];
  }
  if (!empty($this->config['encryption'])) {
    $upload_params['ServerSideEncryption'] = $this->config['encryption'];
  }

  // Allow other modules to alter the upload params.
  drupal_alter('s3fs_upload_params', $upload_params);
  try {
    $this->s3
      ->putObject($upload_params);
    $this
      ->writeUriToCache($this->uri);
  } catch (\Exception $e) {
    $this
      ->_debug($e
      ->getMessage());
    return $this
      ->_trigger_error($e
      ->getMessage());
  }
  return TRUE;
}