Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

261 rader
9.9 KiB

  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.media_store_path = self.ensure_directory(
  117. config.get("media_store_path", "media_store")
  118. )
  119. backup_media_store_path = config.get("backup_media_store_path")
  120. synchronous_backup_media_store = config.get(
  121. "synchronous_backup_media_store", False
  122. )
  123. storage_providers = config.get("media_storage_providers", [])
  124. if backup_media_store_path:
  125. if storage_providers:
  126. raise ConfigError(
  127. "Cannot use both 'backup_media_store_path' and 'storage_providers'"
  128. )
  129. storage_providers = [
  130. {
  131. "module": "file_system",
  132. "store_local": True,
  133. "store_synchronous": synchronous_backup_media_store,
  134. "store_remote": True,
  135. "config": {"directory": backup_media_store_path},
  136. }
  137. ]
  138. # This is a list of config that can be used to create the storage
  139. # providers. The entries are tuples of (Class, class_config,
  140. # MediaStorageProviderConfig), where Class is the class of the provider,
  141. # the class_config the config to pass to it, and
  142. # MediaStorageProviderConfig are options for StorageProviderWrapper.
  143. #
  144. # We don't create the storage providers here as not all workers need
  145. # them to be started.
  146. self.media_storage_providers: List[tuple] = []
  147. for i, provider_config in enumerate(storage_providers):
  148. # We special case the module "file_system" so as not to need to
  149. # expose FileStorageProviderBackend
  150. if provider_config["module"] == "file_system":
  151. provider_config["module"] = (
  152. "synapse.rest.media.v1.storage_provider"
  153. ".FileStorageProviderBackend"
  154. )
  155. provider_class, parsed_config = load_module(
  156. provider_config, ("media_storage_providers", "<item %i>" % i)
  157. )
  158. wrapper_config = MediaStorageProviderConfig(
  159. provider_config.get("store_local", False),
  160. provider_config.get("store_remote", False),
  161. provider_config.get("store_synchronous", False),
  162. )
  163. self.media_storage_providers.append(
  164. (provider_class, parsed_config, wrapper_config)
  165. )
  166. self.dynamic_thumbnails = config.get("dynamic_thumbnails", False)
  167. self.thumbnail_requirements = parse_thumbnail_requirements(
  168. config.get("thumbnail_sizes", DEFAULT_THUMBNAIL_SIZES)
  169. )
  170. self.url_preview_enabled = config.get("url_preview_enabled", False)
  171. if self.url_preview_enabled:
  172. check_requirements("url-preview")
  173. proxy_env = getproxies_environment()
  174. if "url_preview_ip_range_blacklist" not in config:
  175. if "http" not in proxy_env or "https" not in proxy_env:
  176. raise ConfigError(
  177. "For security, you must specify an explicit target IP address "
  178. "blacklist in url_preview_ip_range_blacklist for url previewing "
  179. "to work"
  180. )
  181. else:
  182. if "http" in proxy_env or "https" in proxy_env:
  183. logger.warning("".join(HTTP_PROXY_SET_WARNING))
  184. # we always blacklist '0.0.0.0' and '::', which are supposed to be
  185. # unroutable addresses.
  186. self.url_preview_ip_range_blacklist = generate_ip_set(
  187. config["url_preview_ip_range_blacklist"],
  188. ["0.0.0.0", "::"],
  189. config_path=("url_preview_ip_range_blacklist",),
  190. )
  191. self.url_preview_ip_range_whitelist = generate_ip_set(
  192. config.get("url_preview_ip_range_whitelist", ()),
  193. config_path=("url_preview_ip_range_whitelist",),
  194. )
  195. self.url_preview_url_blacklist = config.get("url_preview_url_blacklist", ())
  196. self.url_preview_accept_language = config.get(
  197. "url_preview_accept_language"
  198. ) or ["en"]
  199. media_retention = config.get("media_retention") or {}
  200. self.media_retention_local_media_lifetime_ms = None
  201. local_media_lifetime = media_retention.get("local_media_lifetime")
  202. if local_media_lifetime is not None:
  203. self.media_retention_local_media_lifetime_ms = self.parse_duration(
  204. local_media_lifetime
  205. )
  206. self.media_retention_remote_media_lifetime_ms = None
  207. remote_media_lifetime = media_retention.get("remote_media_lifetime")
  208. if remote_media_lifetime is not None:
  209. self.media_retention_remote_media_lifetime_ms = self.parse_duration(
  210. remote_media_lifetime
  211. )
  212. def generate_config_section(self, data_dir_path: str, **kwargs: Any) -> str:
  213. assert data_dir_path is not None
  214. media_store = os.path.join(data_dir_path, "media_store")
  215. return f"media_store_path: {media_store}"