Project

General

Profile

Paste
Download (1.86 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / media_youtube / includes / MediaYouTubeStreamWrapper.inc @ 70a4c29b

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
    $v = check_plain($parts['v']);
22
    //  Attempt to pull a HD thumbnail from YouTube. If it exists pass it on
23
    //  otherwise pass on the smaller one.
24
    $thumbname = drupal_tempnam('temporary://', 'youtube');
25
    $response = drupal_http_request('http://img.youtube.com/vi/' . $v . '/maxresdefault.jpg');
26
    if (!isset($response->error)) {
27
      file_unmanaged_save_data($response->data, $thumbname, $replace = FILE_EXISTS_REPLACE);
28
    }
29
    if ((filesize($thumbname)) == 0) {
30
      return 'http://img.youtube.com/vi/' . $v . '/0.jpg';
31
    }
32
    else {
33
      return 'http://img.youtube.com/vi/' . $v . '/maxresdefault.jpg';
34
    }
35
  }
36

    
37
  function getLocalThumbnailPath() {
38
    $parts = $this->get_parameters();
39
    // There's no need to hide thumbnails, always use the public system rather
40
    // than file_default_scheme().
41
    $local_path = 'public://media-youtube/' . check_plain($parts['v']) . '.jpg';
42

    
43
    if (!file_exists($local_path)) {
44
      $dirname = drupal_dirname($local_path);
45
      file_prepare_directory($dirname, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
46
      $response = drupal_http_request($this->getOriginalThumbnailPath());
47

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

    
56
    return $local_path;
57
  }
58
}