From 796a91d994da214eaf542bc36be987fa8c4f1638 Mon Sep 17 00:00:00 2001 From: Sergey Shpikin Date: Sun, 11 Apr 2021 13:49:58 +0300 Subject: [PATCH 1/3] More robust handling of the Content-Type header for thumbnail generation Signed-off-by: Sergey Shpikin --- changelog.d/9788.bugfix | 1 + synapse/config/repository.py | 1 + synapse/rest/media/v1/preview_url_resource.py | 4 ++++ 3 files changed, 6 insertions(+) create mode 100644 changelog.d/9788.bugfix diff --git a/changelog.d/9788.bugfix b/changelog.d/9788.bugfix new file mode 100644 index 000000000000..05acf8b85394 --- /dev/null +++ b/changelog.d/9788.bugfix @@ -0,0 +1 @@ +Fix thumbnail generation for some sites with non-standard content types. diff --git a/synapse/config/repository.py b/synapse/config/repository.py index 061c4ec83fc7..508d8754833f 100644 --- a/synapse/config/repository.py +++ b/synapse/config/repository.py @@ -71,6 +71,7 @@ def parse_thumbnail_requirements(thumbnail_sizes): jpeg_thumbnail = ThumbnailRequirement(width, height, method, "image/jpeg") png_thumbnail = ThumbnailRequirement(width, height, method, "image/png") requirements.setdefault("image/jpeg", []).append(jpeg_thumbnail) + requirements.setdefault("image/jpg", []).append(jpeg_thumbnail) requirements.setdefault("image/webp", []).append(jpeg_thumbnail) requirements.setdefault("image/gif", []).append(png_thumbnail) requirements.setdefault("image/png", []).append(png_thumbnail) diff --git a/synapse/rest/media/v1/preview_url_resource.py b/synapse/rest/media/v1/preview_url_resource.py index 814145a04a15..765ebdff5c9f 100644 --- a/synapse/rest/media/v1/preview_url_resource.py +++ b/synapse/rest/media/v1/preview_url_resource.py @@ -513,6 +513,10 @@ async def _download_url(self, url: str, user: str) -> Dict[str, Any]: if b"Content-Type" in headers: media_type = headers[b"Content-Type"][0].decode("ascii") + if media_type.startswith("image/"): + scpos = media_type.find(";") + if scpos > 0: + media_type = media_type[:scpos] else: media_type = "application/octet-stream" From c0288736d4d2c8b83bab95a931ab309f2d5cbf38 Mon Sep 17 00:00:00 2001 From: Sergey Shpikin Date: Wed, 14 Apr 2021 17:53:33 +0300 Subject: [PATCH 2/3] Strip content-type parameters --- synapse/rest/media/v1/media_repository.py | 3 +++ synapse/rest/media/v1/preview_url_resource.py | 4 ---- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/synapse/rest/media/v1/media_repository.py b/synapse/rest/media/v1/media_repository.py index 0c041b542d4d..5c4d33ee07d6 100644 --- a/synapse/rest/media/v1/media_repository.py +++ b/synapse/rest/media/v1/media_repository.py @@ -468,6 +468,9 @@ async def _download_remote_file( return media_info def _get_thumbnail_requirements(self, media_type): + scpos = media_type.find(";") + if scpos > 0: + media_type = media_type[:scpos] return self.thumbnail_requirements.get(media_type, ()) def _generate_thumbnail( diff --git a/synapse/rest/media/v1/preview_url_resource.py b/synapse/rest/media/v1/preview_url_resource.py index 765ebdff5c9f..814145a04a15 100644 --- a/synapse/rest/media/v1/preview_url_resource.py +++ b/synapse/rest/media/v1/preview_url_resource.py @@ -513,10 +513,6 @@ async def _download_url(self, url: str, user: str) -> Dict[str, Any]: if b"Content-Type" in headers: media_type = headers[b"Content-Type"][0].decode("ascii") - if media_type.startswith("image/"): - scpos = media_type.find(";") - if scpos > 0: - media_type = media_type[:scpos] else: media_type = "application/octet-stream" From c08e6a16c9d2f9dd91987d6288a2f224db8dbc9d Mon Sep 17 00:00:00 2001 From: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Date: Wed, 14 Apr 2021 16:30:50 +0100 Subject: [PATCH 3/3] Update 9788.bugfix --- changelog.d/9788.bugfix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.d/9788.bugfix b/changelog.d/9788.bugfix index 05acf8b85394..edb58fbd5b37 100644 --- a/changelog.d/9788.bugfix +++ b/changelog.d/9788.bugfix @@ -1 +1 @@ -Fix thumbnail generation for some sites with non-standard content types. +Fix thumbnail generation for some sites with non-standard content types. Contributed by @rkfg.