aboutsummaryrefslogtreecommitdiff
path: root/modules/video_s3/includes/amazon_s3.inc
blob: db25032ef779090337e83e1e0cf137714863e048 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php

/*
 * @file
 * Class file to handle amazon s3 transfers.
 *
 */

// Include our class file.
if (!class_exists('S3'))
  require_once('S3.php');

define('VIDEO_S3_PENDING', 1);
define('VIDEO_S3_ACTIVE', 5);
define('VIDEO_S3_COMPLETE', 10);
define('VIDEO_S3_FAILED', 20);

class video_amazon_s3 {

  private $access_key;
  private $secret_key;
  private $ssl;
  private $limit;
  private $bucket;
  public $s3;

  public function __construct() {
    $this->access_key = variable_get('amazon_s3_access_key', '');
    $this->secret_key = variable_get('amazon_s3_secret_access_key', '');
    $this->ssl = variable_get('amazon_s3_ssl', FALSE);
    $this->limit = variable_get('amazon_s3_limit', 5);
    $this->bucket = variable_get('amazon_s3_bucket', '');
  }

  public function connect($access_key = '', $secret_key = '', $ssl = FALSE) {
    $access_key = $access_key ? $access_key : $this->access_key;
    $secret_key = $secret_key ? $secret_key : $this->secret_key;
    $ssl = $ssl ? $ssl : $this->ssl;
    // Make our connection to Amazon.
    $this->s3 = new S3($access_key, $secret_key, $ssl);
  }

  /*
   * Verifies the existence of a file id, returns the row or false if none found.
   */

  public function verify($fid) {
    $sql = db_query("SELECT * FROM {video_s3} WHERE fid=%d", $fid);
    $row = db_fetch_object($sql);
    return $row;
  }

  /*
   * Gets a video object from the database.
   */

  public function get($fid) {
    $sql = db_query("SELECT * FROM {video_s3} WHERE fid=%d AND status=%d", $fid, VIDEO_S3_COMPLETE);
    $row = db_fetch_object($sql);
    return $row;
  }

  /*
   * Inserts file object into the database.
   */

  public function insert($fid) {
    db_query("INSERT INTO {video_s3} (fid, status) VALUES (%d, %d)", $fid, VIDEO_S3_PENDING);
  }

  /*
   * Updates the database after a successful transfer to amazon.
   */

  public function update($video) {
    $result = db_query("UPDATE {video_s3} SET bucket='%s', filename='%s', filepath='%s', filemime='%s', filesize='%s', status=%d, completed=%d WHERE vid=%d",
            $video->bucket, $video->filename, $video->filepath, $video->filemime, $video->filesize, VIDEO_S3_COMPLETE, time(), $video->vid);
    return $result;
  }

  public function working($vid) {
    db_query("UPDATE {video_s3} SET status=%d WHERE vid=%d", VIDEO_S3_ACTIVE, $vid);
  }

  public function failed($vid) {
    db_query("UPDATE {video_s3} SET status=%d WHERE vid=%d", VIDEO_S3_FAILED, $vid);
  }

  public function delete($fid) {
    // Lets get our file no matter the status and delete it.
    if ($video = $this->verify($fid)) {
      if ($video->bucket) {
        // It has been pushed to amazon so lets remove it.
        $this->s3->deleteObject($video->bucket, $video->filename);
      }
      // Lets delete our record from the database.
      db_query("DELETE FROM {video_s3} WHERE vid=%d", $video->vid);
    }
  }

  /*
   * Selects the pending queue to be transfered to amazon.
   */

  public function queue() {
    $video = false;
    $sql = db_query("SELECT vid, fid FROM {video_s3} WHERE status=%d LIMIT %d", VIDEO_S3_PENDING, $this->limit);
    while ($row = db_fetch_object($sql)) {
      $video = false;
      // We need to check if this file id exists in our transcoding table.
      $sql_video = db_query("SELECT * FROM {video_files} WHERE fid=%d", $row->fid);
      if ($sql_video_row = db_fetch_object($sql_video)) {
        // This is a transcoded file, lets verify it has been transcoded and if so lets push it to amazon.
        module_load_include('inc', 'video', '/includes/conversion');
        if ($sql_video_row->status == VIDEO_RENDERING_COMPLETE) {
          $video = $sql_video_row;
        }
      } else {
        // This is a regular video file, lets get our file object from the files table and push it to amazon.
        $sql_files = db_query("SELECT * FROM {files} WHERE fid=%d", $row->fid);
        if ($sql_files_row = db_fetch_object($sql_files)) {
          $video = $sql_files_row;
        }
      }
      // If we have a video lets go ahead and send it.
      if ($video) {
        // Update our status to working.
        $this->working($row->vid);
        $filepath = $video->filepath;
        // get the folder path as file object
        $filename = $video->filepath;
        // use the file object as file name
        $video->filename = $filename;
        $perm = (variable_get('amazon_s3_private', FALSE) == FALSE) ? S3::ACL_PUBLIC_READ : S3::ACL_PRIVATE;

        if ($this->s3->putObjectFile($filepath, $this->bucket, $filename, $perm)) {
          // Update our table.
          $video->bucket = $this->bucket;
          $video->vid = $row->vid;
          $prefix = $this->ssl ? 'https://' : 'http://';
          $video->filepath = $prefix . $video->bucket . '.s3.amazonaws.com/' . $filename;
          if ($this->update($video)) {
            watchdog('amazon_s3', t('Successfully uploaded our file: !file into the bucket %bucket on the Amazon S3 server.', array('!file' => $filepath, '%bucket' => $this->bucket)), array(), WATCHDOG_INFO);
          }
        } else {
          watchdog('amazon_s3', 'Failed to upload our file to the amazon s3 server.', array(), WATCHDOG_ERROR);
          $this->failed($row->vid);
        }
      } else {
        watchdog('amazon_s3', 'We did not find the file id: ' . $row->fid . ' or it is still queued for ffmpeg processing.', array(), WATCHDOG_DEBUG);
      }
    }
  }

  public function get_object_info($object) {
    return $this->s3->getObjectInfo($this->bucket, $object);
  }

  public function get_authenticated_url($object) {
    $lifetime = variable_get('amazon_s3_lifetime', '1800');
    return $this->s3->getAuthenticatedURL($this->bucket, $object, $lifetime);
  }

  public function get_object($object, $saveTo = false) {
    return $this->s3->getObject($this->bucket, $object, $saveTo);
  }

}