-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathzencoderfile.php
More file actions
203 lines (154 loc) · 5.37 KB
/
Copy pathzencoderfile.php
File metadata and controls
203 lines (154 loc) · 5.37 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
namespace Zencoder\Models;
use Zencoder;
use Laravel\Config;
use Laravel\URL;
class ZencoderFile extends \Laravel\Database\Eloquent\Model
{
protected $request;
protected $encodeWith;
/**
* Run the encoding job for the data in the model
* todo: should we be catching these exceptions? Seems like it might be better to let the exceptions fly so the end "user" can catch them and handle them as needed for their app?
*/
public function run()
{
try{
//todo: catch file error
$this->buildRequest();
//todo: catch connection error
$this->sendRequest();
}catch(\Exception $e)
{
return false;
}
return true;
}
/**
* Build up the request
* todo: validate data
* todo: error checking
*/
private function buildRequest()
{
//New blank request
$request = new \stdClass();
//Set test mode as configured in api config
$request->test = Config::get('zencoder::api.test');
//Check which input format we want to use
$inputToUse = Config::get('zencoder::api.inputs.use');
//Set the baseurl
$this->input_path = Config::get('zencoder::api.inputs.options.' . $inputToUse . '.base_url') . $this->original_filename;
$request->input = $this->input_path;
//Set the region
$request->region = Config::get('zencoder::api.region');
//Check to see if the encoding preset was set at run time, else we'll get the default encoding profile from config
$encodeWith = $this->getEncodingPreset();
if(!$this->encodeWith)
$encodeWith = Config::get('zencoder::api.default_encoding_profile');
$encodingScheme = json_decode(Config::get('zencoder::encoding.schemes.' . $encodeWith));
//todo: this needs to really be figured out a bit more - for now we'll just throw an exception if no format exists.
if(!$encodingScheme->format)
{
throw new \Zencoder\ZencoderConnectionException('No format was specified in your encoding settings (bundles/zencoder/config/encoding.php) so an output filename can not be generated.');
}
//new filename
//todo: need to find out how we should grab the file extension - add a config option?
$this->encoded_filename = md5($this->original_filename . date("Y-m-d H:i:s")) . '.' . $encodingScheme->format;
//Check which input format we want to use
$outputToUse = Config::get('zencoder::api.outputs.use');
//Set the baseurl
$outputBaseUrl = Config::get('zencoder::api.outputs.options.' . $outputToUse . '.base_url');
//todo: do we need to guess at or otherwise set the output_path here?
$encodingScheme->base_url = $outputBaseUrl;
$encodingScheme->filename = $this->encoded_filename;
//Special check for S3 public permissions flag - this only really applies for S3 at this point
if(strtolower($outputToUse) == 's3')
{
$public = Config::get('zencoder::api.outputs.options.s3.public');
if($public)
$encodingScheme->public = 1;
}
$encodingScheme->notifications = array(
array(
"url" => URL::base() . Config::get('zencoder::api.notifications.url'),
"format" => 'json'
)
);
$email = Config::get('zencoder::api.notifications.email');
if($email)
{
$encodingScheme->notifications[] = $email;
}
$thumbnails = (boolean)\Config::get('zencoder::thumbnails.enabled');
if($thumbnails)
{
$thumbnails = new \stdClass();
$prefix = trim(\Config::get('zencoder::thumbnails.prefix'));
if($prefix)
$thumbnails->prefix = $prefix;
$interval = \Config::get('zencoder::thumbnails.interval');
$number = \Config::get('zencoder::thumbnails.number');
if($interval)
$thumbnails->interval = $interval;
else if($number)
$thumbnails->number = $number;
//Check which output format we want to use
$thumbnailOutputToUse = \Config::get('zencoder::thumbnails.output.use');
//Set the baseurl
$thumbnails->base_url = \Config::get('zencoder::thumbnails.output.options.' . $thumbnailOutputToUse . '.base_url');
//Special check for S3 public permissions flag - this only really applies for S3 at this point
if(strtolower($thumbnailOutputToUse) == 's3')
{
$public = \Config::get('zencoder::thumbnails.output.options.s3.public');
if($public)
$thumbnails->public = 1;
}
$encodingScheme->thumbnails = $thumbnails;
}
$request->output = array(
$encodingScheme
);
$this->request = $request;
}
/**
* Send the request via cURL
* todo: validation, error handling, throwing the exceptions created
*/
private function sendRequest()
{
$apiEndpoint = Config::get('zencoder::api.endpoint');
$jsonRequest = json_encode($this->request);
//Do curl request
$ch = curl_init($apiEndpoint);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonRequest);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json',
'Content-Length: ' . strlen($jsonRequest),
'Zencoder-Api-Key: ' . Config::get('zencoder::api.api_key'))
);
$result = curl_exec($ch);
$result = json_decode($result);
if(isset($result->errors))
{
$errorMessage = '';
foreach($result->errors as $error)
{
$errorMessage . $error . ". ";
}
Throw new \Exception($errorMessage);
}
$this->job_id = $result->id;
}
public function setEncodingPreset($encodeWith = 1)
{
$this->encodeWith = $encodeWith;
}
public function getEncodingPreset()
{
return $this->encodeWith;
}
}