Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 2 additions & 25 deletions ayon_api/server_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7474,29 +7474,6 @@ def get_workfile_thumbnail(
project_name, "workfile", workfile_id, thumbnail_id
)

def _get_thumbnail_mime_type(self, thumbnail_path):
"""Get thumbnail mime type on thumbnail creation based on source path.

Args:
thumbnail_path (str): Path to thumbnail source fie.

Returns:
str: Mime type used for thumbnail creation.

Raises:
ValueError: Mime type cannot be determined.

"""
ext = os.path.splitext(thumbnail_path)[-1].lower()
if ext == ".png":
return "image/png"

elif ext in (".jpeg", ".jpg"):
return "image/jpeg"

raise ValueError(
"Thumbnail source file has unknown extensions {}".format(ext))

def create_thumbnail(self, project_name, src_filepath, thumbnail_id=None):
"""Create new thumbnail on server from passed path.

Expand Down Expand Up @@ -7524,7 +7501,7 @@ def create_thumbnail(self, project_name, src_filepath, thumbnail_id=None):
)
return thumbnail_id

mime_type = self._get_thumbnail_mime_type(src_filepath)
mime_type = get_media_mime_type(src_filepath)
response = self.upload_file(
"projects/{}/thumbnails".format(project_name),
src_filepath,
Expand Down Expand Up @@ -7552,7 +7529,7 @@ def update_thumbnail(self, project_name, thumbnail_id, src_filepath):
if not os.path.exists(src_filepath):
raise ValueError("Entered filepath does not exist.")

mime_type = self._get_thumbnail_mime_type(src_filepath)
mime_type = get_media_mime_type(src_filepath)
response = self.upload_file(
"projects/{}/thumbnails/{}".format(project_name, thumbnail_id),
src_filepath,
Expand Down
40 changes: 32 additions & 8 deletions ayon_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,11 @@ def _get_media_mime_type_from_ftyp(content):
return None


def get_media_mime_type_for_content(content: bytes) -> Optional[str]:
def _get_media_mime_type_for_content_base(content: bytes) -> Optional[str]:
"""Determine Mime-Type of a file.

Use header of the file to determine mime type (needs 12 bytes).
"""
content_len = len(content)
# Pre-validation (largest definition check)
# - hopefully there cannot be media defined in less than 12 bytes
Expand All @@ -790,10 +794,6 @@ def get_media_mime_type_for_content(content: bytes) -> Optional[str]:
if content[0:4] == b"\211PNG":
return "image/png"

# SVG
if b'xmlns="http://www.w3.org/2000/svg"' in content:
return "image/svg+xml"

# JPEG, JFIF or Exif
if (
content[0:4] == b"\xff\xd8\xff\xdb"
Expand All @@ -820,6 +820,32 @@ def get_media_mime_type_for_content(content: bytes) -> Optional[str]:
return None


def _get_svg_mime_type(content: bytes) -> Optional[str]:
# SVG
if b'xmlns="http://www.w3.org/2000/svg"' in content:
return "image/svg+xml"
return None


def get_media_mime_type_for_content(content: bytes) -> Optional[str]:
mime_type = _get_media_mime_type_for_content_base(content)
if mime_type is not None:
return mime_type
return _get_svg_mime_type(content)


def get_media_mime_type_for_stream(stream) -> Optional[str]:
# Read only 12 bytes to determine mime type
content = stream.read(12)
if len(content) < 12:
return None
mime_type = _get_media_mime_type_for_content_base(content)
if mime_type is None:
content += stream.read()
mime_type = _get_svg_mime_type(content)
return mime_type


def get_media_mime_type(filepath: str) -> Optional[str]:
"""Determine Mime-Type of a file.

Expand All @@ -834,9 +860,7 @@ def get_media_mime_type(filepath: str) -> Optional[str]:
return None

with open(filepath, "rb") as stream:
content = stream.read()

return get_media_mime_type_for_content(content)
return get_media_mime_type_for_stream(stream)


def take_web_action_event(
Expand Down