repository.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. # Copyright 2014, 2015 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import logging
  15. import os
  16. from typing import Any, Dict, List, Tuple
  17. from urllib.request import getproxies_environment # type: ignore
  18. import attr
  19. from synapse.config.server import generate_ip_set
  20. from synapse.types import JsonDict
  21. from synapse.util.check_dependencies import check_requirements
  22. from synapse.util.module_loader import load_module
  23. from ._base import Config, ConfigError
  24. logger = logging.getLogger(__name__)
  25. DEFAULT_THUMBNAIL_SIZES = [
  26. {"width": 32, "height": 32, "method": "crop"},
  27. {"width": 96, "height": 96, "method": "crop"},
  28. {"width": 320, "height": 240, "method": "scale"},
  29. {"width": 640, "height": 480, "method": "scale"},
  30. {"width": 800, "height": 600, "method": "scale"},
  31. ]
  32. THUMBNAIL_SIZE_YAML = """\
  33. # - width: %(width)i
  34. # height: %(height)i
  35. # method: %(method)s
  36. """
  37. # A map from the given media type to the type of thumbnail we should generate
  38. # for it.
  39. THUMBNAIL_SUPPORTED_MEDIA_FORMAT_MAP = {
  40. "image/jpeg": "jpeg",
  41. "image/jpg": "jpeg",
  42. "image/webp": "jpeg",
  43. # Thumbnails can only be jpeg or png. We choose png thumbnails for gif
  44. # because it can have transparency.
  45. "image/gif": "png",
  46. "image/png": "png",
  47. }
  48. HTTP_PROXY_SET_WARNING = """\
  49. The Synapse config url_preview_ip_range_blacklist will be ignored as an HTTP(s) proxy is configured."""
  50. @attr.s(frozen=True, slots=True, auto_attribs=True)
  51. class ThumbnailRequirement:
  52. width: int
  53. height: int
  54. method: str
  55. media_type: str
  56. @attr.s(frozen=True, slots=True, auto_attribs=True)
  57. class MediaStorageProviderConfig:
  58. store_local: bool # Whether to store newly uploaded local files
  59. store_remote: bool # Whether to store newly downloaded remote files
  60. store_synchronous: bool # Whether to wait for successful storage for local uploads
  61. def parse_thumbnail_requirements(
  62. thumbnail_sizes: List[JsonDict],
  63. ) -> Dict[str, Tuple[ThumbnailRequirement, ...]]:
  64. """Takes a list of dictionaries with "width", "height", and "method" keys
  65. and creates a map from image media types to the thumbnail size, thumbnailing
  66. method, and thumbnail media type to precalculate
  67. Args:
  68. thumbnail_sizes: List of dicts with "width", "height", and "method" keys
  69. Returns:
  70. Dictionary mapping from media type string to list of ThumbnailRequirement.
  71. """
  72. requirements: Dict[str, List[ThumbnailRequirement]] = {}
  73. for size in thumbnail_sizes:
  74. width = size["width"]
  75. height = size["height"]
  76. method = size["method"]
  77. for format, thumbnail_format in THUMBNAIL_SUPPORTED_MEDIA_FORMAT_MAP.items():
  78. requirement = requirements.setdefault(format, [])
  79. if thumbnail_format == "jpeg":
  80. requirement.append(
  81. ThumbnailRequirement(width, height, method, "image/jpeg")
  82. )
  83. elif thumbnail_format == "png":
  84. requirement.append(
  85. ThumbnailRequirement(width, height, method, "image/png")
  86. )
  87. else:
  88. raise Exception(
  89. "Unknown thumbnail mapping from %s to %s. This is a Synapse problem, please report!"
  90. % (format, thumbnail_format)
  91. )
  92. return {
  93. media_type: tuple(thumbnails) for media_type, thumbnails in requirements.items()
  94. }
  95. class ContentRepositoryConfig(Config):
  96. section = "media"
  97. def read_config(self, config: JsonDict, **kwargs: Any) -> None:
  98. # Only enable the media repo if either the media repo is enabled or the
  99. # current worker app is the media repo.
  100. if (
  101. self.root.server.enable_media_repo is False
  102. and config.get("worker_app") != "synapse.app.media_repository"
  103. ):
  104. self.can_load_media_repo = False
  105. return
  106. else:
  107. self.can_load_media_repo = True
  108. # Whether this instance should be the one to run the background jobs to
  109. # e.g clean up old URL previews.
  110. self.media_instance_running_background_jobs = config.get(
  111. "media_instance_running_background_jobs",
  112. )
  113. self.max_upload_size = self.parse_size(config.get("max_upload_size", "50M"))
  114. self.max_image_pixels = self.parse_size(config.get("max_image_pixels", "32M"))
  115. self.max_spider_size = self.parse_size(config.get("max_spider_size", "10M"))
  116. self.prevent_media_downloads_from = config.get(
  117. "prevent_media_downloads_from", []
  118. )
  119. self.media_store_path = self.ensure_directory(
  120. config.get("media_store_path", "media_store")
  121. )
  122. backup_media_store_path = config.get("backup_media_store_path")
  123. synchronous_backup_media_store = config.get(
  124. "synchronous_backup_media_store", False
  125. )
  126. storage_providers = config.get("media_storage_providers", [])
  127. if backup_media_store_path:
  128. if storage_providers:
  129. raise ConfigError(
  130. "Cannot use both 'backup_media_store_path' and 'storage_providers'"
  131. )
  132. storage_providers = [
  133. {
  134. "module": "file_system",
  135. "store_local": True,
  136. "store_synchronous": synchronous_backup_media_store,
  137. "store_remote": True,
  138. "config": {"directory": backup_media_store_path},
  139. }
  140. ]
  141. # This is a list of config that can be used to create the storage
  142. # providers. The entries are tuples of (Class, class_config,
  143. # MediaStorageProviderConfig), where Class is the class of the provider,
  144. # the class_config the config to pass to it, and
  145. # MediaStorageProviderConfig are options for StorageProviderWrapper.
  146. #
  147. # We don't create the storage providers here as not all workers need
  148. # them to be started.
  149. self.media_storage_providers: List[tuple] = []
  150. for i, provider_config in enumerate(storage_providers):
  151. # We special case the module "file_system" so as not to need to
  152. # expose FileStorageProviderBackend
  153. if (
  154. provider_config["module"] == "file_system"
  155. or provider_config["module"] == "synapse.rest.media.v1.storage_provider"
  156. ):
  157. provider_config[
  158. "module"
  159. ] = "synapse.media.storage_provider.FileStorageProviderBackend"
  160. provider_class, parsed_config = load_module(
  161. provider_config, ("media_storage_providers", "<item %i>" % i)
  162. )
  163. wrapper_config = MediaStorageProviderConfig(
  164. provider_config.get("store_local", False),
  165. provider_config.get("store_remote", False),
  166. provider_config.get("store_synchronous", False),
  167. )
  168. self.media_storage_providers.append(
  169. (provider_class, parsed_config, wrapper_config)
  170. )
  171. self.dynamic_thumbnails = config.get("dynamic_thumbnails", False)
  172. self.thumbnail_requirements = parse_thumbnail_requirements(
  173. config.get("thumbnail_sizes", DEFAULT_THUMBNAIL_SIZES)
  174. )
  175. self.url_preview_enabled = config.get("url_preview_enabled", False)
  176. if self.url_preview_enabled:
  177. check_requirements("url-preview")
  178. proxy_env = getproxies_environment()
  179. if "url_preview_ip_range_blacklist" not in config:
  180. if "http" not in proxy_env or "https" not in proxy_env:
  181. raise ConfigError(
  182. "For security, you must specify an explicit target IP address "
  183. "blacklist in url_preview_ip_range_blacklist for url previewing "
  184. "to work"
  185. )
  186. else:
  187. if "http" in proxy_env or "https" in proxy_env:
  188. logger.warning("".join(HTTP_PROXY_SET_WARNING))
  189. # we always block '0.0.0.0' and '::', which are supposed to be
  190. # unroutable addresses.
  191. self.url_preview_ip_range_blocklist = generate_ip_set(
  192. config["url_preview_ip_range_blacklist"],
  193. ["0.0.0.0", "::"],
  194. config_path=("url_preview_ip_range_blacklist",),
  195. )
  196. self.url_preview_ip_range_allowlist = generate_ip_set(
  197. config.get("url_preview_ip_range_whitelist", ()),
  198. config_path=("url_preview_ip_range_whitelist",),
  199. )
  200. self.url_preview_url_blocklist = config.get("url_preview_url_blacklist", ())
  201. self.url_preview_accept_language = config.get(
  202. "url_preview_accept_language"
  203. ) or ["en"]
  204. media_retention = config.get("media_retention") or {}
  205. self.media_retention_local_media_lifetime_ms = None
  206. local_media_lifetime = media_retention.get("local_media_lifetime")
  207. if local_media_lifetime is not None:
  208. self.media_retention_local_media_lifetime_ms = self.parse_duration(
  209. local_media_lifetime
  210. )
  211. self.media_retention_remote_media_lifetime_ms = None
  212. remote_media_lifetime = media_retention.get("remote_media_lifetime")
  213. if remote_media_lifetime is not None:
  214. self.media_retention_remote_media_lifetime_ms = self.parse_duration(
  215. remote_media_lifetime
  216. )
  217. def generate_config_section(self, data_dir_path: str, **kwargs: Any) -> str:
  218. assert data_dir_path is not None
  219. media_store = os.path.join(data_dir_path, "media_store")
  220. return f"media_store_path: {media_store}"