Skip to content

Commit

Permalink
metadata: add support for JPG thumbnails
Browse files Browse the repository at this point in the history
Thx to pedrolamas for the regex fix

Signed-off-by: Stefan Dej <[email protected]>
  • Loading branch information
meteyou committed Aug 11, 2023
1 parent a7e154f commit f354f42
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions moonraker/components/file_manager/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit f354f42

Please sign in to comment.