summary refs log tree commit diff
path: root/synapse/rest/media/v1
diff options
context:
space:
mode:
Diffstat (limited to 'synapse/rest/media/v1')
-rw-r--r--synapse/rest/media/v1/media_repository.py1
-rw-r--r--synapse/rest/media/v1/preview_url_resource.py127
2 files changed, 73 insertions, 55 deletions
diff --git a/synapse/rest/media/v1/media_repository.py b/synapse/rest/media/v1/media_repository.py
index 8f3491b91c..11f672aeab 100644
--- a/synapse/rest/media/v1/media_repository.py
+++ b/synapse/rest/media/v1/media_repository.py
@@ -80,4 +80,3 @@ class MediaRepositoryResource(Resource):
         self.putChild("thumbnail", ThumbnailResource(hs, filepaths))
         self.putChild("identicon", IdenticonResource())
         self.putChild("preview_url", PreviewUrlResource(hs, filepaths))
-
diff --git a/synapse/rest/media/v1/preview_url_resource.py b/synapse/rest/media/v1/preview_url_resource.py
index 7c69c01a6c..29db5c7fce 100644
--- a/synapse/rest/media/v1/preview_url_resource.py
+++ b/synapse/rest/media/v1/preview_url_resource.py
@@ -13,25 +13,31 @@
 # limitations under the License.
 
 from .base_resource import BaseMediaResource
-from synapse.api.errors import Codes
-from twisted.web.resource import Resource
+
 from twisted.web.server import NOT_DONE_YET
 from twisted.internet import defer
 from lxml import html
 from urlparse import urlparse, urlunparse
+
+from synapse.api.errors import Codes
 from synapse.util.stringutils import random_string
 from synapse.util.caches.expiringcache import ExpiringCache
 from synapse.http.client import SpiderHttpClient
-from synapse.http.server import request_handler, respond_with_json, respond_with_json_bytes
+from synapse.http.server import (
+    request_handler, respond_with_json, respond_with_json_bytes
+)
 from synapse.util.async import ObservableDeferred
+from synapse.util.stringutils import is_ascii
 
 import os
 import re
+import cgi
 import ujson as json
 
 import logging
 logger = logging.getLogger(__name__)
 
+
 class PreviewUrlResource(BaseMediaResource):
     isLeaf = True
 
@@ -41,9 +47,10 @@ class PreviewUrlResource(BaseMediaResource):
 
         # simple memory cache mapping urls to OG metadata
         self.cache = ExpiringCache(
-            cache_name = "url_previews",
-            clock = self.clock,
-            expiry_ms = 60*60*1000, # don't spider URLs more often than once an hour
+            cache_name="url_previews",
+            clock=self.clock,
+            # don't spider URLs more often than once an hour
+            expiry_ms=60 * 60 * 1000,
         )
         self.cache.start()
 
@@ -56,12 +63,15 @@ class PreviewUrlResource(BaseMediaResource):
     @request_handler
     @defer.inlineCallbacks
     def _async_render_GET(self, request):
-        
+
         try:
             # XXX: if get_user_by_req fails, what should we do in an async render?
             requester = yield self.auth.get_user_by_req(request)
             url = request.args.get("url")[0]
-            ts = int(request.args.get("ts")[0]) if "ts" in request.args else self.clock.time_msec()
+            if "ts" in request.args:
+                ts = int(request.args.get("ts")[0])
+            else:
+                ts = self.clock.time_msec()
 
             # first check the memory cache - good to handle all the clients on this
             # HS thundering away to preview the same URL at the same time.
@@ -98,7 +108,7 @@ class PreviewUrlResource(BaseMediaResource):
 
                 @download.addBoth
                 def callback(media_info):
-                    del self.downloads[key]
+                    del self.downloads[url]
                     return media_info
             media_info = yield download.observe()
 
@@ -111,13 +121,15 @@ class PreviewUrlResource(BaseMediaResource):
 
             if self._is_media(media_info['media_type']):
                 dims = yield self._generate_local_thumbnails(
-                        media_info['filesystem_id'], media_info
-                      )
+                    media_info['filesystem_id'], media_info
+                )
 
                 og = {
-                    "og:description" : media_info['download_name'],
-                    "og:image" : "mxc://%s/%s" % (self.server_name, media_info['filesystem_id']),
-                    "og:image:type" : media_info['media_type'],
+                    "og:description": media_info['download_name'],
+                    "og:image": "mxc://%s/%s" % (
+                        self.server_name, media_info['filesystem_id']
+                    ),
+                    "og:image:type": media_info['media_type'],
                 }
 
                 if dims:
@@ -138,14 +150,14 @@ class PreviewUrlResource(BaseMediaResource):
                     file = open(media_info['filename'])
                     body = file.read()
                     file.close()
-                    tree = html.fromstring(body.decode('utf-8','ignore'))
+                    tree = html.fromstring(body.decode('utf-8', 'ignore'))
                     og = yield self._calc_og(tree, media_info, requester)
 
             else:
                 logger.warn("Failed to find any OG data in %s", url)
                 og = {}
 
-            logger.debug("Calculated OG for %s as %s" % (url, og));
+            logger.debug("Calculated OG for %s as %s" % (url, og))
 
             # store OG in ephemeral in-memory cache
             self.cache[url] = og
@@ -181,28 +193,20 @@ class PreviewUrlResource(BaseMediaResource):
         # suck our tree into lxml and define our OG response.
 
         # if we see any image URLs in the OG response, then spider them
-        # (although the client could choose to do this by asking for previews of those URLs to avoid DoSing the server)
-
-        # "og:type"        : "article"
-        # "og:url"         : "https://twitter.com/matrixdotorg/status/684074366691356672"
-        # "og:title"       : "Matrix on Twitter"
-        # "og:image"       : "https://pbs.twimg.com/profile_images/500400952029888512/yI0qtFi7_400x400.png"
-        # "og:description" : "Synapse 0.12 is out! Lots of polishing, performance & bugfixes: /sync API, /r0 prefix, fulltext search, 3PID invites https://t.co/5alhXLLEGP"
-        # "og:site_name"   : "Twitter"
-        
-        # or:
+        # (although the client could choose to do this by asking for previews of those
+        # URLs to avoid DoSing the server)
 
         # "og:type"         : "video",
         # "og:url"          : "https://www.youtube.com/watch?v=LXDBoHyjmtw",
         # "og:site_name"    : "YouTube",
         # "og:video:type"   : "application/x-shockwave-flash",
-        # "og:description"  : " ",
+        # "og:description"  : "Fun stuff happening here",
         # "og:title"        : "RemoteJam - Matrix team hack for Disrupt Europe Hackathon",
         # "og:image"        : "https://i.ytimg.com/vi/LXDBoHyjmtw/maxresdefault.jpg",
         # "og:video:url"    : "http://www.youtube.com/v/LXDBoHyjmtw?version=3&autohide=1",
         # "og:video:width"  : "1280"
         # "og:video:height" : "720",
-        # "og:video:secure_url": "https://www.youtube.com/v/LXDBoHyjmtw?version=3&autohide=1",
+        # "og:video:secure_url": "https://www.youtube.com/v/LXDBoHyjmtw?version=3",
 
         og = {}
         for tag in tree.xpath("//*/meta[starts-with(@property, 'og:')]"):
@@ -210,64 +214,76 @@ class PreviewUrlResource(BaseMediaResource):
 
         # TODO: grab article: meta tags too, e.g.:
 
-        # <meta property="article:publisher" content="https://www.facebook.com/thethudonline" />
-        # <meta property="article:author" content="https://www.facebook.com/thethudonline" />
-        # <meta property="article:tag" content="baby" />
-        # <meta property="article:section" content="Breaking News" />
-        # <meta property="article:published_time" content="2016-03-31T19:58:24+00:00" />
-        # <meta property="article:modified_time" content="2016-04-01T18:31:53+00:00" />
+        # "article:publisher" : "https://www.facebook.com/thethudonline" />
+        # "article:author" content="https://www.facebook.com/thethudonline" />
+        # "article:tag" content="baby" />
+        # "article:section" content="Breaking News" />
+        # "article:published_time" content="2016-03-31T19:58:24+00:00" />
+        # "article:modified_time" content="2016-04-01T18:31:53+00:00" />
 
         if 'og:title' not in og:
             # do some basic spidering of the HTML
             title = tree.xpath("(//title)[1] | (//h1)[1] | (//h2)[1] | (//h3)[1]")
             og['og:title'] = title[0].text.strip() if title else None
 
-
         if 'og:image' not in og:
             # TODO: extract a favicon failing all else
-            meta_image = tree.xpath("//*/meta[translate(@itemprop, 'IMAGE', 'image')='image']/@content");
+            meta_image = tree.xpath(
+                "//*/meta[translate(@itemprop, 'IMAGE', 'image')='image']/@content"
+            )
             if meta_image:
                 og['og:image'] = self._rebase_url(meta_image[0], media_info['uri'])
             else:
                 # TODO: consider inlined CSS styles as well as width & height attribs
                 images = tree.xpath("//img[@src][number(@width)>10][number(@height)>10]")
-                images = sorted(images, key=lambda i: (-1 * int(i.attrib['width']) * int(i.attrib['height'])))
+                images = sorted(images, key=lambda i: (
+                    -1 * int(i.attrib['width']) * int(i.attrib['height'])
+                ))
                 if not images:
                     images = tree.xpath("//img[@src]")
                 if images:
                     og['og:image'] = images[0].attrib['src']
 
         # pre-cache the image for posterity
-        # FIXME: it might be cleaner to use the same flow as the main /preview_url request itself
-        # and benefit from the same caching etc.  But for now we just rely on the caching
-        # of the master request to speed things up.
+        # FIXME: it might be cleaner to use the same flow as the main /preview_url request
+        # itself and benefit from the same caching etc.  But for now we just rely on the
+        # caching on the master request to speed things up.
         if 'og:image' in og and og['og:image']:
-            image_info = yield self._download_url(self._rebase_url(og['og:image'], media_info['uri']), requester.user)
+            image_info = yield self._download_url(
+                self._rebase_url(og['og:image'], media_info['uri']), requester.user
+            )
 
             if self._is_media(image_info['media_type']):
                 # TODO: make sure we don't choke on white-on-transparent images
                 dims = yield self._generate_local_thumbnails(
-                        image_info['filesystem_id'], image_info
-                      )
+                    image_info['filesystem_id'], image_info
+                )
                 if dims:
                     og["og:image:width"] = dims['width']
                     og["og:image:height"] = dims['height']
                 else:
                     logger.warn("Couldn't get dims for %s" % og["og:image"])
 
-                og["og:image"] = "mxc://%s/%s" % (self.server_name, image_info['filesystem_id'])
+                og["og:image"] = "mxc://%s/%s" % (
+                    self.server_name, image_info['filesystem_id']
+                )
                 og["og:image:type"] = image_info['media_type']
             else:
                 del og["og:image"]
 
         if 'og:description' not in og:
-            meta_description = tree.xpath("//*/meta[translate(@name, 'DESCRIPTION', 'description')='description']/@content");
+            meta_description = tree.xpath(
+                "//*/meta"
+                "[translate(@name, 'DESCRIPTION', 'description')='description']"
+                "/@content")
             if meta_description:
                 og['og:description'] = meta_description[0]
             else:
-                # text_nodes = tree.xpath("//h1/text() | //h2/text() | //h3/text() | //p/text() | //div/text() | //span/text() | //a/text()")                
-                text_nodes = tree.xpath("//text()[not(ancestor::header | ancestor::nav | ancestor::aside | " +
-                                        "ancestor::footer | ancestor::script | ancestor::style)]" +
+                # text_nodes = tree.xpath("//h1/text() | //h2/text() | //h3/text() | "
+                #    "//p/text() | //div/text() | //span/text() | //a/text()")
+                text_nodes = tree.xpath("//text()[not(ancestor::header | ancestor::nav | "
+                                        "ancestor::aside | ancestor::footer | "
+                                        "ancestor::script | ancestor::style)]" +
                                         "[ancestor::body]")
                 text = ''
                 for text_node in text_nodes:
@@ -280,15 +296,16 @@ class PreviewUrlResource(BaseMediaResource):
                 text = text.strip()[:500]
                 og['og:description'] = text if text else None
 
-        # TODO: delete the url downloads to stop diskfilling, as we only ever cared about its OG
-        defer.returnValue(og);
+        # TODO: delete the url downloads to stop diskfilling,
+        # as we only ever cared about its OG
+        defer.returnValue(og)
 
     def _rebase_url(self, url, base):
         base = list(urlparse(base))
         url = list(urlparse(url))
-        if not url[0]: # fix up schema
+        if not url[0]:  # fix up schema
             url[0] = base[0] or "http"
-        if not url[1]: # fix up hostname
+        if not url[1]:  # fix up hostname
             url[1] = base[1]
             if not url[2].startswith('/'):
                 url[2] = re.sub(r'/[^/]+$', '/', base[2]) + url[2]
@@ -377,6 +394,8 @@ class PreviewUrlResource(BaseMediaResource):
 
     def _is_html(self, content_type):
         content_type = content_type.lower()
-        if (content_type.startswith("text/html") or
-            content_type.startswith("application/xhtml")):
+        if (
+            content_type.startswith("text/html") or
+            content_type.startswith("application/xhtml")
+        ):
             return True