From ae5db571d2cad844cc7659161eb0f3fe8df7aeab Mon Sep 17 00:00:00 2001 From: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> Date: Thu, 14 Nov 2024 13:16:52 +0100 Subject: [PATCH 1/2] fix thumbnail mime type calculation --- ayon_api/server_api.py | 27 ++------------------------- 1 file changed, 2 insertions(+), 25 deletions(-) diff --git a/ayon_api/server_api.py b/ayon_api/server_api.py index 2e151ffc9..17535d8d7 100644 --- a/ayon_api/server_api.py +++ b/ayon_api/server_api.py @@ -7365,29 +7365,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. @@ -7415,7 +7392,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, @@ -7443,7 +7420,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, From 865303cb960b72e4e1fcf33907531cad144b0585 Mon Sep 17 00:00:00 2001 From: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> Date: Fri, 15 Nov 2024 16:04:22 +0100 Subject: [PATCH 2/2] added more targeted functions to read file content --- ayon_api/utils.py | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/ayon_api/utils.py b/ayon_api/utils.py index 0346fb4fa..d418ef7e7 100644 --- a/ayon_api/utils.py +++ b/ayon_api/utils.py @@ -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 @@ -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" @@ -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. @@ -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(