Skip to content

Commit

Permalink
fix thumbnails parse for SLMetaData (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarkafa authored Oct 30, 2023
1 parent ffe5214 commit 47e2c1e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
15 changes: 9 additions & 6 deletions gcode_metadata/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,8 @@ class SLMetaData(MetaData):
"fileCreationTimestamp": str,
}

THUMBNAIL_NAME_PAT = re.compile(r"(?P<dim>\d+x\d+)")
THUMBNAIL_NAME_PAT = re.compile(
r".*?(?P<dim>\d+x\d+)\.(?P<format>qoi|jpg|png)")

def load(self, save_cache=True):
"""Load metadata"""
Expand Down Expand Up @@ -824,11 +825,13 @@ def extract_thumbnails(path: str) -> Dict[str, bytes]:
with zipfile.ZipFile(path, "r") as zip_file:
for info in zip_file.infolist():
if info.filename.startswith("thumbnail/"):
data = zip_file.read(info.filename)
data = base64.b64encode(data)
dim = SLMetaData.THUMBNAIL_NAME_PAT.findall(
info.filename)[-1]
thumbnails[dim] = data
match = SLMetaData.THUMBNAIL_NAME_PAT.match(info.filename)
if match:
img_format = match.group("format").upper()
img_dim = match.group("dim")
data = zip_file.read(info.filename)
data = base64.b64encode(data)
thumbnails[f"{img_dim}_{img_format}"] = data
return thumbnails


Expand Down
4 changes: 2 additions & 2 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ def test_sl(self):
'fileCreationTimestamp': '2020-09-17 at 13:53:21 UTC'
}

assert len(meta.thumbnails["400x400"]) == 19688
assert len(meta.thumbnails["800x480"]) == 64524
assert len(meta.thumbnails["400x400_PNG"]) == 19688
assert len(meta.thumbnails["800x480_PNG"]) == 64524

def test_sl_empty_file(self):
"""Test a file that is empty"""
Expand Down

0 comments on commit 47e2c1e

Please sign in to comment.