25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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