MediaYouTubeStreamWrapper.inc
1.83 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
<?php
/**
* @file media_youtube/includes/MediaYouTubeStreamWrapper.inc
*
* Create a YouTube Stream Wrapper class for the Media/Resource module.
*/
/**
* Create an instance like this:
* $youtube = new MediaYouTubeStreamWrapper('youtube://v/[video-code]');
*/
class MediaYouTubeStreamWrapper extends MediaReadOnlyStreamWrapper {
// Overrides $base_url defined in MediaReadOnlyStreamWrapper.
protected $base_url = 'http://www.youtube.com/watch';
/**
* Returns a url in the format "http://www.youtube.com/watch?v=qsPQN4MiTeE".
*
* Overrides interpolateUrl() defined in MediaReadOnlyStreamWrapper.
* This is an exact copy of the function in MediaReadOnlyStreamWrapper,
* here in case that example is redefined or removed.
*/
function interpolateUrl() {
if ($parameters = $this->get_parameters()) {
return $this->base_url . '?' . http_build_query($parameters);
}
}
static function getMimeType($uri, $mapping = NULL) {
return 'video/youtube';
}
function getTarget($f) {
return FALSE;
}
function getOriginalThumbnailPath() {
$parts = $this->get_parameters();
return 'http://img.youtube.com/vi/' . check_plain($parts['v']) . '/0.jpg';
}
function getLocalThumbnailPath() {
$parts = $this->get_parameters();
$local_path = file_default_scheme() . '://media-youtube/' . check_plain($parts['v']) . '.jpg';
if (!file_exists($local_path)) {
$dirname = drupal_dirname($local_path);
file_prepare_directory($dirname, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
$response = drupal_http_request($this->getOriginalThumbnailPath());
if (!isset($response->error)) {
file_unmanaged_save_data($response->data, $local_path, TRUE);
}
else {
@copy($this->getOriginalThumbnailPath(), $local_path);
}
}
return $local_path;
}
}