2016-01-25 00:47:27 +01:00
|
|
|
# Copyright 2016 OpenMarket Ltd
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2016-03-29 04:13:25 +02:00
|
|
|
from .base_resource import BaseMediaResource
|
|
|
|
from synapse.api.errors import Codes
|
2016-01-25 00:47:27 +01:00
|
|
|
from twisted.web.resource import Resource
|
2016-03-29 04:13:25 +02:00
|
|
|
from twisted.web.server import NOT_DONE_YET
|
2016-03-29 03:07:57 +02:00
|
|
|
from twisted.internet import defer
|
2016-01-25 00:47:27 +01:00
|
|
|
from lxml import html
|
2016-04-01 00:52:58 +02:00
|
|
|
from urlparse import urlparse, urlunparse
|
2016-03-29 04:13:25 +02:00
|
|
|
from synapse.util.stringutils import random_string
|
2016-03-31 02:55:21 +02:00
|
|
|
from synapse.http.client import SpiderHttpClient
|
2016-03-29 04:13:25 +02:00
|
|
|
from synapse.http.server import request_handler, respond_with_json, respond_with_json_bytes
|
|
|
|
|
|
|
|
import os
|
2016-03-31 15:15:09 +02:00
|
|
|
import re
|
2016-03-29 03:07:57 +02:00
|
|
|
import ujson as json
|
2016-01-25 00:47:27 +01:00
|
|
|
|
|
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2016-03-29 04:13:25 +02:00
|
|
|
class PreviewUrlResource(BaseMediaResource):
|
2016-01-25 00:47:27 +01:00
|
|
|
isLeaf = True
|
|
|
|
|
|
|
|
def __init__(self, hs, filepaths):
|
2016-03-29 04:13:25 +02:00
|
|
|
BaseMediaResource.__init__(self, hs, filepaths)
|
2016-03-31 02:55:21 +02:00
|
|
|
self.client = SpiderHttpClient(hs)
|
2016-01-25 00:47:27 +01:00
|
|
|
|
|
|
|
def render_GET(self, request):
|
|
|
|
self._async_render_GET(request)
|
|
|
|
return NOT_DONE_YET
|
|
|
|
|
|
|
|
@request_handler
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _async_render_GET(self, request):
|
|
|
|
|
|
|
|
try:
|
2016-03-29 04:13:25 +02:00
|
|
|
# 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]
|
|
|
|
|
2016-01-25 00:47:27 +01:00
|
|
|
# TODO: keep track of whether there's an ongoing request for this preview
|
|
|
|
# and block and return their details if there is one.
|
|
|
|
|
2016-03-29 04:13:25 +02:00
|
|
|
media_info = yield self._download_url(url, requester.user)
|
|
|
|
|
2016-03-29 04:14:16 +02:00
|
|
|
logger.debug("got media_info of '%s'" % media_info)
|
2016-03-29 04:13:25 +02:00
|
|
|
|
|
|
|
if self._is_media(media_info['media_type']):
|
|
|
|
dims = yield self._generate_local_thumbnails(
|
2016-03-29 04:32:55 +02:00
|
|
|
media_info['filesystem_id'], media_info
|
2016-03-29 04:13:25 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
og = {
|
2016-03-29 04:32:55 +02:00
|
|
|
"og:description" : media_info['download_name'],
|
|
|
|
"og:image" : "mxc://%s/%s" % (self.server_name, media_info['filesystem_id']),
|
2016-03-29 04:13:25 +02:00
|
|
|
"og:image:type" : media_info['media_type'],
|
2016-03-29 04:32:55 +02:00
|
|
|
"og:image:width" : dims['width'],
|
|
|
|
"og:image:height" : dims['height'],
|
2016-03-29 04:13:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# define our OG response for this media
|
|
|
|
elif self._is_html(media_info['media_type']):
|
2016-03-31 15:15:09 +02:00
|
|
|
# TODO: somehow stop a big HTML tree from exploding synapse's RAM
|
2016-03-31 16:14:14 +02:00
|
|
|
|
|
|
|
# XXX: can't work out how to make lxml ignore UTF8 decoding errors
|
|
|
|
# so slurp as a string at this point.
|
|
|
|
file = open(media_info['filename'])
|
|
|
|
body = file.read()
|
|
|
|
file.close()
|
|
|
|
# FIXME: we shouldn't be forcing utf-8 if the page isn't actually utf-8...
|
|
|
|
tree = html.fromstring(body.decode('utf-8','ignore'))
|
|
|
|
# tree = html.parse(media_info['filename'])
|
2016-03-29 04:13:25 +02:00
|
|
|
|
|
|
|
# suck it up into lxml and define our OG response.
|
|
|
|
# if we see any 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"
|
2016-03-31 15:15:09 +02:00
|
|
|
|
|
|
|
# or:
|
|
|
|
|
|
|
|
# "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: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",
|
2016-03-29 04:13:25 +02:00
|
|
|
|
|
|
|
og = {}
|
|
|
|
for tag in tree.xpath("//*/meta[starts-with(@property, 'og:')]"):
|
|
|
|
og[tag.attrib['property']] = tag.attrib['content']
|
|
|
|
|
2016-03-31 15:15:09 +02:00
|
|
|
if not 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 if title else None
|
|
|
|
|
2016-03-31 16:14:14 +02:00
|
|
|
images = [ i for i in tree.xpath("//img") if 'src' in i.attrib ]
|
2016-03-31 15:15:09 +02:00
|
|
|
big_images = [ i for i in images if (
|
2016-03-31 16:14:14 +02:00
|
|
|
'width' in i.attrib and 'height' in i.attrib and
|
2016-03-31 15:15:09 +02:00
|
|
|
i.attrib['width'] > 64 and i.attrib['height'] > 64
|
2016-03-31 16:14:14 +02:00
|
|
|
)]
|
|
|
|
big_images = big_images.sort(key=lambda i: (-1 * int(i.attrib['width']) * int(i.attrib['height'])))
|
|
|
|
images = big_images if big_images else images
|
|
|
|
|
|
|
|
if images:
|
2016-04-01 00:52:58 +02:00
|
|
|
base = list(urlparse(media_info['uri']))
|
|
|
|
src = list(urlparse(images[0].attrib['src']))
|
|
|
|
if not src[0] and not src[1]:
|
|
|
|
src[0] = base[0]
|
|
|
|
src[1] = base[1]
|
|
|
|
if not src[2].startswith('/'):
|
|
|
|
src[2] = re.sub(r'/[^/]+$', '/', base[2]) + src[2]
|
|
|
|
og['og:image'] = urlunparse(src)
|
2016-03-31 15:15:09 +02:00
|
|
|
|
|
|
|
text_nodes = tree.xpath("//h1/text() | //h2/text() | //h3/text() | //p/text() | //div/text() | //span/text() | //a/text()")
|
2016-03-31 16:14:14 +02:00
|
|
|
# text_nodes = tree.xpath("//h1/text() | //h2/text() | //h3/text() | //p/text() | //div/text()")
|
2016-03-31 15:15:09 +02:00
|
|
|
text = ''
|
|
|
|
for text_node in text_nodes:
|
|
|
|
if len(text) < 1024:
|
|
|
|
text += text_node + ' '
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
text = re.sub(r'[\t ]+', ' ', text)
|
|
|
|
text = re.sub(r'[\t \r\n]*[\r\n]+', '\n', text)
|
|
|
|
text = text.strip()[:1024]
|
|
|
|
og['og:description'] = text if text else None
|
|
|
|
|
2016-04-01 00:52:58 +02:00
|
|
|
# TODO: extract a favicon?
|
2016-03-31 15:15:09 +02:00
|
|
|
# TODO: turn any OG media URLs into mxc URLs to capture and thumbnail them too
|
2016-03-29 04:13:25 +02:00
|
|
|
# TODO: store our OG details in a cache (and expire them when stale)
|
|
|
|
# TODO: delete the content to stop diskfilling, as we only ever cared about its OG
|
|
|
|
else:
|
|
|
|
logger.warn("Failed to find any OG data in %s", url)
|
|
|
|
og = {}
|
|
|
|
|
2016-03-31 15:15:09 +02:00
|
|
|
logger.warn(og)
|
|
|
|
|
2016-03-29 04:13:25 +02:00
|
|
|
respond_with_json_bytes(request, 200, json.dumps(og), send_cors=True)
|
2016-01-25 00:47:27 +01:00
|
|
|
except:
|
2016-03-29 04:13:25 +02:00
|
|
|
# XXX: if we don't explicitly respond here, the request never returns.
|
|
|
|
# isn't this what server.py's wrapper is meant to be doing for us?
|
|
|
|
respond_with_json(
|
|
|
|
request,
|
|
|
|
500,
|
|
|
|
{
|
|
|
|
"error": "Internal server error",
|
|
|
|
"errcode": Codes.UNKNOWN,
|
|
|
|
},
|
|
|
|
send_cors=True
|
|
|
|
)
|
2016-01-25 00:47:27 +01:00
|
|
|
raise
|
|
|
|
|
|
|
|
|
2016-03-29 04:13:25 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _download_url(self, url, user):
|
2016-03-31 15:15:09 +02:00
|
|
|
# TODO: we should probably honour robots.txt... except in practice
|
|
|
|
# we're most likely being explicitly triggered by a human rather than a
|
|
|
|
# bot, so are we really a robot?
|
|
|
|
|
2016-01-25 00:47:27 +01:00
|
|
|
# XXX: horrible duplication with base_resource's _download_remote_file()
|
|
|
|
file_id = random_string(24)
|
|
|
|
|
|
|
|
fname = self.filepaths.local_media_filepath(file_id)
|
|
|
|
self._makedirs(fname)
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(fname, "wb") as f:
|
2016-03-29 04:14:16 +02:00
|
|
|
logger.debug("Trying to get url '%s'" % url)
|
2016-04-01 00:52:58 +02:00
|
|
|
length, headers, uri = yield self.client.get_file(
|
2016-01-25 00:47:27 +01:00
|
|
|
url, output_stream=f, max_size=self.max_spider_size,
|
|
|
|
)
|
2016-03-29 04:32:55 +02:00
|
|
|
# FIXME: handle 404s sanely - don't spider an error page
|
2016-01-25 00:47:27 +01:00
|
|
|
media_type = headers["Content-Type"][0]
|
|
|
|
time_now_ms = self.clock.time_msec()
|
|
|
|
|
|
|
|
content_disposition = headers.get("Content-Disposition", None)
|
|
|
|
if content_disposition:
|
|
|
|
_, params = cgi.parse_header(content_disposition[0],)
|
|
|
|
download_name = None
|
|
|
|
|
|
|
|
# First check if there is a valid UTF-8 filename
|
|
|
|
download_name_utf8 = params.get("filename*", None)
|
|
|
|
if download_name_utf8:
|
|
|
|
if download_name_utf8.lower().startswith("utf-8''"):
|
|
|
|
download_name = download_name_utf8[7:]
|
|
|
|
|
|
|
|
# If there isn't check for an ascii name.
|
|
|
|
if not download_name:
|
|
|
|
download_name_ascii = params.get("filename", None)
|
|
|
|
if download_name_ascii and is_ascii(download_name_ascii):
|
|
|
|
download_name = download_name_ascii
|
|
|
|
|
|
|
|
if download_name:
|
|
|
|
download_name = urlparse.unquote(download_name)
|
|
|
|
try:
|
|
|
|
download_name = download_name.decode("utf-8")
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
download_name = None
|
|
|
|
else:
|
|
|
|
download_name = None
|
|
|
|
|
|
|
|
yield self.store.store_local_media(
|
|
|
|
media_id=fname,
|
|
|
|
media_type=media_type,
|
|
|
|
time_now_ms=self.clock.time_msec(),
|
|
|
|
upload_name=download_name,
|
|
|
|
media_length=length,
|
2016-03-29 04:13:25 +02:00
|
|
|
user_id=user,
|
2016-01-25 00:47:27 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
except:
|
|
|
|
os.remove(fname)
|
|
|
|
raise
|
|
|
|
|
2016-03-29 04:13:25 +02:00
|
|
|
defer.returnValue({
|
2016-01-25 00:47:27 +01:00
|
|
|
"media_type": media_type,
|
|
|
|
"media_length": length,
|
|
|
|
"download_name": download_name,
|
|
|
|
"created_ts": time_now_ms,
|
|
|
|
"filesystem_id": file_id,
|
|
|
|
"filename": fname,
|
2016-04-01 00:52:58 +02:00
|
|
|
"uri": uri,
|
2016-03-29 03:07:57 +02:00
|
|
|
})
|
2016-01-25 00:47:27 +01:00
|
|
|
|
2016-03-29 04:13:25 +02:00
|
|
|
def _is_media(self, content_type):
|
2016-01-25 00:47:27 +01:00
|
|
|
if content_type.lower().startswith("image/"):
|
|
|
|
return True
|
|
|
|
|
2016-03-29 04:13:25 +02:00
|
|
|
def _is_html(self, content_type):
|
2016-01-25 00:47:27 +01:00
|
|
|
content_type = content_type.lower()
|
2016-03-29 04:13:25 +02:00
|
|
|
if (content_type.startswith("text/html") or
|
2016-03-29 03:07:57 +02:00
|
|
|
content_type.startswith("application/xhtml")):
|
2016-01-25 00:47:27 +01:00
|
|
|
return True
|