Projet

Général

Profil

Révision 70a4c29b

Ajouté par Assos Assos il y a environ 9 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/media_youtube/includes/MediaYouTubeStreamWrapper.inc
1 1
<?php
2 2

  
3 3
/**
4
 *  @file media_youtube/includes/MediaYouTubeStreamWrapper.inc
5
 *
6
 *  Create a YouTube Stream Wrapper class for the Media/Resource module.
4
 *  @file
5
 *  Extends the MediaReadOnlyStreamWrapper class to handle YouTube videos.
7 6
 */
8 7

  
9 8
/**
......
11 10
 *  $youtube = new MediaYouTubeStreamWrapper('youtube://v/[video-code]');
12 11
 */
13 12
class MediaYouTubeStreamWrapper extends MediaReadOnlyStreamWrapper {
14

  
15
  // Overrides $base_url defined in MediaReadOnlyStreamWrapper.
16 13
  protected $base_url = 'http://www.youtube.com/watch';
17 14

  
18
  /**
19
   * Returns a url in the format "http://www.youtube.com/watch?v=qsPQN4MiTeE".
20
   *
21
   * Overrides interpolateUrl() defined in MediaReadOnlyStreamWrapper.
22
   * This is an exact copy of the function in MediaReadOnlyStreamWrapper,
23
   * here in case that example is redefined or removed.
24
   */
25
  function interpolateUrl() {
26
    if ($parameters = $this->get_parameters()) {
27
      return $this->base_url . '?' . http_build_query($parameters);
28
    }
29
  }
30

  
31 15
  static function getMimeType($uri, $mapping = NULL) {
32 16
    return 'video/youtube';
33 17
  }
34 18

  
35
  function getTarget($f) {
36
    return FALSE;
37
  }
38

  
39 19
  function getOriginalThumbnailPath() {
40 20
    $parts = $this->get_parameters();
41
    return 'http://img.youtube.com/vi/' . check_plain($parts['v']) . '/0.jpg';
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
    }
42 35
  }
43 36

  
44 37
  function getLocalThumbnailPath() {
45 38
    $parts = $this->get_parameters();
46
    $local_path = file_default_scheme() . '://media-youtube/' . check_plain($parts['v']) . '.jpg';
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

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

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

  
58 56
    return $local_path;
59 57
  }
60 58
}

Formats disponibles : Unified diff