Skip to content

Commit

Permalink
Bugfix to ensure image type is correctly extracted from content type (#…
Browse files Browse the repository at this point in the history
…4062)

* fix: modified extension.py to split file extension by ';'

* test: added test that ensures that stripped file url is expected file type

* test: added pytest cases for test_get_extension_from_content_type()

* fix: added a . to one of the file extension parameters in pytest.mark.parametrize for test_get_extension_from_content_type()

* Apply linting

* Fix tests (initially)

* fix: added check for the case of a semicolon in the content_type and modified get_file_extension_from_content_type() to utilize mimetypes.guess_extension() to automatically find file extensions in strings

* fix: added check for test case for 'image/png;charset=UTF-8'

* fix: changed jpeg to jpg

* fix: changed test cases 'audio/midi' to compare to 'midi', and cases that return None when passed to mimetypes.guess_extension() to compare to None. Also, call mimetypes.guess_extension() in conditional header, assign it to variable, and then only strip it of '.' if it isn't None

* fix: removed test input '5' because not iterable and added test input 'foobar'

* Lint files

---------

Co-authored-by: Darien <[email protected]>
Co-authored-by: Madison Swain-Bowden <[email protected]>
  • Loading branch information
3 people authored Apr 10, 2024
1 parent 7e9eb78 commit 35197e9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
9 changes: 7 additions & 2 deletions api/api/utils/image_proxy/extension.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import mimetypes
from os.path import splitext
from urllib.parse import urlparse

Expand Down Expand Up @@ -78,6 +79,10 @@ def _get_file_extension_from_content_type(content_type: str) -> str | None:
Return the image extension if present in the Response's content type
header.
"""
if content_type and "/" in content_type:
return content_type.split("/")[1]
if (
content_type
and "/" in content_type
and (ext := mimetypes.guess_extension(content_type.split(";")[0], strict=False))
):
return ext.strip(".")
return None
22 changes: 22 additions & 0 deletions api/test/unit/utils/test_image_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,28 @@ def test__get_extension_from_url(image_url, expected_ext):
assert extension._get_file_extension_from_url(image_url) == expected_ext


@pytest.mark.parametrize(
"content_type, expected_ext",
[
("image/png;charset=UTF-8", "png"),
("image/jpeg", "jpg"),
("image/png", "png"),
("image/gif", "gif"),
("image/svg+xml", "svg"),
("audio/midi", "midi"),
("audio/mpeg", "mp3"),
("audio/ogg", None),
("audio/opus", "opus"),
("audio/wav", None),
("video/webm", "webm"),
(None, None),
("foobar", None),
],
)
def test_get_extension_from_content_type(content_type, expected_ext):
assert extension._get_file_extension_from_content_type(content_type) == expected_ext


@pytest.mark.django_db
@pytest.mark.parametrize("image_type", ["apng", "tiff", "bmp"])
def test_photon_get_raises_by_not_allowed_types(photon_get, image_type):
Expand Down

0 comments on commit 35197e9

Please sign in to comment.