You are here

public function AmazonS3StreamWrapper::stream_flush in AmazonS3 7

Support for fflush(). Flush current cached stream data to storage.

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

./AmazonS3StreamWrapper.inc, line 606
Drupal stream wrapper implementation for Amazon S3

Class

AmazonS3StreamWrapper
@file Drupal stream wrapper implementation for Amazon S3

Code

public function stream_flush() {
  if ($this->write_buffer) {
    try {

      // Make sure the S3 class is loaded.
      $this
        ->getS3();

      // Set the storage type.
      $s3_storage_type = AmazonS3::STORAGE_STANDARD;
      $local_path = $this
        ->getLocalPath();
      foreach ($this->rrs as $path) {
        if (preg_match('#' . strtr($path, '#', '\\#') . '#', $local_path)) {
          $s3_storage_type = AmazonS3::STORAGE_REDUCED;
          break;
        }
      }

      // Set the metadata.
      $headers = array();
      $headers = array_merge($headers, module_invoke_all('amazons3_save_headers', $local_path, $headers));

      // Save the object.
      $response = $this
        ->getS3()
        ->create_object($this->bucket, $local_path, array(
        'body' => $this->buffer,
        'acl' => AmazonS3::ACL_PUBLIC,
        'contentType' => AmazonS3StreamWrapper::getMimeType($this->uri),
        'storage' => $s3_storage_type,
        'headers' => $headers,
      ));
      if ($response
        ->isOK()) {

        // Delete cache entry.
        db_delete('amazons3_file')
          ->condition('uri', $this->uri)
          ->execute();
        return TRUE;
      }
    } catch (Exception $e) {
      watchdog('amazons3', 'Unable to write file @path', array(
        '@path' => $local_path,
      ), WATCHDOG_NOTICE);
    }
  }
  $this
    ->clearBuffer();
  return FALSE;
}