1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Extends the MediaReadOnlyStreamWrapper class to handle YouTube videos.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Create an instance like this:
|
10
|
* $youtube = new MediaYouTubeStreamWrapper('youtube://v/[video-code]');
|
11
|
*/
|
12
|
class MediaYouTubeStreamWrapper extends MediaReadOnlyStreamWrapper {
|
13
|
protected $base_url = 'https://www.youtube.com/watch';
|
14
|
|
15
|
static function getMimeType($uri, $mapping = NULL) {
|
16
|
return 'video/youtube';
|
17
|
}
|
18
|
|
19
|
function getOriginalThumbnailPath() {
|
20
|
$parts = $this->get_parameters();
|
21
|
$thumbnail_url = 'https://img.youtube.com/vi/' . check_plain($parts['v']) . "/maxresdefault.jpg";
|
22
|
$response = drupal_http_request($thumbnail_url);
|
23
|
if ($response->code == 404) {
|
24
|
$thumbnail_url = 'https://img.youtube.com/vi/' . check_plain($parts['v']) . "/hqdefault.jpg";
|
25
|
$response = drupal_http_request($thumbnail_url);
|
26
|
}
|
27
|
if (!isset($response->error)) {
|
28
|
return $thumbnail_url;
|
29
|
}
|
30
|
elseif ($response->code == -110) {
|
31
|
throw new MediaInternetValidationException("Connection timed out.");
|
32
|
}
|
33
|
elseif ($response->code == 401) {
|
34
|
throw new MediaInternetValidationException("Embedding has been disabled for this video.");
|
35
|
}
|
36
|
elseif ($response->code == 404) {
|
37
|
return "https://s.ytimg.com/yts/img/image-hh-404-vflvCykRp.png";
|
38
|
}
|
39
|
elseif ($response->code != 200) {
|
40
|
throw new MediaInternetValidationException("The YouTube video ID is invalid or the video was deleted.");
|
41
|
}
|
42
|
else {
|
43
|
$uri = file_stream_wrapper_uri_normalize('youtube://v/' . check_plain($parts['v']));
|
44
|
$external_url = file_create_url($uri);
|
45
|
$oembed_url = url('https://www.youtube.com/oembed', array('query' => array('url' => $external_url, 'format' => 'json')));
|
46
|
$response = drupal_http_request($oembed_url);
|
47
|
|
48
|
if (!isset($response->error)) {
|
49
|
$data = drupal_json_decode($response->data);
|
50
|
return $data['thumbnail_url'];
|
51
|
}
|
52
|
else {
|
53
|
throw new Exception("Error Processing Request. (Error: {$response->code}, {$response->error})");
|
54
|
return;
|
55
|
}
|
56
|
}
|
57
|
}
|
58
|
|
59
|
function getLocalThumbnailPath() {
|
60
|
$parts = $this->get_parameters();
|
61
|
$id = array_pop($parts);
|
62
|
$local_path = file_default_scheme() . '://media-youtube/' . check_plain($id) . '.jpg';
|
63
|
if (!file_exists($local_path)) {
|
64
|
// getOriginalThumbnailPath throws an exception if there are any errors
|
65
|
// when retrieving the original thumbnail from YouTube.
|
66
|
try {
|
67
|
$dirname = drupal_dirname($local_path);
|
68
|
file_prepare_directory($dirname, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
|
69
|
$response = drupal_http_request($this->getOriginalThumbnailPath());
|
70
|
|
71
|
if (!isset($response->error)) {
|
72
|
file_unmanaged_save_data($response->data, $local_path, TRUE);
|
73
|
}
|
74
|
else {
|
75
|
system_retrieve_file($this->getOriginalThumbnailPath(), $local_path, FALSE, FILE_EXISTS_REPLACE);
|
76
|
}
|
77
|
}
|
78
|
catch (Exception $e) {
|
79
|
// In the event of an endpoint error, use the mime type icon provided
|
80
|
// by the Media module.
|
81
|
$file = file_uri_to_object($this->uri);
|
82
|
$icon_dir = variable_get('media_icon_base_directory', 'public://media-icons') . '/' . variable_get('media_icon_set', 'default');
|
83
|
$local_path = file_icon_path($file, $icon_dir);
|
84
|
}
|
85
|
}
|
86
|
|
87
|
return $local_path;
|
88
|
}
|
89
|
|
90
|
/**
|
91
|
* Updates $base_url depending on whether the embed is a video or playlist.
|
92
|
*/
|
93
|
function setBaseUrl($parameters) {
|
94
|
if (isset($parameters['l'])) {
|
95
|
if (!isset($parameters['v'])) {
|
96
|
$this->base_url = 'https://youtube.com/playlist';
|
97
|
}
|
98
|
$parameters['list'] = $parameters['l'];
|
99
|
unset($parameters['l']);
|
100
|
}
|
101
|
return $parameters;
|
102
|
}
|
103
|
|
104
|
/**
|
105
|
* Returns a url in the format "https://www.youtube.com/watch?v=qsPQN4MiTeE".
|
106
|
*
|
107
|
* Overrides interpolateUrl() defined in MediaReadOnlyStreamWrapper.
|
108
|
*/
|
109
|
function interpolateUrl() {
|
110
|
if ($parameters = $this->get_parameters()) {
|
111
|
$parameters = $this->setBaseUrl($parameters);
|
112
|
return $this->base_url . '?' . http_build_query($parameters);
|
113
|
}
|
114
|
}
|
115
|
|
116
|
}
|