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.
 
 
 
 
 
 

861 lines
33 KiB

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