1 | <?php |
||
16 | class YouTubeService extends BaseApplicationComponent |
||
17 | { |
||
18 | /** |
||
19 | * Holds the OAuth client. |
||
20 | * |
||
21 | * @var \Google_Client|null |
||
22 | */ |
||
23 | protected $client; |
||
24 | |||
25 | /** |
||
26 | * Holds the YouTube API. |
||
27 | * |
||
28 | * @var \Google_Service_YouTube|null |
||
29 | */ |
||
30 | protected $youtube; |
||
31 | |||
32 | /** |
||
33 | * Holds asset existence checks. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $exists = array(); |
||
38 | |||
39 | /** |
||
40 | * Holds cached asset locations. |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $assets = array(); |
||
45 | |||
46 | /** |
||
47 | * Holds cached file hashes. |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $hashes = array(); |
||
52 | |||
53 | /** |
||
54 | * Upload and process the result. |
||
55 | * |
||
56 | * @param BaseElementModel $element |
||
57 | * @param AssetFileModel $asset |
||
58 | * @param string $handle |
||
59 | * |
||
60 | * @return bool |
||
61 | */ |
||
62 | 6 | public function process(BaseElementModel $element, AssetFileModel $asset, $handle) |
|
63 | { |
||
64 | // Check if we have this asset already or upload to YouTube |
||
65 | 6 | if (!($youTubeId = $this->exists($asset))) { |
|
66 | try { |
||
67 | 6 | $youTubeId = $this->assemble($asset); |
|
68 | 5 | } catch (Exception $e) { |
|
69 | 5 | return $e->getMessage(); |
|
70 | } |
||
71 | } |
||
72 | |||
73 | // Get current video's |
||
74 | 1 | $content = $element->getContent()->getAttribute($handle); |
|
75 | |||
76 | // Make sure content's an array |
||
77 | 1 | $content = is_array($content) ? $content : array(); |
|
78 | |||
79 | // Remove this asset's id from the content |
||
80 | 1 | unset($content[array_search($asset->id, $content)]); |
|
81 | |||
82 | // Add video to (existing) content |
||
83 | 1 | $element->getContent()->$handle = array_merge($content, array($youTubeId)); |
|
84 | |||
85 | // Save the content without validation |
||
86 | 1 | craft()->content->saveContent($element, false); |
|
87 | |||
88 | // All went well |
||
89 | 1 | return true; |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * Check if this asset file already exists. |
||
94 | * |
||
95 | * @param AssetFileModel $asset |
||
96 | * |
||
97 | * @return string|bool |
||
98 | */ |
||
99 | protected function exists(AssetFileModel $asset) |
||
100 | { |
||
101 | // Check if we have this exist cached already |
||
102 | if (!isset($this->exists[$asset->id])) { |
||
103 | $hash = $this->getAssetFileHash($asset); |
||
104 | |||
105 | // Look up in db |
||
106 | $record = YouTube_HashesRecord::model()->findByAttributes(array( |
||
107 | 'hash' => $hash, |
||
108 | )); |
||
109 | |||
110 | // Get YouTube ID |
||
111 | $this->exists[$asset->id] = $record ? $record->youtubeId : false; |
||
112 | } |
||
113 | |||
114 | return $this->exists[$asset->id]; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Send video's to YouTube. |
||
119 | * |
||
120 | * @param AssetFileModel $asset |
||
121 | * |
||
122 | * @return string|bool |
||
123 | * |
||
124 | * @throws Exception |
||
125 | */ |
||
126 | 6 | protected function assemble(AssetFileModel $asset) |
|
165 | |||
166 | /** |
||
167 | * Authenticate with YouTube. |
||
168 | */ |
||
169 | 6 | protected function authenticate() |
|
170 | { |
||
171 | // Get token |
||
172 | 6 | $token = craft()->youTube_oauth->getToken(); |
|
173 | |||
174 | // Make token compatible with Google API |
||
175 | 6 | $json = JsonHelper::encode(array( |
|
176 | 6 | 'access_token' => $token->accessToken, |
|
177 | 6 | 'refresh_token' => $token->refreshToken, |
|
178 | 6 | 'expires_in' => $token->endOfLife, |
|
179 | 6 | 'created' => time(), |
|
180 | )); |
||
181 | |||
182 | // Set up a Google Client |
||
183 | 6 | $this->client = new \Google_Client(); |
|
184 | 6 | $this->client->setAccessToken($json); |
|
185 | |||
186 | // Define an object that will be used to make all API requests. |
||
187 | 6 | $this->youtube = new \Google_Service_YouTube($this->client); |
|
188 | 6 | } |
|
189 | |||
190 | /** |
||
191 | * Create a snippet with title, description, tags and category ID |
||
192 | * Create an asset resource and set its snippet metadata and type. |
||
193 | * |
||
194 | * @param AssetFileModel $asset |
||
195 | * |
||
196 | * @return \Google_Service_YouTube_VideoSnippet |
||
197 | */ |
||
198 | 6 | protected function createVideoSnippet(AssetFileModel $asset) |
|
205 | |||
206 | /** |
||
207 | * Set the video's status to "public". Valid statuses are "public", |
||
208 | * "private" and "unlisted". |
||
209 | * |
||
210 | * @return \Google_Service_YouTube_VideoStatus |
||
211 | */ |
||
212 | 6 | protected function setVideoStatus() |
|
219 | |||
220 | /** |
||
221 | * Associate the snippet and status objects with a new video resource. |
||
222 | * |
||
223 | * @param \Google_Service_YouTube_VideoSnippet $snippet |
||
224 | * @param \Google_Service_YouTube_VideoStatus $status |
||
225 | * |
||
226 | * @return \Google_Service_YouTube_Video |
||
227 | */ |
||
228 | 6 | protected function createVideoResource(\Google_Service_YouTube_VideoSnippet $snippet, \Google_Service_YouTube_VideoStatus $status) |
|
236 | |||
237 | /** |
||
238 | * Create a resumable video upload to YouTube. |
||
239 | * |
||
240 | * @param AssetFileModel $asset |
||
241 | * @param \Google_Service_YouTube_Video $video |
||
242 | * |
||
243 | * @throws Exception |
||
244 | * |
||
245 | * @return bool|string |
||
246 | */ |
||
247 | 6 | protected function uploadVideo(AssetFileModel $asset, \Google_Service_YouTube_Video $video) |
|
283 | |||
284 | /** |
||
285 | * Upload file in chunks. |
||
286 | * |
||
287 | * @param string $file |
||
288 | * @param \Google_Http_MediaFileUpload $media |
||
289 | * @param int $chunkSizeBytes |
||
290 | * |
||
291 | * @return bool|string |
||
292 | */ |
||
293 | protected function uploadChunks($file, \Google_Http_MediaFileUpload $media, $chunkSizeBytes) |
||
318 | |||
319 | /** |
||
320 | * fread will never return more than 8192 bytes if the stream is read buffered and it does not represent a plain file. |
||
321 | * |
||
322 | * @param resource $handle |
||
323 | * @param int $chunkSize |
||
324 | * |
||
325 | * @return string |
||
326 | */ |
||
327 | protected function readVideoChunk($handle, $chunkSize) |
||
342 | |||
343 | /** |
||
344 | * Save asset hash. |
||
345 | * |
||
346 | * @param AssetFileModel $asset |
||
347 | * @param string $youtubeId |
||
348 | */ |
||
349 | protected function saveHash(AssetFileModel $asset, $youtubeId) |
||
363 | |||
364 | /** |
||
365 | * Gets a file by its asset. |
||
366 | * |
||
367 | * @param AssetFileModel $asset |
||
368 | * |
||
369 | * @return string |
||
370 | */ |
||
371 | 6 | protected function getAssetFile(AssetFileModel $asset) |
|
387 | |||
388 | /** |
||
389 | * Get file hash. |
||
390 | * |
||
391 | * @param AssetFileModel $asset |
||
392 | * |
||
393 | * @return string |
||
394 | */ |
||
395 | protected function getAssetFileHash(AssetFileModel $asset) |
||
408 | } |
||
409 |