You are here

class MediaYouTubeStreamWrapper in Media: YouTube 7.2

Same name and namespace in other branches
  1. 7.3 includes/MediaYouTubeStreamWrapper.inc \MediaYouTubeStreamWrapper
  2. 7 includes/MediaYouTubeStreamWrapper.inc \MediaYouTubeStreamWrapper

Create an instance like this: $youtube = new MediaYouTubeStreamWrapper('youtube://v/[video-code]');

Hierarchy

Expanded class hierarchy of MediaYouTubeStreamWrapper

1 string reference to 'MediaYouTubeStreamWrapper'
media_youtube_stream_wrappers in ./media_youtube.module
Implements hook_stream_wrappers().

File

includes/MediaYouTubeStreamWrapper.inc, line 12
Extends the MediaReadOnlyStreamWrapper class to handle YouTube videos.

View source
class MediaYouTubeStreamWrapper extends MediaReadOnlyStreamWrapper {
  protected $base_url = 'http://www.youtube.com/watch';
  static function getMimeType($uri, $mapping = NULL) {
    return 'video/youtube';
  }
  function getOriginalThumbnailPath() {
    $parts = $this
      ->get_parameters();
    $v = check_plain($parts['v']);

    //  Attempt to pull a HD thumbnail from YouTube. If it exists pass it on
    //  otherwise pass on the smaller one.
    $thumbname = drupal_tempnam('temporary://', 'youtube');
    $response = drupal_http_request('http://img.youtube.com/vi/' . $v . '/maxresdefault.jpg');
    if (!isset($response->error)) {
      file_unmanaged_save_data($response->data, $thumbname, $replace = FILE_EXISTS_REPLACE);
    }
    if (filesize($thumbname) == 0) {
      return 'http://img.youtube.com/vi/' . $v . '/0.jpg';
    }
    else {
      return 'http://img.youtube.com/vi/' . $v . '/maxresdefault.jpg';
    }
  }
  function getLocalThumbnailPath() {
    $parts = $this
      ->get_parameters();

    // There's no need to hide thumbnails, always use the public system rather
    // than file_default_scheme().
    $local_path = 'public://media-youtube/' . check_plain($parts['v']) . '.jpg';
    if (!file_exists($local_path)) {
      $dirname = drupal_dirname($local_path);
      file_prepare_directory($dirname, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
      $response = drupal_http_request($this
        ->getOriginalThumbnailPath());
      if (!isset($response->error)) {
        file_unmanaged_save_data($response->data, $local_path, TRUE);
      }
      else {
        @copy($this
          ->getOriginalThumbnailPath(), $local_path);
      }
    }
    return $local_path;
  }

}

Members