Projet

Général

Profil

Paste
Télécharger (1,8 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / media_youtube / includes / MediaYouTubeStreamWrapper.inc @ 58344a8d

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
    $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

    
26
    if (!isset($response->error)) {
27
      $data = drupal_json_decode($response->data);
28
      return $data['thumbnail_url'];
29
    }
30
    else {
31
      throw new Exception("Error Processing Request. (Error: {$response->code}, {$response->error})");
32
      return;
33
    }
34
  }
35

    
36
  function getLocalThumbnailPath() {
37
    $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

    
42
    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());
46

    
47
      if (!isset($response->error)) {
48
        file_unmanaged_save_data($response->data, $local_path, TRUE);
49
      }
50
      else {
51
        @copy($this->getOriginalThumbnailPath(), $local_path);
52
      }
53
    }
54

    
55
    return $local_path;
56
  }
57
}