Bug 998063 - Support media:thumbnail in feed parser. r=mak
authorWes Johnston <wjohnston@mozilla.com>
Tue, 29 Apr 2014 15:23:15 -0700
changeset 181313 253acf38522981623e9c02d5e57ae8c48794ed51
parent 181312 3e0ff6c2930381760ac8bbedd93112b8f9e75f91
child 181314 5b99fe773ae9e66089e93b03299a67e75a2ccb7e
push id272
push userpvanderbeken@mozilla.com
push dateMon, 05 May 2014 16:31:18 +0000
reviewersmak
bugs998063
milestone32.0a1
Bug 998063 - Support media:thumbnail in feed parser. r=mak
toolkit/components/feeds/FeedProcessor.js
toolkit/components/feeds/test/xml/rss2/mrss_content.xml
--- a/toolkit/components/feeds/FeedProcessor.js
+++ b/toolkit/components/feeds/FeedProcessor.js
@@ -458,21 +458,25 @@ Entry.prototype = {
       this._atomLinksToEnclosures();
 
     // Add RSS2 enclosure to enclosures
     if (bagHasKey(this.fields, "enclosure"))
       this._enclosureToEnclosures();
 
     // Add media:content to enclosures
     if (bagHasKey(this.fields, "mediacontent"))
-      this._mediacontentToEnclosures();
+      this._mediaToEnclosures("mediacontent");
+
+    // Add media:thumbnail to enclosures
+    if (bagHasKey(this.fields, "mediathumbnail"))
+      this._mediaToEnclosures("mediathumbnail");
 
     // Add media:content in media:group to enclosures
     if (bagHasKey(this.fields, "mediagroup"))
-      this._mediagroupToEnclosures();
+      this._mediaToEnclosures("mediagroup", "mediacontent");
   },
 
   __enclosure_map: null,
 
   _addToEnclosures: function Entry_addToEnclosures(new_enc) {
     // items we add to the enclosures array get displayed in the FeedWriter and
     // they must have non-empty urls.
     if (!bagHasKey(new_enc, "url") || new_enc.getPropertyAsAString("url") == "")
@@ -534,60 +538,42 @@ Entry.prototype = {
     var enc = this.fields.getPropertyAsInterface("enclosure", Ci.nsIPropertyBag2);
 
     if (!(enc.getProperty("url")))
       return;
 
     this._addToEnclosures(enc);
   },
 
-  _mediacontentToEnclosures: function Entry_mediacontentToEnclosures() {
-    var mediacontent = this.fields.getPropertyAsInterface("mediacontent", Ci.nsIArray);
+  _mediaToEnclosures: function Entry_mediaToEnclosures(mediaType, contentType) {
+    var content = this.fields.getPropertyAsInterface(mediaType, Ci.nsIArray);
 
-    for (var i = 0; i < mediacontent.length; ++i) {
-      var contentElement = mediacontent.queryElementAt(i, Ci.nsIWritablePropertyBag2);
+    if (contentType)
+      content = content.getPropertyAsInterface(contentType, Ci.nsIArray);
+
+    for (var i = 0; i < content.length; ++i) {
+      var contentElement = content.queryElementAt(i, Ci.nsIWritablePropertyBag2);
 
       // media:content don't require url, but if it's not there, we should
       // skip it.
       if (!bagHasKey(contentElement, "url"))
         continue;
 
       var enc = Cc[BAG_CONTRACTID].createInstance(Ci.nsIWritablePropertyBag2);
 
       // copy media:content bits over to equivalent enclosure bits
       enc.setPropertyAsAString("url", contentElement.getPropertyAsAString("url"));
       if (bagHasKey(contentElement, "type")) {
         enc.setPropertyAsAString("type", contentElement.getPropertyAsAString("type"));
-      }
-      if (bagHasKey(contentElement, "fileSize")) {
-        enc.setPropertyAsAString("length", contentElement.getPropertyAsAString("fileSize"));
+      } else if (mediaType == "mediathumbnail") {
+        // thumbnails won't have a type, but default to image types
+        enc.setPropertyAsAString("type", "image/*");
+        enc.setPropertyAsBool("thumbnail", true);
       }
 
-      this._addToEnclosures(enc);
-    }
-  },
-
-  _mediagroupToEnclosures: function Entry_mediagroupToEnclosures() {
-    var group = this.fields.getPropertyAsInterface("mediagroup", Ci.nsIPropertyBag2);
-
-    var content = group.getPropertyAsInterface("mediacontent", Ci.nsIArray);
-    for (var i = 0; i < content.length; ++i) {
-      var contentElement = content.queryElementAt(i, Ci.nsIWritablePropertyBag2);
-      // media:content don't require url, but if it's not there, we should
-      // skip it.
-      if (!bagHasKey(contentElement, "url"))
-        continue;
-
-      var enc = Cc[BAG_CONTRACTID].createInstance(Ci.nsIWritablePropertyBag2);
-
-      // copy media:content bits over to equivalent enclosure bits
-      enc.setPropertyAsAString("url", contentElement.getPropertyAsAString("url"));
-      if (bagHasKey(contentElement, "type")) {
-        enc.setPropertyAsAString("type", contentElement.getPropertyAsAString("type"));
-      }
       if (bagHasKey(contentElement, "fileSize")) {
         enc.setPropertyAsAString("length", contentElement.getPropertyAsAString("fileSize"));
       }
 
       this._addToEnclosures(enc);
     }
   },
 
@@ -1132,29 +1118,31 @@ function FeedProcessor() {
       "dc:author": new ElementInfo("authors", Cc[PERSON_CONTRACTID],
                                    rssAuthor, true),
       "dc:contributor": new ElementInfo("contributors", Cc[PERSON_CONTRACTID],
                                          rssAuthor, true),
       "category": new ElementInfo("categories", null, rssCatTerm, true),
       "enclosure": new ElementInfo("enclosure", null, null, false),
       "media:content": new ElementInfo("mediacontent", null, null, true),
       "media:group": new ElementInfo("mediagroup", null, null, false),
+      "media:thumbnail": new ElementInfo("mediathumbnail", null, null, true),
       "guid": new ElementInfo("guid", null, rssGuid, false)
     },
 
     "IN_SKIPDAYS": {
       "day": new ElementInfo("days", null, rssArrayElement, true)
     },
 
     "IN_SKIPHOURS":{
       "hour": new ElementInfo("hours", null, rssArrayElement, true)
     },
 
     "IN_MEDIAGROUP": {
-      "media:content": new ElementInfo("mediacontent", null, null, true)
+      "media:content": new ElementInfo("mediacontent", null, null, true),
+      "media:thumbnail": new ElementInfo("mediathumbnail", null, null, true)
     },
  
     /********* RSS1 **********/
     "IN_RDF": {
       // If we hit a rss1:channel, we can verify that we have RSS1
       "rss1:channel": new FeedElementInfo("rdf_channel", "rss1"),
       "rss1:image": new ElementInfo("image", null, null, false),
       "rss1:textinput": new ElementInfo("textInput", null, null, false),
--- a/toolkit/components/feeds/test/xml/rss2/mrss_content.xml
+++ b/toolkit/components/feeds/test/xml/rss2/mrss_content.xml
@@ -4,17 +4,18 @@
 Description: mrss content works
 Expect: var enc = feed.items.queryElementAt(0, Components.interfaces.nsIFeedEntry).fields.getPropertyAsInterface('mediacontent', Components.interfaces.nsIArray).queryElementAt(0, Components.interfaces.nsIPropertyBag); ((enc.getProperty('fileSize') == '24986239') && (enc.getProperty('type') == 'video/mpeg') && (enc.getProperty('url') == 'http://dallas.example.com/joebob_050689.mpeg') && (feed.type == 4) && (feed.enclosureCount == 1));
 
 -->
 <rss xmlns:media="http://search.yahoo.com/mrss" version="2.0" >
 <channel>
 <item>
 
-<media:content fileSize="24986239" type="video/mpeg" url="http://dallas.example.com/joebob_050689.mpeg" /> 
+<media:content fileSize="24986239" type="video/mpeg" url="http://dallas.example.com/joebob_050689.mpeg" />
+<media:thumbnail url="http://dallas.example.com/joebob_050689.jpg"  width="75" height="50"/>
 <author>jbb@dallas.example.com (Joe Bob Briggs)</author>
 <comments>http://example.org</comments>
 <title>test</title>
 
 <category domain="foo">bar</category>
 
 <description>I'm headed for France. I wasn't gonna go this year, but then last week &lt;a href="http://www.imdb.com/title/tt0086525/"&gt;Valley Girl&lt;/a&gt; came out and I said to myself, Joe Bob, you gotta get out of the country for a while.</description></item>
 </channel>