Projet

Général

Profil

Paste
Télécharger (3,67 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / media_youtube / includes / MediaYouTubeStreamWrapper.inc @ 18596a08

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 = 'http://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 = 'http://img.youtube.com/vi/' . check_plain($parts['v']) . "/maxresdefault.jpg";
22
    $response = drupal_http_request($thumbnail_url);
23
    if (!isset($response->error)) {
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.");
34
    }
35
    else {
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
      }
49
    }
50
  }
51

    
52
  function getLocalThumbnailPath() {
53
    $parts = $this->get_parameters();
54
    $id = array_pop($parts);
55
    $local_path = file_default_scheme() . '://media-youtube/' . check_plain($id) . '.jpg';
56
    if (!file_exists($local_path)) {
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());
63

    
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
        }
70
      }
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);
77
      }
78
    }
79

    
80
    return $local_path;
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

    
109
}