Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

860 linhas
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. if isinstance(cache_result.og, str):
  217. return cache_result.og.encode("utf8")
  218. return cache_result.og
  219. # If this URL can be accessed via an allowed oEmbed, use that instead.
  220. url_to_download = url
  221. oembed_url = self._oembed.get_oembed_url(url)
  222. if oembed_url:
  223. url_to_download = oembed_url
  224. media_info = await self._handle_url(url_to_download, user)
  225. logger.debug("got media_info of '%s'", media_info)
  226. # The number of milliseconds that the response should be considered valid.
  227. expiration_ms = media_info.expires
  228. author_name: Optional[str] = None
  229. if _is_media(media_info.media_type):
  230. file_id = media_info.filesystem_id
  231. dims = await self.media_repo._generate_thumbnails(
  232. None, file_id, file_id, media_info.media_type, url_cache=True
  233. )
  234. og = {
  235. "og:description": media_info.download_name,
  236. "og:image": f"mxc://{self.server_name}/{media_info.filesystem_id}",
  237. "og:image:type": media_info.media_type,
  238. "matrix:image:size": media_info.media_length,
  239. }
  240. if dims:
  241. og["og:image:width"] = dims["width"]
  242. og["og:image:height"] = dims["height"]
  243. else:
  244. logger.warning("Couldn't get dims for %s" % url)
  245. # define our OG response for this media
  246. elif _is_html(media_info.media_type):
  247. # TODO: somehow stop a big HTML tree from exploding synapse's RAM
  248. with open(media_info.filename, "rb") as file:
  249. body = file.read()
  250. tree = decode_body(body, media_info.uri, media_info.media_type)
  251. if tree is not None:
  252. # Check if this HTML document points to oEmbed information and
  253. # defer to that.
  254. oembed_url = self._oembed.autodiscover_from_html(tree)
  255. og_from_oembed: JsonDict = {}
  256. # Only download to the oEmbed URL if it is allowed.
  257. if oembed_url:
  258. try:
  259. oembed_info = await self._handle_url(
  260. oembed_url, user, allow_data_urls=True
  261. )
  262. except Exception as e:
  263. # Fetching the oEmbed info failed, don't block the entire URL preview.
  264. logger.warning(
  265. "oEmbed fetch failed during URL preview: %s errored with %s",
  266. oembed_url,
  267. e,
  268. )
  269. else:
  270. (
  271. og_from_oembed,
  272. author_name,
  273. expiration_ms,
  274. ) = await self._handle_oembed_response(
  275. url, oembed_info, expiration_ms
  276. )
  277. # Parse Open Graph information from the HTML in case the oEmbed
  278. # response failed or is incomplete.
  279. og_from_html = parse_html_to_open_graph(tree)
  280. # Compile the Open Graph response by using the scraped
  281. # information from the HTML and overlaying any information
  282. # from the oEmbed response.
  283. og = {**og_from_html, **og_from_oembed}
  284. await self._precache_image_url(user, media_info, og)
  285. else:
  286. og = {}
  287. elif oembed_url:
  288. # Handle the oEmbed information.
  289. og, author_name, expiration_ms = await self._handle_oembed_response(
  290. url, media_info, expiration_ms
  291. )
  292. await self._precache_image_url(user, media_info, og)
  293. else:
  294. logger.warning("Failed to find any OG data in %s", url)
  295. og = {}
  296. # If we don't have a title but we have author_name, copy it as
  297. # title
  298. if not og.get("og:title") and author_name:
  299. og["og:title"] = author_name
  300. # filter out any stupidly long values
  301. keys_to_remove = []
  302. for k, v in og.items():
  303. # values can be numeric as well as strings, hence the cast to str
  304. if len(k) > OG_TAG_NAME_MAXLEN or len(str(v)) > OG_TAG_VALUE_MAXLEN:
  305. logger.warning(
  306. "Pruning overlong tag %s from OG data", k[:OG_TAG_NAME_MAXLEN]
  307. )
  308. keys_to_remove.append(k)
  309. for k in keys_to_remove:
  310. del og[k]
  311. logger.debug("Calculated OG for %s as %s", url, og)
  312. jsonog = json_encoder.encode(og)
  313. # Cap the amount of time to consider a response valid.
  314. expiration_ms = min(expiration_ms, ONE_DAY)
  315. # store OG in history-aware DB cache
  316. await self.store.store_url_cache(
  317. url,
  318. media_info.response_code,
  319. media_info.etag,
  320. media_info.created_ts_ms + expiration_ms,
  321. jsonog,
  322. media_info.filesystem_id,
  323. media_info.created_ts_ms,
  324. )
  325. return jsonog.encode("utf8")
  326. def _is_url_blocked(self, url: str) -> bool:
  327. """
  328. Check whether the URL is allowed to be previewed (according to the homeserver
  329. configuration).
  330. Args:
  331. url: The requested URL.
  332. Return:
  333. True if the URL is blocked, False if it is allowed.
  334. """
  335. url_tuple = urlsplit(url)
  336. for entry in self.url_preview_url_blocklist:
  337. match = True
  338. # Iterate over each entry. If *all* attributes of that entry match
  339. # the current URL, then reject it.
  340. for attrib, pattern in entry.items():
  341. value = getattr(url_tuple, attrib)
  342. logger.debug(
  343. "Matching attrib '%s' with value '%s' against pattern '%s'",
  344. attrib,
  345. value,
  346. pattern,
  347. )
  348. if value is None:
  349. match = False
  350. break
  351. # Some attributes might not be parsed as strings by urlsplit (such as the
  352. # port, which is parsed as an int). Because we use match functions that
  353. # expect strings, we want to make sure that's what we give them.
  354. value_str = str(value)
  355. # Check the value against the pattern as either a regular expression or
  356. # a glob. If it doesn't match, the entry doesn't match.
  357. if pattern.startswith("^"):
  358. if not re.match(pattern, value_str):
  359. match = False
  360. break
  361. else:
  362. if not fnmatch.fnmatch(value_str, pattern):
  363. match = False
  364. break
  365. # All fields matched, return true (the URL is blocked).
  366. if match:
  367. logger.warning("URL %s blocked by entry %s", url, entry)
  368. return match
  369. # No matches were found, the URL is allowed.
  370. return False
  371. async def _download_url(self, url: str, output_stream: BinaryIO) -> DownloadResult:
  372. """
  373. Fetches a remote URL and parses the headers.
  374. Args:
  375. url: The URL to fetch.
  376. output_stream: The stream to write the content to.
  377. Returns:
  378. A tuple of:
  379. Media length, URL downloaded, the HTTP response code,
  380. the media type, the downloaded file name, the number of
  381. milliseconds the result is valid for, the etag header.
  382. """
  383. try:
  384. logger.debug("Trying to get preview for url '%s'", url)
  385. length, headers, uri, code = await self.client.get_file(
  386. url,
  387. output_stream=output_stream,
  388. max_size=self.max_spider_size,
  389. headers={
  390. b"Accept-Language": self.url_preview_accept_language,
  391. # Use a custom user agent for the preview because some sites will only return
  392. # Open Graph metadata to crawler user agents. Omit the Synapse version
  393. # string to avoid leaking information.
  394. b"User-Agent": [
  395. "Synapse (bot; +https://github.com/matrix-org/synapse)"
  396. ],
  397. },
  398. is_allowed_content_type=_is_previewable,
  399. )
  400. except SynapseError:
  401. # Pass SynapseErrors through directly, so that the servlet
  402. # handler will return a SynapseError to the client instead of
  403. # blank data or a 500.
  404. raise
  405. except DNSLookupError:
  406. # DNS lookup returned no results
  407. # Note: This will also be the case if one of the resolved IP
  408. # addresses is blocked.
  409. raise SynapseError(
  410. 502,
  411. "DNS resolution failure during URL preview generation",
  412. Codes.UNKNOWN,
  413. )
  414. except Exception as e:
  415. # FIXME: pass through 404s and other error messages nicely
  416. logger.warning("Error downloading %s: %r", url, e)
  417. raise SynapseError(
  418. 500,
  419. "Failed to download content: %s"
  420. % (traceback.format_exception_only(sys.exc_info()[0], e),),
  421. Codes.UNKNOWN,
  422. )
  423. if b"Content-Type" in headers:
  424. media_type = headers[b"Content-Type"][0].decode("ascii")
  425. else:
  426. media_type = "application/octet-stream"
  427. download_name = get_filename_from_headers(headers)
  428. # FIXME: we should calculate a proper expiration based on the
  429. # Cache-Control and Expire headers. But for now, assume 1 hour.
  430. expires = ONE_HOUR
  431. etag = headers[b"ETag"][0].decode("ascii") if b"ETag" in headers else None
  432. return DownloadResult(
  433. length, uri, code, media_type, download_name, expires, etag
  434. )
  435. async def _parse_data_url(
  436. self, url: str, output_stream: BinaryIO
  437. ) -> DownloadResult:
  438. """
  439. Parses a data: URL.
  440. Args:
  441. url: The URL to parse.
  442. output_stream: The stream to write the content to.
  443. Returns:
  444. A tuple of:
  445. Media length, URL downloaded, the HTTP response code,
  446. the media type, the downloaded file name, the number of
  447. milliseconds the result is valid for, the etag header.
  448. """
  449. try:
  450. logger.debug("Trying to parse data url '%s'", url)
  451. with urlopen(url) as url_info:
  452. # TODO Can this be more efficient.
  453. output_stream.write(url_info.read())
  454. except Exception as e:
  455. logger.warning("Error parsing data: URL %s: %r", url, e)
  456. raise SynapseError(
  457. 500,
  458. "Failed to parse data URL: %s"
  459. % (traceback.format_exception_only(sys.exc_info()[0], e),),
  460. Codes.UNKNOWN,
  461. )
  462. return DownloadResult(
  463. # Read back the length that has been written.
  464. length=output_stream.tell(),
  465. uri=url,
  466. # If it was parsed, consider this a 200 OK.
  467. response_code=200,
  468. # urlopen shoves the media-type from the data URL into the content type
  469. # header object.
  470. media_type=url_info.headers.get_content_type(),
  471. # Some features are not supported by data: URLs.
  472. download_name=None,
  473. expires=ONE_HOUR,
  474. etag=None,
  475. )
  476. async def _handle_url(
  477. self, url: str, user: UserID, allow_data_urls: bool = False
  478. ) -> MediaInfo:
  479. """
  480. Fetches content from a URL and parses the result to generate a MediaInfo.
  481. It uses the media storage provider to persist the fetched content and
  482. stores the mapping into the database.
  483. Args:
  484. url: The URL to fetch.
  485. user: The user who ahs requested this URL.
  486. allow_data_urls: True if data URLs should be allowed.
  487. Returns:
  488. A MediaInfo object describing the fetched content.
  489. Raises:
  490. SynapseError if the URL is blocked.
  491. """
  492. if self._is_url_blocked(url):
  493. raise SynapseError(
  494. 403, "URL blocked by url pattern blocklist entry", Codes.UNKNOWN
  495. )
  496. # TODO: we should probably honour robots.txt... except in practice
  497. # we're most likely being explicitly triggered by a human rather than a
  498. # bot, so are we really a robot?
  499. file_id = datetime.date.today().isoformat() + "_" + random_string(16)
  500. file_info = FileInfo(server_name=None, file_id=file_id, url_cache=True)
  501. with self.media_storage.store_into_file(file_info) as (f, fname, finish):
  502. if url.startswith("data:"):
  503. if not allow_data_urls:
  504. raise SynapseError(
  505. 500, "Previewing of data: URLs is forbidden", Codes.UNKNOWN
  506. )
  507. download_result = await self._parse_data_url(url, f)
  508. else:
  509. download_result = await self._download_url(url, f)
  510. await finish()
  511. try:
  512. time_now_ms = self.clock.time_msec()
  513. await self.store.store_local_media(
  514. media_id=file_id,
  515. media_type=download_result.media_type,
  516. time_now_ms=time_now_ms,
  517. upload_name=download_result.download_name,
  518. media_length=download_result.length,
  519. user_id=user,
  520. url_cache=url,
  521. )
  522. except Exception as e:
  523. logger.error("Error handling downloaded %s: %r", url, e)
  524. # TODO: we really ought to delete the downloaded file in this
  525. # case, since we won't have recorded it in the db, and will
  526. # therefore not expire it.
  527. raise
  528. return MediaInfo(
  529. media_type=download_result.media_type,
  530. media_length=download_result.length,
  531. download_name=download_result.download_name,
  532. created_ts_ms=time_now_ms,
  533. filesystem_id=file_id,
  534. filename=fname,
  535. uri=download_result.uri,
  536. response_code=download_result.response_code,
  537. expires=download_result.expires,
  538. etag=download_result.etag,
  539. )
  540. async def _precache_image_url(
  541. self, user: UserID, media_info: MediaInfo, og: JsonDict
  542. ) -> None:
  543. """
  544. Pre-cache the image (if one exists) for posterity
  545. Args:
  546. user: The user requesting the preview.
  547. media_info: The media being previewed.
  548. og: The Open Graph dictionary. This is modified with image information.
  549. """
  550. # If there's no image or it is blank, there's nothing to do.
  551. if "og:image" not in og:
  552. return
  553. # Remove the raw image URL, this will be replaced with an MXC URL, if successful.
  554. image_url = og.pop("og:image")
  555. if not image_url:
  556. return
  557. # The image URL from the HTML might be relative to the previewed page,
  558. # convert it to a URL which can be requested directly.
  559. url_parts = urlparse(image_url)
  560. if url_parts.scheme != "data":
  561. image_url = urljoin(media_info.uri, image_url)
  562. # FIXME: it might be cleaner to use the same flow as the main /preview_url
  563. # request itself and benefit from the same caching etc. But for now we
  564. # just rely on the caching on the master request to speed things up.
  565. try:
  566. image_info = await self._handle_url(image_url, user, allow_data_urls=True)
  567. except Exception as e:
  568. # Pre-caching the image failed, don't block the entire URL preview.
  569. logger.warning(
  570. "Pre-caching image failed during URL preview: %s errored with %s",
  571. image_url,
  572. e,
  573. )
  574. return
  575. if _is_media(image_info.media_type):
  576. # TODO: make sure we don't choke on white-on-transparent images
  577. file_id = image_info.filesystem_id
  578. dims = await self.media_repo._generate_thumbnails(
  579. None, file_id, file_id, image_info.media_type, url_cache=True
  580. )
  581. if dims:
  582. og["og:image:width"] = dims["width"]
  583. og["og:image:height"] = dims["height"]
  584. else:
  585. logger.warning("Couldn't get dims for %s", image_url)
  586. og["og:image"] = f"mxc://{self.server_name}/{image_info.filesystem_id}"
  587. og["og:image:type"] = image_info.media_type
  588. og["matrix:image:size"] = image_info.media_length
  589. async def _handle_oembed_response(
  590. self, url: str, media_info: MediaInfo, expiration_ms: int
  591. ) -> Tuple[JsonDict, Optional[str], int]:
  592. """
  593. Parse the downloaded oEmbed info.
  594. Args:
  595. url: The URL which is being previewed (not the one which was
  596. requested).
  597. media_info: The media being previewed.
  598. expiration_ms: The length of time, in milliseconds, the media is valid for.
  599. Returns:
  600. A tuple of:
  601. The Open Graph dictionary, if the oEmbed info can be parsed.
  602. The author name if it could be retrieved from oEmbed.
  603. The (possibly updated) length of time, in milliseconds, the media is valid for.
  604. """
  605. # If JSON was not returned, there's nothing to do.
  606. if not _is_json(media_info.media_type):
  607. return {}, None, expiration_ms
  608. with open(media_info.filename, "rb") as file:
  609. body = file.read()
  610. oembed_response = self._oembed.parse_oembed_response(url, body)
  611. open_graph_result = oembed_response.open_graph_result
  612. # Use the cache age from the oEmbed result, if one was given.
  613. if open_graph_result and oembed_response.cache_age is not None:
  614. expiration_ms = oembed_response.cache_age
  615. return open_graph_result, oembed_response.author_name, expiration_ms
  616. def _start_expire_url_cache_data(self) -> Deferred:
  617. return run_as_background_process(
  618. "expire_url_cache_data", self._expire_url_cache_data
  619. )
  620. async def _expire_url_cache_data(self) -> None:
  621. """Clean up expired url cache content, media and thumbnails."""
  622. assert self._worker_run_media_background_jobs
  623. now = self.clock.time_msec()
  624. logger.debug("Running url preview cache expiry")
  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", "application/xhtml"))
  717. def _is_json(content_type: str) -> bool:
  718. return content_type.lower().startswith("application/json")
  719. def _is_previewable(content_type: str) -> bool:
  720. """Returns True for content types for which we will perform URL preview and False
  721. otherwise."""
  722. return _is_html(content_type) or _is_media(content_type) or _is_json(content_type)