Skip to content

Commit

Permalink
added more targeted functions to read file content
Browse files Browse the repository at this point in the history
  • Loading branch information
iLLiCiTiT committed Nov 15, 2024
1 parent ae5db57 commit 865303c
Showing 1 changed file with 32 additions and 8 deletions.
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

0 comments on commit 865303c

Please sign in to comment.