You are here

public function S3fsStreamWrapper::stream_flush in S3 File System 7

Same name and namespace in other branches
  1. 7.3 S3fsStreamWrapper.inc \S3fsStreamWrapper::stream_flush()
  2. 7.2 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 (or there was no data to store).

Overrides StreamWrapperInterface::stream_flush

See also

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

File

./S3fsStreamWrapper.inc, line 677
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->params['Key']}.");
  if ($this->mode == 'r') {
    return FALSE;
  }

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

  // All files uploaded to S3 must be set to public-read, or users' browsers
  // will get PermissionDenied errors, and torrent URLs won't work.
  $upload_params['ACL'] = 'public-read';
  $upload_params['ContentType'] = S3fsStreamWrapper::getMimeType($this->uri);

  // Set the Cache-Control header, if the user specified one.
  if ($this->cacheControlHeader) {
    $upload_params['CacheControl'] = $this->cacheControlHeader;
  }

  // Allow other modules to change the upload params.
  drupal_alter('s3fs_upload_params', $upload_params);
  try {
    $this->s3
      ->putObject($upload_params);
    $this->s3
      ->waitUntilObjectExists($this->params);
    $metadata = $this
      ->_get_metadata_from_s3($this->uri);
    if ($metadata === FALSE) {

      // This should never happen, but just in case...
      throw new Exception(t('Uploading the file %file to S3 failed in an unexpected way.', array(
        '%file' => $this->uri,
      )));
    }
    $this
      ->_write_cache($metadata);
    clearstatcache(TRUE, $this->uri);
    return TRUE;
  } catch (\Exception $e) {
    $this
      ->_debug($e
      ->getMessage());
    return $this
      ->_trigger_error($e
      ->getMessage());
  }
}