You are here

public function video_amazon_s3::pushFile in Video 6.5

Same name and namespace in other branches
  1. 6.4 plugins/video_s3/video_s3.lib.inc \video_amazon_s3::pushFile()

File

plugins/video_s3/video_s3.lib.inc, line 73
Class file to handle amazon s3 transfers.

Class

video_amazon_s3

Code

public function pushFile(stdClass $file, $keeplocal = FALSE) {
  $expires_offset = variable_get('amazon_s3_expires_offset', 604800);
  $perm = variable_get('amazon_s3_private', FALSE) ? AmazonS3::ACL_PRIVATE : AmazonS3::ACL_PUBLIC;
  $cc = variable_get('amazon_s3_cache_control_max_age', 'none');
  $headers = array();
  if ($expires_offset !== 'none') {
    $headers['Expires'] = gmdate('r', $expires_offset == 0 ? 0 : time() + $expires_offset);
  }
  if ($cc !== 'none') {
    $headers['Cache-Control'] = 'max-age=' . $cc;
  }

  // Increase the database timeout to prevent database errors after a long upload
  _video_db_increase_timeout();

  // The video may be stored in the private file store using an absolute path, so remove the first slash if there is one.
  $dstfilepath = ltrim($file->filepath, '/');
  $response = $this->s3
    ->create_object($this->bucket, $dstfilepath, array(
    'fileUpload' => $file->filepath,
    'acl' => $perm,
    'contentType' => $file->filemime,
    'headers' => $headers,
  ));
  if ($response
    ->isOK()) {

    // If private, set permissions for Zencoder to read
    if ($perm == AmazonS3::ACL_PRIVATE) {
      $this
        ->setZencoderAccessPolicy($this->bucket, $dstfilepath);
    }
    $file->bucket = $this->bucket;
    if (!$keeplocal) {
      file_delete($file->filepath);
    }
    $this
      ->watchdog('Successfully uploaded %file to Amazon S3 location <a href="@s3-url">@s3-path</a> and permission %permission.', array(
      '%file' => $file->filename,
      '@s3-url' => 'http://' . $file->bucket . '.s3.amazonaws.com/' . drupal_urlencode($dstfilepath),
      '@s3-path' => $dstfilepath,
      '%permission' => $perm,
    ), WATCHDOG_INFO, $file);
    return $file;
  }
  $this
    ->watchdog('Failed to upload %file to Amazon S3.', array(
    '%file' => $file->filepath,
  ), WATCHDOG_ERROR, $file);
  return NULL;
}