Skip to content

Commit 68aa9cb

Browse files
authored
Merge pull request #433 from ynput/fix-warn-when-thumbnail-mime-type-mismatches
Warn when thumbnail mime mismatches instead of returning an error
2 parents 199da63 + eeb8ec2 commit 68aa9cb

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

api/thumbnails/thumbnails.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
BadRequestException,
2525
ForbiddenException,
2626
NotFoundException,
27+
UnsupportedMediaException,
2728
)
2829
from ayon_server.files import Storages
2930
from ayon_server.helpers.mimetypes import guess_mime_type
@@ -84,8 +85,27 @@ async def store_thumbnail(
8485
MAX_THUMBNAIL_WIDTH = 600
8586
MAX_THUMBNAIL_HEIGHT = 600
8687

87-
if guess_mime_type(payload) != mime:
88-
raise BadRequestException("Mime type does not match the payload")
88+
guessed_mime = guess_mime_type(payload)
89+
if guessed_mime is None:
90+
# This shouldn't happen, but we'll log it.
91+
# Upload will probably fail later on, in process_thumbnail.
92+
logging.warning(
93+
f"Could not guess mime type of thumbnail. Using provided {mime}"
94+
)
95+
96+
elif guessed_mime != mime:
97+
# This is a warning, not an error, because we can still store the thumbnail
98+
# even if the mime type is wrong. We're just logging it and using the
99+
# correct mime type instead of the provided one.
100+
logging.warning(
101+
"Thumbnail mime type mismatch: "
102+
f"Payload contains {guessed_mime} "
103+
f"but was requested to store {mime}"
104+
)
105+
mime = guessed_mime
106+
107+
if mime not in ["image/png", "image/jpeg"]:
108+
raise UnsupportedMediaException(f"Unsupported thumbnail mime type {mime}")
89109

90110
try:
91111
thumbnail = await process_thumbnail(
@@ -94,7 +114,8 @@ async def store_thumbnail(
94114
raise_on_noop=True,
95115
)
96116
except ValueError as e:
97-
raise BadRequestException(str(e))
117+
raise UnsupportedMediaException(str(e))
118+
98119
except ThumbnailProcessNoop:
99120
thumbnail = payload
100121
else:

0 commit comments

Comments
 (0)