|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ApiVideo\Client\Api; |
|
4
|
|
|
|
|
5
|
|
|
use ApiVideo\Client\Buzz\FormByteRangeUpload; |
|
6
|
|
|
use ApiVideo\Client\Buzz\OAuthBrowser; |
|
7
|
|
|
use ApiVideo\Client\Model\Video; |
|
8
|
|
|
use Buzz\Message\Form\FormUpload; |
|
9
|
|
|
use Buzz\Message\RequestInterface; |
|
10
|
|
|
|
|
11
|
|
|
class Videos extends BaseApi |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var int Upload chunk size in bytes */ |
|
14
|
|
|
public $chunkSize; // 64 MiB; |
|
15
|
|
|
|
|
16
|
|
|
/** @var Captions */ |
|
17
|
|
|
public $captions; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(OAuthBrowser $browser) |
|
20
|
|
|
{ |
|
21
|
|
|
parent::__construct($browser); |
|
22
|
|
|
$this->chunkSize = 64 * 1024 * 1024; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param string $videoId |
|
27
|
|
|
* @return Video|null |
|
28
|
|
|
*/ |
|
29
|
|
|
public function get($videoId) |
|
30
|
|
|
{ |
|
31
|
|
|
$response = $this->browser->get("/videos/$videoId"); |
|
32
|
|
|
if (!$response->isSuccessful()) { |
|
33
|
|
|
$this->registerLastError($response); |
|
34
|
|
|
|
|
35
|
|
|
return null; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return $this->unmarshal($response); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Incrementally iterate over a collection of elements. |
|
43
|
|
|
* By default the elements are returned in an array, unless you pass a |
|
44
|
|
|
* $callback which will be called for each instance of Video. |
|
45
|
|
|
* Available parameters: |
|
46
|
|
|
* - currentPage (int) current pagination page |
|
47
|
|
|
* - pageSize (int) number of elements per page |
|
48
|
|
|
* - videoIds (array) videoIds to limit the search to |
|
49
|
|
|
* - tags (array) |
|
50
|
|
|
* - metadata (array) |
|
51
|
|
|
* If currentPage and pageSize are not given, the method iterates over all |
|
52
|
|
|
* pages of results and return an array containing all the results. |
|
53
|
|
|
* |
|
54
|
|
|
* @param array $parameters |
|
55
|
|
|
* @param callable $callback |
|
56
|
|
|
* @return Video[]|null |
|
57
|
|
|
*/ |
|
58
|
|
|
public function search(array $parameters = array(), $callback = null) |
|
59
|
|
|
{ |
|
60
|
|
|
$params = $parameters; |
|
61
|
|
|
$currentPage = isset($parameters['currentPage']) ? $parameters['currentPage'] : 1; |
|
62
|
|
|
$params['pageSize'] = isset($parameters['pageSize']) ? $parameters['pageSize'] : 100; |
|
63
|
|
|
$params['currentPage'] = $currentPage; |
|
64
|
|
|
$allVideos = array(); |
|
65
|
|
|
|
|
66
|
|
|
do { |
|
67
|
|
|
$response = $this->browser->get('/videos?'.http_build_query($params)); |
|
68
|
|
|
|
|
69
|
|
|
if (!$response->isSuccessful()) { |
|
70
|
|
|
$this->registerLastError($response); |
|
71
|
|
|
|
|
72
|
|
|
return null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$json = json_decode($response->getContent(), true); |
|
76
|
|
|
$videos = $json['data']; |
|
77
|
|
|
|
|
78
|
|
|
$allVideos[] = $this->castAll($videos); |
|
79
|
|
|
if (null !== $callback) { |
|
80
|
|
|
foreach (current($allVideos) as $video) { |
|
81
|
|
|
call_user_func($callback, $video); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if (isset($parameters['currentPage'])) { |
|
86
|
|
|
break; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$pagination = $json['pagination']; |
|
90
|
|
|
$pagination['currentPage']++; |
|
91
|
|
|
$params['currentPage'] = $pagination['currentPage']; |
|
92
|
|
|
} while ($pagination['pagesTotal'] > $pagination['currentPage']); |
|
93
|
|
|
$allVideos = call_user_func_array('array_merge', $allVideos); |
|
94
|
|
|
|
|
95
|
|
|
if (null === $callback) { |
|
96
|
|
|
return $allVideos; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return null; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param string $title |
|
104
|
|
|
* @param array $properties |
|
105
|
|
|
* @return Video|null |
|
106
|
|
|
*/ |
|
107
|
|
|
public function create($title, array $properties = array()) |
|
108
|
|
|
{ |
|
109
|
|
|
$response = $this->browser->post( |
|
110
|
|
|
'/videos', |
|
111
|
|
|
array(), |
|
112
|
|
|
json_encode( |
|
113
|
|
|
array_merge( |
|
114
|
|
|
$properties, |
|
115
|
|
|
array('title' => $title) |
|
116
|
|
|
) |
|
117
|
|
|
) |
|
118
|
|
|
); |
|
119
|
|
|
if (!$response->isSuccessful()) { |
|
120
|
|
|
$this->registerLastError($response); |
|
121
|
|
|
|
|
122
|
|
|
return null; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $this->unmarshal($response); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function download($source, $title, array $properties = array()) |
|
129
|
|
|
{ |
|
130
|
|
|
$properties['source'] = $source; |
|
131
|
|
|
|
|
132
|
|
|
return $this->create($title, $properties); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param string $source Path to the file to upload |
|
137
|
|
|
* @param array $properties |
|
138
|
|
|
* @param string $videoId |
|
139
|
|
|
* @return Video|null |
|
140
|
|
|
* @throws \Buzz\Exception\RequestException |
|
141
|
|
|
* @throws \UnexpectedValueException |
|
142
|
|
|
*/ |
|
143
|
|
|
public function upload($source, array $properties = array(), $videoId = null) |
|
144
|
|
|
{ |
|
145
|
|
|
$timeout = $this->browser->getClient()->getTimeout(); |
|
|
|
|
|
|
146
|
|
|
$this->browser->getClient()->setTimeout(0); |
|
|
|
|
|
|
147
|
|
|
if (!is_readable($source)) { |
|
148
|
|
|
throw new \UnexpectedValueException("'$source' must be a readable source file."); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
if (null === $videoId) { |
|
152
|
|
|
if (!isset($properties['title'])) { |
|
153
|
|
|
$properties['title'] = basename($source); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
$videoId = $this->create($properties['title'], $properties)->videoId; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
$resource = fopen($source, 'rb'); |
|
160
|
|
|
|
|
161
|
|
|
$stats = fstat($resource); |
|
|
|
|
|
|
162
|
|
|
$length = $stats['size']; |
|
163
|
|
|
if (0 >= $length) { |
|
164
|
|
|
throw new \UnexpectedValueException("'$source' is empty."); |
|
165
|
|
|
} |
|
166
|
|
|
// Complete upload in a single request when file is small enough |
|
167
|
|
|
if ($this->chunkSize > $length) { |
|
168
|
|
|
$response = $this->browser->submit( |
|
169
|
|
|
"/videos/$videoId/source", |
|
170
|
|
|
array('file' => new FormUpload($source)) |
|
171
|
|
|
); |
|
172
|
|
|
|
|
173
|
|
|
if (!$response->isSuccessful()) { |
|
174
|
|
|
$this->registerLastError($response); |
|
175
|
|
|
|
|
176
|
|
|
return null; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
return $this->unmarshal($response); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
// Split content to upload big files in multiple requests |
|
183
|
|
|
$copiedBytes = 0; |
|
184
|
|
|
stream_set_chunk_size($resource, $this->chunkSize); |
|
|
|
|
|
|
185
|
|
|
$lastResponse = null; |
|
|
|
|
|
|
186
|
|
|
do { |
|
187
|
|
|
$chunkPath = tempnam(sys_get_temp_dir(), 'upload-chunk-'); |
|
188
|
|
|
$chunk = fopen($chunkPath, 'w+b'); |
|
189
|
|
|
$from = $copiedBytes; |
|
190
|
|
|
$copiedBytes += stream_copy_to_stream($resource, $chunk, $this->chunkSize, $copiedBytes); |
|
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
$response = $this->browser->submit( |
|
193
|
|
|
"/videos/$videoId/source", |
|
194
|
|
|
array('file' => new FormByteRangeUpload($chunkPath, $from, $copiedBytes, $length)), |
|
195
|
|
|
RequestInterface::METHOD_POST, |
|
196
|
|
|
array( |
|
197
|
|
|
'Content-Range' => 'bytes '.$from.'-'.($copiedBytes - 1).'/'.$length, |
|
198
|
|
|
) |
|
199
|
|
|
); |
|
200
|
|
|
|
|
201
|
|
|
if ($response->getStatusCode() >= 400) { |
|
202
|
|
|
$this->registerLastError($response); |
|
203
|
|
|
|
|
204
|
|
|
return null; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
$lastResponse = $response; |
|
208
|
|
|
|
|
209
|
|
|
fclose($chunk); |
|
|
|
|
|
|
210
|
|
|
unlink($chunkPath); |
|
211
|
|
|
|
|
212
|
|
|
} while ($copiedBytes < $length); |
|
213
|
|
|
|
|
214
|
|
|
fclose($resource); |
|
215
|
|
|
|
|
216
|
|
|
if (null !== $lastResponse) { |
|
217
|
|
|
$lastResponse = $this->unmarshal($lastResponse); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
$this->browser->getClient()->setTimeout($timeout); |
|
221
|
|
|
|
|
222
|
|
|
return $lastResponse; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* @param string $source Path to the file to upload |
|
227
|
|
|
* @param string $videoId |
|
228
|
|
|
* @return Video|null |
|
229
|
|
|
* @throws \Buzz\Exception\RequestException |
|
230
|
|
|
* @throws \UnexpectedValueException |
|
231
|
|
|
*/ |
|
232
|
|
|
public function uploadThumbnail($source, $videoId) |
|
233
|
|
|
{ |
|
234
|
|
|
if (!is_readable($source)) { |
|
235
|
|
|
throw new \UnexpectedValueException("'$source' must be a readable source file."); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
$resource = fopen($source, 'rb'); |
|
239
|
|
|
|
|
240
|
|
|
$stats = fstat($resource); |
|
|
|
|
|
|
241
|
|
|
$length = $stats['size']; |
|
242
|
|
|
if (0 >= $length) { |
|
243
|
|
|
throw new \UnexpectedValueException("'$source' is empty."); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
$response = $this->browser->submit( |
|
247
|
|
|
"/videos/$videoId/thumbnail", |
|
248
|
|
|
array('file' => new FormUpload($source)) |
|
249
|
|
|
); |
|
250
|
|
|
|
|
251
|
|
|
if (!$response->isSuccessful()) { |
|
252
|
|
|
$this->registerLastError($response); |
|
253
|
|
|
|
|
254
|
|
|
return null; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
return $this->unmarshal($response); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* @param string $videoId |
|
262
|
|
|
* @param array $properties |
|
263
|
|
|
* @return Video|null |
|
264
|
|
|
*/ |
|
265
|
|
|
public function update($videoId, array $properties) |
|
266
|
|
|
{ |
|
267
|
|
|
$response = $this->browser->patch( |
|
268
|
|
|
"/videos/$videoId", |
|
269
|
|
|
array(), |
|
270
|
|
|
json_encode($properties) |
|
271
|
|
|
); |
|
272
|
|
|
|
|
273
|
|
|
if (!$response->isSuccessful()) { |
|
274
|
|
|
$this->registerLastError($response); |
|
275
|
|
|
|
|
276
|
|
|
return null; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
return $this->unmarshal($response); |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* @param string $videoId |
|
284
|
|
|
* @return Video|null |
|
285
|
|
|
*/ |
|
286
|
|
|
public function setPublic($videoId) |
|
287
|
|
|
{ |
|
288
|
|
|
$response = $this->browser->patch( |
|
289
|
|
|
"/videos/$videoId", |
|
290
|
|
|
array(), |
|
291
|
|
|
json_encode(array('public' => true)) |
|
292
|
|
|
); |
|
293
|
|
|
|
|
294
|
|
|
if (!$response->isSuccessful()) { |
|
295
|
|
|
$this->registerLastError($response); |
|
296
|
|
|
|
|
297
|
|
|
return null; |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
return $this->unmarshal($response); |
|
301
|
|
|
} |
|
302
|
|
|
/** |
|
303
|
|
|
* @param string $videoId |
|
304
|
|
|
* @return Video|null |
|
305
|
|
|
*/ |
|
306
|
|
|
public function setPrivate($videoId) |
|
307
|
|
|
{ |
|
308
|
|
|
$response = $this->browser->patch( |
|
309
|
|
|
"/videos/$videoId", |
|
310
|
|
|
array(), |
|
311
|
|
|
json_encode(array('public' => false)) |
|
312
|
|
|
); |
|
313
|
|
|
|
|
314
|
|
|
if (!$response->isSuccessful()) { |
|
315
|
|
|
$this->registerLastError($response); |
|
316
|
|
|
|
|
317
|
|
|
return null; |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
return $this->unmarshal($response); |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* @param string $videoId |
|
325
|
|
|
* @param string $timecode |
|
326
|
|
|
* @return Video|null |
|
327
|
|
|
*/ |
|
328
|
|
|
public function updateThumbnailWithTimeCode($videoId, $timecode) |
|
329
|
|
|
{ |
|
330
|
|
|
if (empty($timecode)) { |
|
331
|
|
|
throw new \UnexpectedValueException('Timecode is empty.'); |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
$response = $this->browser->patch( |
|
335
|
|
|
"/videos/$videoId/thumbnail", |
|
336
|
|
|
array(), |
|
337
|
|
|
json_encode(array('timecode' => $timecode)) |
|
338
|
|
|
); |
|
339
|
|
|
|
|
340
|
|
|
if (!$response->isSuccessful()) { |
|
341
|
|
|
$this->registerLastError($response); |
|
342
|
|
|
|
|
343
|
|
|
return null; |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
return $this->unmarshal($response); |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
/** |
|
350
|
|
|
* @param string $videoId |
|
351
|
|
|
* @return int|null |
|
352
|
|
|
*/ |
|
353
|
|
|
public function delete($videoId) |
|
354
|
|
|
{ |
|
355
|
|
|
$response = $this->browser->delete("/videos/$videoId"); |
|
356
|
|
|
|
|
357
|
|
|
if (!$response->isSuccessful()) { |
|
358
|
|
|
$this->registerLastError($response); |
|
359
|
|
|
|
|
360
|
|
|
return null; |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
return $response->getStatusCode(); |
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
|
|
/** |
|
367
|
|
|
* @param array $data |
|
368
|
|
|
* @return Video |
|
369
|
|
|
* @throws \Exception |
|
370
|
|
|
*/ |
|
371
|
|
|
protected function cast(array $data) |
|
372
|
|
|
{ |
|
373
|
|
|
$video = new Video; |
|
374
|
|
|
$video->videoId = $data['videoId']; |
|
375
|
|
|
$video->title = $data['title']; |
|
376
|
|
|
$video->description = $data['description']; |
|
377
|
|
|
$video->public = $data['public']; |
|
378
|
|
|
$video->tags = $data['tags']; |
|
379
|
|
|
$video->metadata = $data['metadata']; |
|
380
|
|
|
$video->source = $data['source']; |
|
381
|
|
|
$video->assets = $data['assets']; |
|
382
|
|
|
$video->publishedAt = new \DateTimeImmutable($data['publishedAt']); |
|
383
|
|
|
|
|
384
|
|
|
return $video; |
|
385
|
|
|
} |
|
386
|
|
|
} |
|
387
|
|
|
|