From f354f42308ce11f9f3793ce0637e63a468dac4e1 Mon Sep 17 00:00:00 2001 From: Stefan Dej Date: Sun, 2 Jul 2023 12:54:44 +0200 Subject: [PATCH] metadata: add support for JPG thumbnails Thx to pedrolamas for the regex fix Signed-off-by: Stefan Dej --- moonraker/components/file_manager/metadata.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/moonraker/components/file_manager/metadata.py b/moonraker/components/file_manager/metadata.py index 411ec8b80..139697226 100644 --- a/moonraker/components/file_manager/metadata.py +++ b/moonraker/components/file_manager/metadata.py @@ -219,7 +219,8 @@ def parse_first_layer_extr_temp(self) -> Optional[float]: def parse_thumbnails(self) -> Optional[List[Dict[str, Any]]]: for data in [self.header_data, self.footer_data]: thumb_matches: List[str] = re.findall( - r"; thumbnail begin[;/\+=\w\s]+?; thumbnail end", data) + r"; (?:thumbnail|thumbnail_JPG) begin([;/\+=\w\s]+?)" + r"; (?:thumbnail|thumbnail_JPG) end", data) if thumb_matches: break else: @@ -235,7 +236,7 @@ def parse_thumbnails(self) -> Optional[List[Dict[str, Any]]]: parsed_matches: List[Dict[str, Any]] = [] has_miniature: bool = False for match in thumb_matches: - lines = re.split(r"\r?\n", match.replace('; ', '')) + lines = re.split(r"\r?\n", match[1].replace('; ', '')) info = _regex_find_ints(r".*", lines[0]) data = "".join(lines[1:-1]) if len(info) != 3: @@ -248,7 +249,12 @@ def parse_thumbnails(self) -> Optional[List[Dict[str, Any]]]: f"MetadataError: Thumbnail Size Mismatch: " f"detected {info[2]}, actual {len(data)}") continue - thumb_name = f"{thumb_base}-{info[0]}x{info[1]}.png" + # set default thumbnail extension + thumb_extension = "png" + # check if thumbnail is a jpg + if match[0] == "thumbnail_JPG": + thumb_extension = "jpg" + thumb_name = f"{thumb_base}-{info[0]}x{info[1]}.{thumb_extension}" thumb_path = os.path.join(thumb_dir, thumb_name) rel_thumb_path = os.path.join(".thumbs", thumb_name) with open(thumb_path, "wb") as f: