Révision 18596a08
Ajouté par Assos Assos il y a presque 7 ans
drupal7/sites/all/modules/media_youtube/includes/MediaYouTubeStreamWrapper.inc | ||
---|---|---|
18 | 18 |
|
19 | 19 |
function getOriginalThumbnailPath() { |
20 | 20 |
$parts = $this->get_parameters(); |
21 |
$uri = file_stream_wrapper_uri_normalize('youtube://v/' . check_plain($parts['v'])); |
|
22 |
$external_url = file_create_url($uri); |
|
23 |
$oembed_url = url('http://www.youtube.com/oembed', array('query' => array('url' => $external_url, 'format' => 'json'))); |
|
24 |
$response = drupal_http_request($oembed_url); |
|
25 |
|
|
21 |
$thumbnail_url = 'http://img.youtube.com/vi/' . check_plain($parts['v']) . "/maxresdefault.jpg"; |
|
22 |
$response = drupal_http_request($thumbnail_url); |
|
26 | 23 |
if (!isset($response->error)) { |
27 |
$data = drupal_json_decode($response->data); |
|
28 |
return $data['thumbnail_url']; |
|
24 |
return $thumbnail_url; |
|
25 |
} |
|
26 |
elseif ($response->code == 401) { |
|
27 |
throw new MediaInternetValidationException("Embedding has been disabled for this video."); |
|
28 |
} |
|
29 |
elseif ($response->code == 404) { |
|
30 |
return "http://s.ytimg.com/yts/img/image-hh-404-vflvCykRp.png"; |
|
31 |
} |
|
32 |
elseif ($response->code != 200) { |
|
33 |
throw new MediaInternetValidationException("The YouTube video ID is invalid or the video was deleted."); |
|
29 | 34 |
} |
30 | 35 |
else { |
31 |
throw new Exception("Error Processing Request. (Error: {$response->code}, {$response->error})"); |
|
32 |
return; |
|
36 |
$uri = file_stream_wrapper_uri_normalize('youtube://v/' . check_plain($parts['v'])); |
|
37 |
$external_url = file_create_url($uri); |
|
38 |
$oembed_url = url('http://www.youtube.com/oembed', array('query' => array('url' => $external_url, 'format' => 'json'))); |
|
39 |
$response = drupal_http_request($oembed_url); |
|
40 |
|
|
41 |
if (!isset($response->error)) { |
|
42 |
$data = drupal_json_decode($response->data); |
|
43 |
return $data['thumbnail_url']; |
|
44 |
} |
|
45 |
else { |
|
46 |
throw new Exception("Error Processing Request. (Error: {$response->code}, {$response->error})"); |
|
47 |
return; |
|
48 |
} |
|
33 | 49 |
} |
34 | 50 |
} |
35 | 51 |
|
36 | 52 |
function getLocalThumbnailPath() { |
37 | 53 |
$parts = $this->get_parameters(); |
38 |
// There's no need to hide thumbnails, always use the public system rather |
|
39 |
// than file_default_scheme(). |
|
40 |
$local_path = 'public://media-youtube/' . check_plain($parts['v']) . '.jpg'; |
|
41 |
|
|
54 |
$id = array_pop($parts); |
|
55 |
$local_path = file_default_scheme() . '://media-youtube/' . check_plain($id) . '.jpg'; |
|
42 | 56 |
if (!file_exists($local_path)) { |
43 |
$dirname = drupal_dirname($local_path); |
|
44 |
file_prepare_directory($dirname, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS); |
|
45 |
$response = drupal_http_request($this->getOriginalThumbnailPath()); |
|
57 |
// getOriginalThumbnailPath throws an exception if there are any errors |
|
58 |
// when retrieving the original thumbnail from YouTube. |
|
59 |
try { |
|
60 |
$dirname = drupal_dirname($local_path); |
|
61 |
file_prepare_directory($dirname, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS); |
|
62 |
$response = drupal_http_request($this->getOriginalThumbnailPath()); |
|
46 | 63 |
|
47 |
if (!isset($response->error)) { |
|
48 |
file_unmanaged_save_data($response->data, $local_path, TRUE); |
|
64 |
if (!isset($response->error)) { |
|
65 |
file_unmanaged_save_data($response->data, $local_path, TRUE); |
|
66 |
} |
|
67 |
else { |
|
68 |
@copy($this->getOriginalThumbnailPath(), $local_path); |
|
69 |
} |
|
49 | 70 |
} |
50 |
else { |
|
51 |
@copy($this->getOriginalThumbnailPath(), $local_path); |
|
71 |
catch (Exception $e) { |
|
72 |
// In the event of an endpoint error, use the mime type icon provided |
|
73 |
// by the Media module. |
|
74 |
$file = file_uri_to_object($this->uri); |
|
75 |
$icon_dir = variable_get('media_icon_base_directory', 'public://media-icons') . '/' . variable_get('media_icon_set', 'default'); |
|
76 |
$local_path = file_icon_path($file, $icon_dir); |
|
52 | 77 |
} |
53 | 78 |
} |
54 | 79 |
|
55 | 80 |
return $local_path; |
56 | 81 |
} |
82 |
|
|
83 |
/** |
|
84 |
* Updates $base_url depending on whether the embed is a video or playlist. |
|
85 |
*/ |
|
86 |
function setBaseUrl($parameters) { |
|
87 |
if (isset($parameters['l'])) { |
|
88 |
if (!isset($parameters['v'])) { |
|
89 |
$this->base_url = 'http://youtube.com/playlist'; |
|
90 |
} |
|
91 |
$parameters['list'] = $parameters['l']; |
|
92 |
unset($parameters['l']); |
|
93 |
} |
|
94 |
return $parameters; |
|
95 |
} |
|
96 |
|
|
97 |
/** |
|
98 |
* Returns a url in the format "http://www.youtube.com/watch?v=qsPQN4MiTeE". |
|
99 |
* |
|
100 |
* Overrides interpolateUrl() defined in MediaReadOnlyStreamWrapper. |
|
101 |
*/ |
|
102 |
function interpolateUrl() { |
|
103 |
if ($parameters = $this->get_parameters()) { |
|
104 |
$parameters = $this->setBaseUrl($parameters); |
|
105 |
return $this->base_url . '?' . http_build_query($parameters); |
|
106 |
} |
|
107 |
} |
|
108 |
|
|
57 | 109 |
} |
Formats disponibles : Unified diff
Weekly update of contrib modules