Projet

Général

Profil

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

root / drupal7 / sites / all / modules / media_youtube / includes / MediaYouTubeStreamWrapper.inc @ 3acd948f

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 == 401) {
31
      throw new MediaInternetValidationException("Embedding has been disabled for this video.");
32
    }
33
    elseif ($response->code == 404) {
34
      return "https://s.ytimg.com/yts/img/image-hh-404-vflvCykRp.png";
35
    }
36
    elseif ($response->code != 200) {
37
      throw new MediaInternetValidationException("The YouTube video ID is invalid or the video was deleted.");
38
    }
39
    else {
40
      $uri = file_stream_wrapper_uri_normalize('youtube://v/' . check_plain($parts['v']));
41
      $external_url = file_create_url($uri);
42
      $oembed_url = url('https://www.youtube.com/oembed', array('query' => array('url' => $external_url, 'format' => 'json')));
43
      $response = drupal_http_request($oembed_url);
44

    
45
      if (!isset($response->error)) {
46
        $data = drupal_json_decode($response->data);
47
        return $data['thumbnail_url'];
48
      }
49
      else {
50
        throw new Exception("Error Processing Request. (Error: {$response->code}, {$response->error})");
51
        return;
52
      }
53
    }
54
  }
55

    
56
  function getLocalThumbnailPath() {
57
    $parts = $this->get_parameters();
58
    $id = array_pop($parts);
59
    $local_path = file_default_scheme() . '://media-youtube/' . check_plain($id) . '.jpg';
60
    if (!file_exists($local_path)) {
61
      // getOriginalThumbnailPath throws an exception if there are any errors
62
      // when retrieving the original thumbnail from YouTube.
63
      try {
64
        $dirname = drupal_dirname($local_path);
65
        file_prepare_directory($dirname, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
66
        $response = drupal_http_request($this->getOriginalThumbnailPath());
67

    
68
        if (!isset($response->error)) {
69
          file_unmanaged_save_data($response->data, $local_path, TRUE);
70
        }
71
        else {
72
          system_retrieve_file($this->getOriginalThumbnailPath(), $local_path, FALSE, FILE_EXISTS_REPLACE);
73
        }
74
      }
75
      catch (Exception $e) {
76
        // In the event of an endpoint error, use the mime type icon provided
77
        // by the Media module.
78
        $file = file_uri_to_object($this->uri);
79
        $icon_dir = variable_get('media_icon_base_directory', 'public://media-icons') . '/' . variable_get('media_icon_set', 'default');
80
        $local_path = file_icon_path($file, $icon_dir);
81
      }
82
    }
83

    
84
    return $local_path;
85
  }
86

    
87
  /**
88
   * Updates $base_url depending on whether the embed is a video or playlist.
89
   */
90
  function setBaseUrl($parameters) {
91
    if (isset($parameters['l'])) {
92
      if (!isset($parameters['v'])) {
93
        $this->base_url = 'https://youtube.com/playlist';
94
      }
95
      $parameters['list'] = $parameters['l'];
96
      unset($parameters['l']);
97
    }
98
    return $parameters;
99
  }
100

    
101
  /**
102
   * Returns a url in the format "http://www.youtube.com/watch?v=qsPQN4MiTeE".
103
   *
104
   * Overrides interpolateUrl() defined in MediaReadOnlyStreamWrapper.
105
   */
106
  function interpolateUrl() {
107
    if ($parameters = $this->get_parameters()) {
108
      $parameters = $this->setBaseUrl($parameters);
109
      return $this->base_url . '?' . http_build_query($parameters);
110
    }
111
  }
112

    
113
}