You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

863 lines
33 KiB

  1. # Copyright 2016 OpenMarket Ltd
  2. # Copyright 2020-2021 The Matrix.org Foundation C.I.C.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import datetime
  16. import errno
  17. import fnmatch
  18. import logging
  19. import os
  20. import re
  21. import shutil
  22. import sys
  23. import traceback
  24. from typing import TYPE_CHECKING, BinaryIO, Iterable, Optional, Tuple
  25. from urllib.parse import urljoin, urlparse, urlsplit
  26. from urllib.request import urlopen
  27. import attr
  28. from twisted.internet.defer import Deferred
  29. from twisted.internet.error import DNSLookupError
  30. from synapse.api.errors import Codes, SynapseError
  31. from synapse.http.client import SimpleHttpClient
  32. from synapse.http.server import (
  33. DirectServeJsonResource,
  34. respond_with_json,
  35. respond_with_json_bytes,
  36. )
  37. from synapse.http.servlet import parse_integer, parse_string
  38. from synapse.http.site import SynapseRequest
  39. from synapse.logging.context import make_deferred_yieldable, run_in_background
  40. from synapse.metrics.background_process_metrics import run_as_background_process
  41. from synapse.rest.media.v1._base import get_filename_from_headers
  42. from synapse.rest.media.v1.media_storage import MediaStorage
  43. from synapse.rest.media.v1.oembed import OEmbedProvider
  44. from synapse.rest.media.v1.preview_html import decode_body, parse_html_to_open_graph
  45. from synapse.types import JsonDict, UserID
  46. from synapse.util import json_encoder
  47. from synapse.util.async_helpers import ObservableDeferred
  48. from synapse.util.caches.expiringcache import ExpiringCache
  49. from synapse.util.stringutils import random_string
  50. from ._base import FileInfo
  51. if TYPE_CHECKING:
  52. from synapse.rest.media.v1.media_repository import MediaRepository
  53. from synapse.server import HomeServer
  54. logger = logging.getLogger(__name__)
  55. OG_TAG_NAME_MAXLEN = 50
  56. OG_TAG_VALUE_MAXLEN = 1000
  57. ONE_HOUR = 60 * 60 * 1000
  58. ONE_DAY = 24 * ONE_HOUR
  59. IMAGE_CACHE_EXPIRY_MS = 2 * ONE_DAY
  60. @attr.s(slots=True, frozen=True, auto_attribs=True)
  61. class DownloadResult:
  62. length: int
  63. uri: str
  64. response_code: int
  65. media_type: str
  66. download_name: Optional[str]
  67. expires: int
  68. etag: Optional[str]
  69. @attr.s(slots=True, frozen=True, auto_attribs=True)
  70. class MediaInfo:
  71. """
  72. Information parsed from downloading media being previewed.
  73. """
  74. # The Content-Type header of the response.
  75. media_type: str
  76. # The length (in bytes) of the downloaded media.
  77. media_length: int
  78. # The media filename, according to the server. This is parsed from the
  79. # returned headers, if possible.
  80. download_name: Optional[str]
  81. # The time of the preview.
  82. created_ts_ms: int
  83. # Information from the media storage provider about where the file is stored
  84. # on disk.
  85. filesystem_id: str
  86. filename: str
  87. # The URI being previewed.
  88. uri: str
  89. # The HTTP response code.
  90. response_code: int
  91. # The timestamp (in milliseconds) of when this preview expires.
  92. expires: int
  93. # The ETag header of the response.
  94. etag: Optional[str]
  95. class PreviewUrlResource(DirectServeJsonResource):
  96. """
  97. The `GET /_matrix/media/r0/preview_url` endpoint provides a generic preview API
  98. for URLs which outputs Open Graph (https://ogp.me/) responses (with some Matrix
  99. specific additions).
  100. This does have trade-offs compared to other designs:
  101. * Pros:
  102. * Simple and flexible; can be used by any clients at any point
  103. * Cons:
  104. * If each homeserver provides one of these independently, all the homeservers in a
  105. room may needlessly DoS the target URI
  106. * The URL metadata must be stored somewhere, rather than just using Matrix
  107. itself to store the media.
  108. * Matrix cannot be used to distribute the metadata between homeservers.
  109. When Synapse is asked to preview a URL it does the following:
  110. 1. Checks against a URL blacklist (defined as `url_preview_url_blacklist` in the
  111. config).
  112. 2. Checks the URL against an in-memory cache and returns the result if it exists. (This
  113. is also used to de-duplicate processing of multiple in-flight requests at once.)
  114. 3. Kicks off a background process to generate a preview:
  115. 1. Checks URL and timestamp against the database cache and returns the result if it
  116. has not expired and was successful (a 2xx return code).
  117. 2. Checks if the URL matches an oEmbed (https://oembed.com/) pattern. If it
  118. does, update the URL to download.
  119. 3. Downloads the URL and stores it into a file via the media storage provider
  120. and saves the local media metadata.
  121. 4. If the media is an image:
  122. 1. Generates thumbnails.
  123. 2. Generates an Open Graph response based on image properties.
  124. 5. If the media is HTML:
  125. 1. Decodes the HTML via the stored file.
  126. 2. Generates an Open Graph response from the HTML.
  127. 3. If a JSON oEmbed URL was found in the HTML via autodiscovery:
  128. 1. Downloads the URL and stores it into a file via the media storage provider
  129. and saves the local media metadata.
  130. 2. Convert the oEmbed response to an Open Graph response.
  131. 3. Override any Open Graph data from the HTML with data from oEmbed.
  132. 4. If an image exists in the Open Graph response:
  133. 1. Downloads the URL and stores it into a file via the media storage
  134. provider and saves the local media metadata.
  135. 2. Generates thumbnails.
  136. 3. Updates the Open Graph response based on image properties.
  137. 6. If the media is JSON and an oEmbed URL was found:
  138. 1. Convert the oEmbed response to an Open Graph response.
  139. 2. If a thumbnail or image is in the oEmbed response:
  140. 1. Downloads the URL and stores it into a file via the media storage
  141. provider and saves the local media metadata.
  142. 2. Generates thumbnails.
  143. 3. Updates the Open Graph response based on image properties.
  144. 7. Stores the result in the database cache.
  145. 4. Returns the result.
  146. The in-memory cache expires after 1 hour.
  147. Expired entries in the database cache (and their associated media files) are
  148. deleted every 10 seconds. The default expiration time is 1 hour from download.
  149. """
  150. isLeaf = True
  151. def __init__(
  152. self,
  153. hs: "HomeServer",
  154. media_repo: "MediaRepository",
  155. media_storage: MediaStorage,
  156. ):
  157. super().__init__()
  158. self.auth = hs.get_auth()
  159. self.clock = hs.get_clock()
  160. self.filepaths = media_repo.filepaths
  161. self.max_spider_size = hs.config.media.max_spider_size
  162. self.server_name = hs.hostname
  163. self.store = hs.get_datastores().main
  164. self.client = SimpleHttpClient(
  165. hs,
  166. treq_args={"browser_like_redirects": True},
  167. ip_whitelist=hs.config.media.url_preview_ip_range_whitelist,
  168. ip_blacklist=hs.config.media.url_preview_ip_range_blacklist,
  169. use_proxy=True,
  170. )
  171. self.media_repo = media_repo
  172. self.primary_base_path = media_repo.primary_base_path
  173. self.media_storage = media_storage
  174. self._oembed = OEmbedProvider(hs)
  175. # We run the background jobs if we're the instance specified (or no
  176. # instance is specified, where we assume there is only one instance
  177. # serving media).
  178. instance_running_jobs = hs.config.media.media_instance_running_background_jobs
  179. self._worker_run_media_background_jobs = (
  180. instance_running_jobs is None
  181. or instance_running_jobs == hs.get_instance_name()
  182. )
  183. self.url_preview_url_blacklist = hs.config.media.url_preview_url_blacklist
  184. self.url_preview_accept_language = hs.config.media.url_preview_accept_language
  185. # memory cache mapping urls to an ObservableDeferred returning
  186. # JSON-encoded OG metadata
  187. self._cache: ExpiringCache[str, ObservableDeferred] = ExpiringCache(
  188. cache_name="url_previews",
  189. clock=self.clock,
  190. # don't spider URLs more often than once an hour
  191. expiry_ms=ONE_HOUR,
  192. )
  193. if self._worker_run_media_background_jobs:
  194. self._cleaner_loop = self.clock.looping_call(
  195. self._start_expire_url_cache_data, 10 * 1000
  196. )
  197. async def _async_render_OPTIONS(self, request: SynapseRequest) -> None:
  198. request.setHeader(b"Allow", b"OPTIONS, GET")
  199. respond_with_json(request, 200, {}, send_cors=True)
  200. async def _async_render_GET(self, request: SynapseRequest) -> None:
  201. # XXX: if get_user_by_req fails, what should we do in an async render?
  202. requester = await self.auth.get_user_by_req(request)
  203. url = parse_string(request, "url", required=True)
  204. ts = parse_integer(request, "ts")
  205. if ts is None:
  206. ts = self.clock.time_msec()
  207. # XXX: we could move this into _do_preview if we wanted.
  208. url_tuple = urlsplit(url)
  209. for entry in self.url_preview_url_blacklist:
  210. match = True
  211. for attrib in entry:
  212. pattern = entry[attrib]
  213. value = getattr(url_tuple, attrib)
  214. logger.debug(
  215. "Matching attrib '%s' with value '%s' against pattern '%s'",
  216. attrib,
  217. value,
  218. pattern,
  219. )
  220. if value is None:
  221. match = False
  222. continue
  223. # Some attributes might not be parsed as strings by urlsplit (such as the
  224. # port, which is parsed as an int). Because we use match functions that
  225. # expect strings, we want to make sure that's what we give them.
  226. value_str = str(value)
  227. if pattern.startswith("^"):
  228. if not re.match(pattern, value_str):
  229. match = False
  230. continue
  231. else:
  232. if not fnmatch.fnmatch(value_str, pattern):
  233. match = False
  234. continue
  235. if match:
  236. logger.warning("URL %s blocked by url_blacklist entry %s", url, entry)
  237. raise SynapseError(
  238. 403, "URL blocked by url pattern blacklist entry", Codes.UNKNOWN
  239. )
  240. # the in-memory cache:
  241. # * ensures that only one request is active at a time
  242. # * takes load off the DB for the thundering herds
  243. # * also caches any failures (unlike the DB) so we don't keep
  244. # requesting the same endpoint
  245. observable = self._cache.get(url)
  246. if not observable:
  247. download = run_in_background(self._do_preview, url, requester.user, ts)
  248. observable = ObservableDeferred(download, consumeErrors=True)
  249. self._cache[url] = observable
  250. else:
  251. logger.info("Returning cached response")
  252. og = await make_deferred_yieldable(observable.observe())
  253. respond_with_json_bytes(request, 200, og, send_cors=True)
  254. async def _do_preview(self, url: str, user: UserID, ts: int) -> bytes:
  255. """Check the db, and download the URL and build a preview
  256. Args:
  257. url: The URL to preview.
  258. user: The user requesting the preview.
  259. ts: The timestamp requested for the preview.
  260. Returns:
  261. json-encoded og data
  262. """
  263. # check the URL cache in the DB (which will also provide us with
  264. # historical previews, if we have any)
  265. cache_result = await self.store.get_url_cache(url, ts)
  266. if (
  267. cache_result
  268. and cache_result["expires_ts"] > ts
  269. and cache_result["response_code"] / 100 == 2
  270. ):
  271. # It may be stored as text in the database, not as bytes (such as
  272. # PostgreSQL). If so, encode it back before handing it on.
  273. og = cache_result["og"]
  274. if isinstance(og, str):
  275. og = og.encode("utf8")
  276. return og
  277. # If this URL can be accessed via oEmbed, use that instead.
  278. url_to_download = url
  279. oembed_url = self._oembed.get_oembed_url(url)
  280. if oembed_url:
  281. url_to_download = oembed_url
  282. media_info = await self._handle_url(url_to_download, user)
  283. logger.debug("got media_info of '%s'", media_info)
  284. # The number of milliseconds that the response should be considered valid.
  285. expiration_ms = media_info.expires
  286. author_name: Optional[str] = None
  287. if _is_media(media_info.media_type):
  288. file_id = media_info.filesystem_id
  289. dims = await self.media_repo._generate_thumbnails(
  290. None, file_id, file_id, media_info.media_type, url_cache=True
  291. )
  292. og = {
  293. "og:description": media_info.download_name,
  294. "og:image": f"mxc://{self.server_name}/{media_info.filesystem_id}",
  295. "og:image:type": media_info.media_type,
  296. "matrix:image:size": media_info.media_length,
  297. }
  298. if dims:
  299. og["og:image:width"] = dims["width"]
  300. og["og:image:height"] = dims["height"]
  301. else:
  302. logger.warning("Couldn't get dims for %s" % url)
  303. # define our OG response for this media
  304. elif _is_html(media_info.media_type):
  305. # TODO: somehow stop a big HTML tree from exploding synapse's RAM
  306. with open(media_info.filename, "rb") as file:
  307. body = file.read()
  308. tree = decode_body(body, media_info.uri, media_info.media_type)
  309. if tree is not None:
  310. # Check if this HTML document points to oEmbed information and
  311. # defer to that.
  312. oembed_url = self._oembed.autodiscover_from_html(tree)
  313. og_from_oembed: JsonDict = {}
  314. if oembed_url:
  315. oembed_info = await self._handle_url(
  316. oembed_url, user, allow_data_urls=True
  317. )
  318. (
  319. og_from_oembed,
  320. author_name,
  321. expiration_ms,
  322. ) = await self._handle_oembed_response(
  323. url, oembed_info, expiration_ms
  324. )
  325. # Parse Open Graph information from the HTML in case the oEmbed
  326. # response failed or is incomplete.
  327. og_from_html = parse_html_to_open_graph(tree)
  328. # Compile the Open Graph response by using the scraped
  329. # information from the HTML and overlaying any information
  330. # from the oEmbed response.
  331. og = {**og_from_html, **og_from_oembed}
  332. await self._precache_image_url(user, media_info, og)
  333. else:
  334. og = {}
  335. elif oembed_url:
  336. # Handle the oEmbed information.
  337. og, author_name, expiration_ms = await self._handle_oembed_response(
  338. url, media_info, expiration_ms
  339. )
  340. await self._precache_image_url(user, media_info, og)
  341. else:
  342. logger.warning("Failed to find any OG data in %s", url)
  343. og = {}
  344. # If we don't have a title but we have author_name, copy it as
  345. # title
  346. if not og.get("og:title") and author_name:
  347. og["og:title"] = author_name
  348. # filter out any stupidly long values
  349. keys_to_remove = []
  350. for k, v in og.items():
  351. # values can be numeric as well as strings, hence the cast to str
  352. if len(k) > OG_TAG_NAME_MAXLEN or len(str(v)) > OG_TAG_VALUE_MAXLEN:
  353. logger.warning(
  354. "Pruning overlong tag %s from OG data", k[:OG_TAG_NAME_MAXLEN]
  355. )
  356. keys_to_remove.append(k)
  357. for k in keys_to_remove:
  358. del og[k]
  359. logger.debug("Calculated OG for %s as %s", url, og)
  360. jsonog = json_encoder.encode(og)
  361. # Cap the amount of time to consider a response valid.
  362. expiration_ms = min(expiration_ms, ONE_DAY)
  363. # store OG in history-aware DB cache
  364. await self.store.store_url_cache(
  365. url,
  366. media_info.response_code,
  367. media_info.etag,
  368. media_info.created_ts_ms + expiration_ms,
  369. jsonog,
  370. media_info.filesystem_id,
  371. media_info.created_ts_ms,
  372. )
  373. return jsonog.encode("utf8")
  374. async def _download_url(self, url: str, output_stream: BinaryIO) -> DownloadResult:
  375. """
  376. Fetches a remote URL and parses the headers.
  377. Args:
  378. url: The URL to fetch.
  379. output_stream: The stream to write the content to.
  380. Returns:
  381. A tuple of:
  382. Media length, URL downloaded, the HTTP response code,
  383. the media type, the downloaded file name, the number of
  384. milliseconds the result is valid for, the etag header.
  385. """
  386. try:
  387. logger.debug("Trying to get preview for url '%s'", url)
  388. length, headers, uri, code = await self.client.get_file(
  389. url,
  390. output_stream=output_stream,
  391. max_size=self.max_spider_size,
  392. headers={
  393. b"Accept-Language": self.url_preview_accept_language,
  394. # Use a custom user agent for the preview because some sites will only return
  395. # Open Graph metadata to crawler user agents. Omit the Synapse version
  396. # string to avoid leaking information.
  397. b"User-Agent": [
  398. "Synapse (bot; +https://github.com/matrix-org/synapse)"
  399. ],
  400. },
  401. is_allowed_content_type=_is_previewable,
  402. )
  403. except SynapseError:
  404. # Pass SynapseErrors through directly, so that the servlet
  405. # handler will return a SynapseError to the client instead of
  406. # blank data or a 500.
  407. raise
  408. except DNSLookupError:
  409. # DNS lookup returned no results
  410. # Note: This will also be the case if one of the resolved IP
  411. # addresses is blacklisted
  412. raise SynapseError(
  413. 502,
  414. "DNS resolution failure during URL preview generation",
  415. Codes.UNKNOWN,
  416. )
  417. except Exception as e:
  418. # FIXME: pass through 404s and other error messages nicely
  419. logger.warning("Error downloading %s: %r", url, e)
  420. raise SynapseError(
  421. 500,
  422. "Failed to download content: %s"
  423. % (traceback.format_exception_only(sys.exc_info()[0], e),),
  424. Codes.UNKNOWN,
  425. )
  426. if b"Content-Type" in headers:
  427. media_type = headers[b"Content-Type"][0].decode("ascii")
  428. else:
  429. media_type = "application/octet-stream"
  430. download_name = get_filename_from_headers(headers)
  431. # FIXME: we should calculate a proper expiration based on the
  432. # Cache-Control and Expire headers. But for now, assume 1 hour.
  433. expires = ONE_HOUR
  434. etag = headers[b"ETag"][0].decode("ascii") if b"ETag" in headers else None
  435. return DownloadResult(
  436. length, uri, code, media_type, download_name, expires, etag
  437. )
  438. async def _parse_data_url(
  439. self, url: str, output_stream: BinaryIO
  440. ) -> DownloadResult:
  441. """
  442. Parses a data: URL.
  443. Args:
  444. url: The URL to parse.
  445. output_stream: The stream to write the content to.
  446. Returns:
  447. A tuple of:
  448. Media length, URL downloaded, the HTTP response code,
  449. the media type, the downloaded file name, the number of
  450. milliseconds the result is valid for, the etag header.
  451. """
  452. try:
  453. logger.debug("Trying to parse data url '%s'", url)
  454. with urlopen(url) as url_info:
  455. # TODO Can this be more efficient.
  456. output_stream.write(url_info.read())
  457. except Exception as e:
  458. logger.warning("Error parsing data: URL %s: %r", url, e)
  459. raise SynapseError(
  460. 500,
  461. "Failed to parse data URL: %s"
  462. % (traceback.format_exception_only(sys.exc_info()[0], e),),
  463. Codes.UNKNOWN,
  464. )
  465. return DownloadResult(
  466. # Read back the length that has been written.
  467. length=output_stream.tell(),
  468. uri=url,
  469. # If it was parsed, consider this a 200 OK.
  470. response_code=200,
  471. # urlopen shoves the media-type from the data URL into the content type
  472. # header object.
  473. media_type=url_info.headers.get_content_type(),
  474. # Some features are not supported by data: URLs.
  475. download_name=None,
  476. expires=ONE_HOUR,
  477. etag=None,
  478. )
  479. async def _handle_url(
  480. self, url: str, user: UserID, allow_data_urls: bool = False
  481. ) -> MediaInfo:
  482. """
  483. Fetches content from a URL and parses the result to generate a MediaInfo.
  484. It uses the media storage provider to persist the fetched content and
  485. stores the mapping into the database.
  486. Args:
  487. url: The URL to fetch.
  488. user: The user who ahs requested this URL.
  489. allow_data_urls: True if data URLs should be allowed.
  490. Returns:
  491. A MediaInfo object describing the fetched content.
  492. """
  493. # TODO: we should probably honour robots.txt... except in practice
  494. # we're most likely being explicitly triggered by a human rather than a
  495. # bot, so are we really a robot?
  496. file_id = datetime.date.today().isoformat() + "_" + random_string(16)
  497. file_info = FileInfo(server_name=None, file_id=file_id, url_cache=True)
  498. with self.media_storage.store_into_file(file_info) as (f, fname, finish):
  499. if url.startswith("data:"):
  500. if not allow_data_urls:
  501. raise SynapseError(
  502. 500, "Previewing of data: URLs is forbidden", Codes.UNKNOWN
  503. )
  504. download_result = await self._parse_data_url(url, f)
  505. else:
  506. download_result = await self._download_url(url, f)
  507. await finish()
  508. try:
  509. time_now_ms = self.clock.time_msec()
  510. await self.store.store_local_media(
  511. media_id=file_id,
  512. media_type=download_result.media_type,
  513. time_now_ms=time_now_ms,
  514. upload_name=download_result.download_name,
  515. media_length=download_result.length,
  516. user_id=user,
  517. url_cache=url,
  518. )
  519. except Exception as e:
  520. logger.error("Error handling downloaded %s: %r", url, e)
  521. # TODO: we really ought to delete the downloaded file in this
  522. # case, since we won't have recorded it in the db, and will
  523. # therefore not expire it.
  524. raise
  525. return MediaInfo(
  526. media_type=download_result.media_type,
  527. media_length=download_result.length,
  528. download_name=download_result.download_name,
  529. created_ts_ms=time_now_ms,
  530. filesystem_id=file_id,
  531. filename=fname,
  532. uri=download_result.uri,
  533. response_code=download_result.response_code,
  534. expires=download_result.expires,
  535. etag=download_result.etag,
  536. )
  537. async def _precache_image_url(
  538. self, user: UserID, media_info: MediaInfo, og: JsonDict
  539. ) -> None:
  540. """
  541. Pre-cache the image (if one exists) for posterity
  542. Args:
  543. user: The user requesting the preview.
  544. media_info: The media being previewed.
  545. og: The Open Graph dictionary. This is modified with image information.
  546. """
  547. # If there's no image or it is blank, there's nothing to do.
  548. if "og:image" not in og:
  549. return
  550. # Remove the raw image URL, this will be replaced with an MXC URL, if successful.
  551. image_url = og.pop("og:image")
  552. if not image_url:
  553. return
  554. # The image URL from the HTML might be relative to the previewed page,
  555. # convert it to an URL which can be requested directly.
  556. url_parts = urlparse(image_url)
  557. if url_parts.scheme != "data":
  558. image_url = urljoin(media_info.uri, image_url)
  559. # FIXME: it might be cleaner to use the same flow as the main /preview_url
  560. # request itself and benefit from the same caching etc. But for now we
  561. # just rely on the caching on the master request to speed things up.
  562. try:
  563. image_info = await self._handle_url(image_url, user, allow_data_urls=True)
  564. except Exception as e:
  565. # Pre-caching the image failed, don't block the entire URL preview.
  566. logger.warning(
  567. "Pre-caching image failed during URL preview: %s errored with %s",
  568. image_url,
  569. e,
  570. )
  571. return
  572. if _is_media(image_info.media_type):
  573. # TODO: make sure we don't choke on white-on-transparent images
  574. file_id = image_info.filesystem_id
  575. dims = await self.media_repo._generate_thumbnails(
  576. None, file_id, file_id, image_info.media_type, url_cache=True
  577. )
  578. if dims:
  579. og["og:image:width"] = dims["width"]
  580. og["og:image:height"] = dims["height"]
  581. else:
  582. logger.warning("Couldn't get dims for %s", image_url)
  583. og["og:image"] = f"mxc://{self.server_name}/{image_info.filesystem_id}"
  584. og["og:image:type"] = image_info.media_type
  585. og["matrix:image:size"] = image_info.media_length
  586. async def _handle_oembed_response(
  587. self, url: str, media_info: MediaInfo, expiration_ms: int
  588. ) -> Tuple[JsonDict, Optional[str], int]:
  589. """
  590. Parse the downloaded oEmbed info.
  591. Args:
  592. url: The URL which is being previewed (not the one which was
  593. requested).
  594. media_info: The media being previewed.
  595. expiration_ms: The length of time, in milliseconds, the media is valid for.
  596. Returns:
  597. A tuple of:
  598. The Open Graph dictionary, if the oEmbed info can be parsed.
  599. The author name if it could be retrieved from oEmbed.
  600. The (possibly updated) length of time, in milliseconds, the media is valid for.
  601. """
  602. # If JSON was not returned, there's nothing to do.
  603. if not _is_json(media_info.media_type):
  604. return {}, None, expiration_ms
  605. with open(media_info.filename, "rb") as file:
  606. body = file.read()
  607. oembed_response = self._oembed.parse_oembed_response(url, body)
  608. open_graph_result = oembed_response.open_graph_result
  609. # Use the cache age from the oEmbed result, if one was given.
  610. if open_graph_result and oembed_response.cache_age is not None:
  611. expiration_ms = oembed_response.cache_age
  612. return open_graph_result, oembed_response.author_name, expiration_ms
  613. def _start_expire_url_cache_data(self) -> Deferred:
  614. return run_as_background_process(
  615. "expire_url_cache_data", self._expire_url_cache_data
  616. )
  617. async def _expire_url_cache_data(self) -> None:
  618. """Clean up expired url cache content, media and thumbnails."""
  619. assert self._worker_run_media_background_jobs
  620. now = self.clock.time_msec()
  621. logger.debug("Running url preview cache expiry")
  622. if not (await self.store.db_pool.updates.has_completed_background_updates()):
  623. logger.debug("Still running DB updates; skipping url preview cache expiry")
  624. return
  625. def try_remove_parent_dirs(dirs: Iterable[str]) -> None:
  626. """Attempt to remove the given chain of parent directories
  627. Args:
  628. dirs: The list of directory paths to delete, with children appearing
  629. before their parents.
  630. """
  631. for dir in dirs:
  632. try:
  633. os.rmdir(dir)
  634. except FileNotFoundError:
  635. # Already deleted, continue with deleting the rest
  636. pass
  637. except OSError as e:
  638. # Failed, skip deleting the rest of the parent dirs
  639. if e.errno != errno.ENOTEMPTY:
  640. logger.warning(
  641. "Failed to remove media directory while clearing url preview cache: %r: %s",
  642. dir,
  643. e,
  644. )
  645. break
  646. # First we delete expired url cache entries
  647. media_ids = await self.store.get_expired_url_cache(now)
  648. removed_media = []
  649. for media_id in media_ids:
  650. fname = self.filepaths.url_cache_filepath(media_id)
  651. try:
  652. os.remove(fname)
  653. except FileNotFoundError:
  654. pass # If the path doesn't exist, meh
  655. except OSError as e:
  656. logger.warning(
  657. "Failed to remove media while clearing url preview cache: %r: %s",
  658. media_id,
  659. e,
  660. )
  661. continue
  662. removed_media.append(media_id)
  663. dirs = self.filepaths.url_cache_filepath_dirs_to_delete(media_id)
  664. try_remove_parent_dirs(dirs)
  665. await self.store.delete_url_cache(removed_media)
  666. if removed_media:
  667. logger.debug(
  668. "Deleted %d entries from url preview cache", len(removed_media)
  669. )
  670. else:
  671. logger.debug("No entries removed from url preview cache")
  672. # Now we delete old images associated with the url cache.
  673. # These may be cached for a bit on the client (i.e., they
  674. # may have a room open with a preview url thing open).
  675. # So we wait a couple of days before deleting, just in case.
  676. expire_before = now - IMAGE_CACHE_EXPIRY_MS
  677. media_ids = await self.store.get_url_cache_media_before(expire_before)
  678. removed_media = []
  679. for media_id in media_ids:
  680. fname = self.filepaths.url_cache_filepath(media_id)
  681. try:
  682. os.remove(fname)
  683. except FileNotFoundError:
  684. pass # If the path doesn't exist, meh
  685. except OSError as e:
  686. logger.warning(
  687. "Failed to remove media from url preview cache: %r: %s", media_id, e
  688. )
  689. continue
  690. dirs = self.filepaths.url_cache_filepath_dirs_to_delete(media_id)
  691. try_remove_parent_dirs(dirs)
  692. thumbnail_dir = self.filepaths.url_cache_thumbnail_directory(media_id)
  693. try:
  694. shutil.rmtree(thumbnail_dir)
  695. except FileNotFoundError:
  696. pass # If the path doesn't exist, meh
  697. except OSError as e:
  698. logger.warning(
  699. "Failed to remove media from url preview cache: %r: %s", media_id, e
  700. )
  701. continue
  702. removed_media.append(media_id)
  703. dirs = self.filepaths.url_cache_thumbnail_dirs_to_delete(media_id)
  704. # Note that one of the directories to be deleted has already been
  705. # removed by the `rmtree` above.
  706. try_remove_parent_dirs(dirs)
  707. await self.store.delete_url_cache_media(removed_media)
  708. if removed_media:
  709. logger.debug("Deleted %d media from url preview cache", len(removed_media))
  710. else:
  711. logger.debug("No media removed from url preview cache")
  712. def _is_media(content_type: str) -> bool:
  713. return content_type.lower().startswith("image/")
  714. def _is_html(content_type: str) -> bool:
  715. content_type = content_type.lower()
  716. return content_type.startswith("text/html") or content_type.startswith(
  717. "application/xhtml"
  718. )
  719. def _is_json(content_type: str) -> bool:
  720. return content_type.lower().startswith("application/json")
  721. def _is_previewable(content_type: str) -> bool:
  722. """Returns True for content types for which we will perform URL preview and False
  723. otherwise."""
  724. return _is_html(content_type) or _is_media(content_type) or _is_json(content_type)