1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file media_youtube/includes/MediaYouTubeStreamWrapper.inc
|
5 |
|
|
*
|
6 |
|
|
* Create a YouTube Stream Wrapper class for the Media/Resource module.
|
7 |
|
|
*/
|
8 |
|
|
|
9 |
|
|
/**
|
10 |
|
|
* Create an instance like this:
|
11 |
|
|
* $youtube = new MediaYouTubeStreamWrapper('youtube://v/[video-code]');
|
12 |
|
|
*/
|
13 |
|
|
class MediaYouTubeStreamWrapper extends MediaReadOnlyStreamWrapper {
|
14 |
|
|
|
15 |
|
|
// Overrides $base_url defined in MediaReadOnlyStreamWrapper.
|
16 |
|
|
protected $base_url = 'http://www.youtube.com/watch';
|
17 |
|
|
|
18 |
|
|
/**
|
19 |
|
|
* Returns a url in the format "http://www.youtube.com/watch?v=qsPQN4MiTeE".
|
20 |
|
|
*
|
21 |
|
|
* Overrides interpolateUrl() defined in MediaReadOnlyStreamWrapper.
|
22 |
|
|
* This is an exact copy of the function in MediaReadOnlyStreamWrapper,
|
23 |
|
|
* here in case that example is redefined or removed.
|
24 |
|
|
*/
|
25 |
|
|
function interpolateUrl() {
|
26 |
|
|
if ($parameters = $this->get_parameters()) {
|
27 |
|
|
return $this->base_url . '?' . http_build_query($parameters);
|
28 |
|
|
}
|
29 |
|
|
}
|
30 |
|
|
|
31 |
|
|
static function getMimeType($uri, $mapping = NULL) {
|
32 |
|
|
return 'video/youtube';
|
33 |
|
|
}
|
34 |
|
|
|
35 |
|
|
function getTarget($f) {
|
36 |
|
|
return FALSE;
|
37 |
|
|
}
|
38 |
|
|
|
39 |
|
|
function getOriginalThumbnailPath() {
|
40 |
|
|
$parts = $this->get_parameters();
|
41 |
|
|
return 'http://img.youtube.com/vi/' . check_plain($parts['v']) . '/0.jpg';
|
42 |
|
|
}
|
43 |
|
|
|
44 |
|
|
function getLocalThumbnailPath() {
|
45 |
|
|
$parts = $this->get_parameters();
|
46 |
|
|
$local_path = file_default_scheme() . '://media-youtube/' . check_plain($parts['v']) . '.jpg';
|
47 |
|
|
if (!file_exists($local_path)) {
|
48 |
|
|
$dirname = drupal_dirname($local_path);
|
49 |
|
|
file_prepare_directory($dirname, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
|
50 |
|
|
$response = drupal_http_request($this->getOriginalThumbnailPath());
|
51 |
|
|
if (!isset($response->error)) {
|
52 |
|
|
file_unmanaged_save_data($response->data, $local_path, TRUE);
|
53 |
|
|
}
|
54 |
|
|
else {
|
55 |
|
|
@copy($this->getOriginalThumbnailPath(), $local_path);
|
56 |
|
|
}
|
57 |
|
|
}
|
58 |
|
|
return $local_path;
|
59 |
|
|
}
|
60 |
|
|
} |