Allow specifying the value of Accept-Language header for URL preview
parent
118b58f0c9
commit
c93ca023a7
|
@ -859,6 +859,31 @@ media_store_path: "DATADIR/media_store"
|
||||||
#
|
#
|
||||||
#max_spider_size: 10M
|
#max_spider_size: 10M
|
||||||
|
|
||||||
|
# A list of values for the Accept-Language HTTP header used when
|
||||||
|
# downloading webpages during URL preview generation. This allows
|
||||||
|
# Synapse to specify the preferred languages that URL previews should
|
||||||
|
# be in when communicating with remote servers.
|
||||||
|
#
|
||||||
|
# Each value is a IETF language tag; a 2-3 letter identifier for a
|
||||||
|
# language, optionally followed by subtags separated by '-', specifying
|
||||||
|
# a country or region variant.
|
||||||
|
#
|
||||||
|
# Multiple values can be provided, and a weight can be added to each by
|
||||||
|
# using quality value syntax (;q=). '*' translates to any language.
|
||||||
|
#
|
||||||
|
# Defaults to "en".
|
||||||
|
#
|
||||||
|
# Example:
|
||||||
|
#
|
||||||
|
# url_preview_accept_language:
|
||||||
|
# - en-UK
|
||||||
|
# - en-US;q=0.9
|
||||||
|
# - fr;q=0.8
|
||||||
|
# - *;q=0.7
|
||||||
|
#
|
||||||
|
url_preview_accept_language:
|
||||||
|
# - en
|
||||||
|
|
||||||
|
|
||||||
## Captcha ##
|
## Captcha ##
|
||||||
# See docs/CAPTCHA_SETUP for full details of configuring this.
|
# See docs/CAPTCHA_SETUP for full details of configuring this.
|
||||||
|
|
|
@ -192,6 +192,8 @@ class ContentRepositoryConfig(Config):
|
||||||
|
|
||||||
self.url_preview_url_blacklist = config.get("url_preview_url_blacklist", ())
|
self.url_preview_url_blacklist = config.get("url_preview_url_blacklist", ())
|
||||||
|
|
||||||
|
self.url_preview_accept_language = config.get("url_preview_accept_language") or ["en"]
|
||||||
|
|
||||||
def generate_config_section(self, data_dir_path, **kwargs):
|
def generate_config_section(self, data_dir_path, **kwargs):
|
||||||
media_store = os.path.join(data_dir_path, "media_store")
|
media_store = os.path.join(data_dir_path, "media_store")
|
||||||
uploads_path = os.path.join(data_dir_path, "uploads")
|
uploads_path = os.path.join(data_dir_path, "uploads")
|
||||||
|
@ -329,6 +331,31 @@ class ContentRepositoryConfig(Config):
|
||||||
# The largest allowed URL preview spidering size in bytes
|
# The largest allowed URL preview spidering size in bytes
|
||||||
#
|
#
|
||||||
#max_spider_size: 10M
|
#max_spider_size: 10M
|
||||||
|
|
||||||
|
# A list of values for the Accept-Language HTTP header used when
|
||||||
|
# downloading webpages during URL preview generation. This allows
|
||||||
|
# Synapse to specify the preferred languages that URL previews should
|
||||||
|
# be in when communicating with remote servers.
|
||||||
|
#
|
||||||
|
# Each value is a IETF language tag; a 2-3 letter identifier for a
|
||||||
|
# language, optionally followed by subtags separated by '-', specifying
|
||||||
|
# a country or region variant.
|
||||||
|
#
|
||||||
|
# Multiple values can be provided, and a weight can be added to each by
|
||||||
|
# using quality value syntax (;q=). '*' translates to any language.
|
||||||
|
#
|
||||||
|
# Defaults to "en".
|
||||||
|
#
|
||||||
|
# Example:
|
||||||
|
#
|
||||||
|
# url_preview_accept_language:
|
||||||
|
# - en-UK
|
||||||
|
# - en-US;q=0.9
|
||||||
|
# - fr;q=0.8
|
||||||
|
# - *;q=0.7
|
||||||
|
#
|
||||||
|
url_preview_accept_language:
|
||||||
|
# - en
|
||||||
"""
|
"""
|
||||||
% locals()
|
% locals()
|
||||||
)
|
)
|
||||||
|
|
|
@ -86,6 +86,7 @@ class PreviewUrlResource(DirectServeResource):
|
||||||
self.media_storage = media_storage
|
self.media_storage = media_storage
|
||||||
|
|
||||||
self.url_preview_url_blacklist = hs.config.url_preview_url_blacklist
|
self.url_preview_url_blacklist = hs.config.url_preview_url_blacklist
|
||||||
|
self.url_preview_accept_language = hs.config.url_preview_url_blacklist
|
||||||
|
|
||||||
# memory cache mapping urls to an ObservableDeferred returning
|
# memory cache mapping urls to an ObservableDeferred returning
|
||||||
# JSON-encoded OG metadata
|
# JSON-encoded OG metadata
|
||||||
|
@ -315,9 +316,12 @@ class PreviewUrlResource(DirectServeResource):
|
||||||
|
|
||||||
with self.media_storage.store_into_file(file_info) as (f, fname, finish):
|
with self.media_storage.store_into_file(file_info) as (f, fname, finish):
|
||||||
try:
|
try:
|
||||||
logger.debug("Trying to get url '%s'", url)
|
logger.debug("Trying to get preview for url '%s'", url)
|
||||||
length, headers, uri, code = await self.client.get_file(
|
length, headers, uri, code = await self.client.get_file(
|
||||||
url, output_stream=f, max_size=self.max_spider_size
|
url,
|
||||||
|
output_stream=f,
|
||||||
|
max_size=self.max_spider_size,
|
||||||
|
headers={"Accept Language": self.url_preview_accept_language},
|
||||||
)
|
)
|
||||||
except SynapseError:
|
except SynapseError:
|
||||||
# Pass SynapseErrors through directly, so that the servlet
|
# Pass SynapseErrors through directly, so that the servlet
|
||||||
|
|
Loading…
Reference in New Issue