From 050477b088dcb6f916f7223cc19c6820ef7f2c8d Mon Sep 17 00:00:00 2001
From: andrew morton <drewish@katherinehouse.com>
Date: Fri, 23 Mar 2012 13:00:07 -0600
Subject: [PATCH 1/5] Make getVideoProperties() static and move static
 functions to the top of the file.

---
 includes/MediaVimeoStreamWrapper.inc |   33 +++++++++++++++++----------------
 1 files changed, 17 insertions(+), 16 deletions(-)

diff --git a/includes/MediaVimeoStreamWrapper.inc b/includes/MediaVimeoStreamWrapper.inc
index bc97dc8..ed779a7 100644
--- a/includes/MediaVimeoStreamWrapper.inc
+++ b/includes/MediaVimeoStreamWrapper.inc
@@ -10,14 +10,26 @@
  *  $vimeo = new ResourceVimeoStreamWrapper('vimeo://v/[video-code]');
  */
 class MediaVimeoStreamWrapper extends MediaReadOnlyStreamWrapper {
-  protected $base_url = 'http://vimeo.com/';
+  static function getMimeType($uri, $mapping = NULL) {
+    return 'video/vimeo';
+  }
 
-  function getTarget($f) {
-    return FALSE;
+  /**
+   * Call the Vimeo Simple API and fetch the video information.
+   *
+   * See http://vimeo.com/api/docs/simple-api
+   *
+   * @return
+   *   Array of properties.
+   */
+  static function getVideoProperties($video_id) {
+    // The .php format returns a serialized array.
+    $response = drupal_http_request('http://vimeo.com/api/v2/video/'. $video_id .'.php');
+    return unserialize($response->data);
   }
 
-  static function getMimeType($uri, $mapping = NULL) {
-    return 'video/vimeo';
+  function getTarget($f) {
+    return FALSE;
   }
 
   function getOriginalThumbnailPath() {
@@ -39,15 +51,4 @@ class MediaVimeoStreamWrapper extends MediaReadOnlyStreamWrapper {
     }
     return $local_path;
   }
-
-  /**
-   *  Ref: http://vimeo.com/api/docs/simple-api
-   *  Example of Vimeo simple API 2 request for video properties:
-   *  http://vimeo.com/api/v2/video/xxx.php
-   *  This request returns a serialized array.
-   */
-  function getVideoProperties($video_id) {
-    $response = drupal_http_request($this->base_url . '/api/v2/video/'. $video_id .'.php');
-    return unserialize($response->data);
-  }
 }
-- 
1.7.7.4


From 7b5f2907a2a04e77a0c5833c6bb4fcf29111a5b0 Mon Sep 17 00:00:00 2001
From: andrew morton <drewish@katherinehouse.com>
Date: Fri, 23 Mar 2012 13:01:54 -0600
Subject: [PATCH 2/5] Better checking of video ids.

Make sure the video ids are numeric rather than using check_plain().
Also simplify the way we load the video id.
---
 includes/MediaVimeoStreamWrapper.inc |    9 ++-------
 1 files changed, 2 insertions(+), 7 deletions(-)

diff --git a/includes/MediaVimeoStreamWrapper.inc b/includes/MediaVimeoStreamWrapper.inc
index ed779a7..c679e10 100644
--- a/includes/MediaVimeoStreamWrapper.inc
+++ b/includes/MediaVimeoStreamWrapper.inc
@@ -33,17 +33,12 @@ class MediaVimeoStreamWrapper extends MediaReadOnlyStreamWrapper {
   }
 
   function getOriginalThumbnailPath() {
-    $video_properties = array();
-
-    $parts = $this->get_parameters();
-    $video_properties = $this->getVideoProperties(check_plain($parts['v']));
-
+    $video_properties = self::getVideoProperties($this->parameters['v']);
     return $video_properties[0]['thumbnail_large'];
   }
 
   function getLocalThumbnailPath() {
-    $parts = $this->get_parameters();
-    $local_path = 'public://media-vimeo/' . check_plain($parts['v']) . '.jpg';
+    $local_path = 'public://media-vimeo/' . intval($this->parameters['v']) . '.jpg';
     if (!file_exists($local_path)) {
       $dirname = drupal_dirname($local_path);
       file_prepare_directory($dirname, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
-- 
1.7.7.4


From c9fa21b32e6c14a2332c431e48db164000987dfe Mon Sep 17 00:00:00 2001
From: andrew morton <drewish@katherinehouse.com>
Date: Fri, 23 Mar 2012 12:58:29 -0600
Subject: [PATCH 3/5] Generate the correct external URLs so drupal_realpath()
 works.

---
 includes/MediaVimeoStreamWrapper.inc |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)

diff --git a/includes/MediaVimeoStreamWrapper.inc b/includes/MediaVimeoStreamWrapper.inc
index c679e10..ee3bda0 100644
--- a/includes/MediaVimeoStreamWrapper.inc
+++ b/includes/MediaVimeoStreamWrapper.inc
@@ -32,6 +32,10 @@ class MediaVimeoStreamWrapper extends MediaReadOnlyStreamWrapper {
     return FALSE;
   }
 
+  function interpolateUrl() {
+    return 'http://vimeo.com/' . intval($this->parameters['v']);
+  }
+
   function getOriginalThumbnailPath() {
     $video_properties = self::getVideoProperties($this->parameters['v']);
     return $video_properties[0]['thumbnail_large'];
-- 
1.7.7.4


From d03ac347ee820850c61f4c87ea47bfb520baebd4 Mon Sep 17 00:00:00 2001
From: andrew morton <drewish@katherinehouse.com>
Date: Fri, 23 Mar 2012 13:07:18 -0600
Subject: [PATCH 4/5] Better formatting on the PHPDocs.

---
 includes/MediaInternetVimeoHandler.inc |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/includes/MediaInternetVimeoHandler.inc b/includes/MediaInternetVimeoHandler.inc
index 6318dd2..6baca7c 100644
--- a/includes/MediaInternetVimeoHandler.inc
+++ b/includes/MediaInternetVimeoHandler.inc
@@ -38,7 +38,9 @@ class MediaInternetVimeoHandler extends MediaInternetBaseHandler {
   }
 
   /**
-   * Returns information about the media. See http://video.search.yahoo.com/mrss.
+   * Returns information about the media.
+   *
+   * See http://video.search.yahoo.com/mrss
    *
    * @return
    *   If ATOM+MRSS information is available, a SimpleXML element containing
@@ -59,7 +61,9 @@ class MediaInternetVimeoHandler extends MediaInternetBaseHandler {
   }
 
   /**
-   * Returns information about the media. See http://www.oembed.com/.
+   * Returns information about the media.
+   *
+   * See http://www.oembed.com/ and https://vimeo.com/api/docs/oembed
    *
    * @return
    *   If oEmbed information is available, an array containing 'title', 'type',
-- 
1.7.7.4


From 0762fc6d60cc4d7e919663bfdd42ec6e85bfcef3 Mon Sep 17 00:00:00 2001
From: andrew morton <drewish@katherinehouse.com>
Date: Fri, 23 Mar 2012 13:08:11 -0600
Subject: [PATCH 5/5] Issue #1422980 by valderama and drewish: Set the title
 of the file entity to the fetched title of the video.

---
 includes/MediaInternetVimeoHandler.inc |   12 ++++++++++--
 1 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/includes/MediaInternetVimeoHandler.inc b/includes/MediaInternetVimeoHandler.inc
index 6baca7c..5ec5730 100644
--- a/includes/MediaInternetVimeoHandler.inc
+++ b/includes/MediaInternetVimeoHandler.inc
@@ -34,7 +34,13 @@ class MediaInternetVimeoHandler extends MediaInternetBaseHandler {
     $uri = $this->parse($this->embedCode);
     //@todo: this is terribly broken in some ways because the function is really
     // made for local files which are 'real'
-    return file_uri_to_object($uri);
+    $file = file_uri_to_object($uri, TRUE);
+
+    // Try to default the file name to the video's title.
+    if (empty($file->fid) && $info = $this->getOEmbed()) {
+      $file->filename = truncate_utf8($info['title'], 255);
+    }
+    return $file;
   }
 
   /**
@@ -73,7 +79,9 @@ class MediaInternetVimeoHandler extends MediaInternetBaseHandler {
   public function getOEmbed() {
     $uri = $this->parse($this->embedCode);
     $external_url = drupal_realpath($uri);
-    $oembed_url = url('http://www.vimeo.com/oembed', array('query' => array('url' => $external_url, 'format' => 'json')));
+    $oembed_url = url('http://vimeo.com/api/oembed.json', array(
+      'query' => array('url' => $external_url))
+    );
     $response = drupal_http_request($oembed_url);
     if (!isset($response->error)) {
       return drupal_json_decode($response->data);
-- 
1.7.7.4

