metadata: improve handling of file encoding

The size of the file is not necessarily equal to the number of characters
read due to variable length utf8 encoding.  Read gcode files in binary mode then encode to a string.

When determining the "gcode start byte" and "gcode end byte" use
the length of the decoded string to determine the correct value.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan committed 2025-09-16 07:47:32 -04:00
1 parent fa90202eb9
commit 01963c45c1
1 file changed
+53 -58
+53 -58
View File
@@ -36,6 +36,7 @@ from typing import (
if TYPE_CHECKING:
pass
READ_SIZE = 1024 * 1024 # 1 MiB
UFP_MODEL_PATH = "/3D/model.gcode"
UFP_THUMB_PATH = "/Metadata/thumbnail.png"
SUPPORTED_THUMB_FORMATS = ("png", "jpg", "qoi")
@@ -135,17 +136,16 @@ class BaseSlicer(object):
self.path = file_path
self.slicer_name = "Unknown"
self.slicer_version = "?"
self._file_data: str = ""
self.header_data: str = ""
self.footer_data: str = ""
self.layer_height: Optional[float] = None
self.has_m486_objects: bool = False
def set_data(self,
header_data: str,
footer_data: str,
fsize: int) -> None:
self.header_data = header_data
self.footer_data = footer_data
def set_data(self, file_data: str, fsize: int) -> None:
self._file_data = file_data
self.header_data = file_data[:READ_SIZE]
self.footer_data = file_data[-READ_SIZE:]
self.size: int = fsize
def _check_has_objects(self,
@@ -196,14 +196,14 @@ class BaseSlicer(object):
m = re.search(r"\n[MG]\d+\s.*\n", self.header_data)
if m is None:
return None
return m.start()
return len(self.header_data[:m.start()].encode())
def parse_gcode_end_byte(self) -> Optional[int]:
rev_data = self.footer_data[::-1]
m = re.search(r"\n.*\s\d+[MG]\n", rev_data)
if m is None:
return None
return self.size - m.start()
return self.size - len(rev_data[:m.start()].encode())
def parse_first_layer_height(self) -> Optional[float]:
return None
@@ -271,43 +271,42 @@ class BaseSlicer(object):
return None
thumb_base = os.path.splitext(os.path.basename(self.path))[0]
pattern = r"(thumbnail(?:_[A-Za-z0-9]+)?) begin([;/\+=\w\s]+?); \1 end"
for data in [self.header_data, self.footer_data]:
for match in re.finditer(pattern, data):
ext = match.group(1).partition("_")[2].lower() or "png"
if ext not in SUPPORTED_THUMB_FORMATS:
logger.info(f"Unsupported thumbnail extension: {ext}")
continue
lines = re.split(r"\r?\n", match.group(2).replace('; ', ''))
info = regex_find_ints(r"(%D)", lines[0])
data = "".join(lines[1:-1])
if len(info) != 3:
logger.info(
f"MetadataError: Error parsing thumbnail header: {lines[0]}"
)
continue
if len(data) != info[2]:
logger.info(
f"MetadataError: Thumbnail Size Mismatch: "
f"detected {info[2]}, actual {len(data)}")
continue
dest_ext = FMT_CONV_MAP.get(ext, ext)
thumb_name = f"{thumb_base}-{info[0]}x{info[1]}.{dest_ext}"
thumb_path = os.path.join(thumb_dir, thumb_name)
rel_thumb_path = os.path.join(".thumbs", thumb_name)
if dest_ext != ext:
# Convert image. Format is determined by destination file
# extension. Only formats supported by Pillow should be used.
with Image.open(io.BytesIO(base64.b64decode(data.encode()))) as im:
im.save(thumb_path)
else:
with open(thumb_path, "wb") as f:
f.write(base64.b64decode(data.encode()))
parsed_matches.append({
'width': info[0], 'height': info[1],
'size': os.path.getsize(thumb_path),
'relative_path': rel_thumb_path})
if info[0] == 32 and info[1] == 32:
has_miniature = True
for match in re.finditer(pattern, self._file_data):
ext = match.group(1).partition("_")[2].lower() or "png"
if ext not in SUPPORTED_THUMB_FORMATS:
logger.info(f"Unsupported thumbnail extension: {ext}")
continue
lines = re.split(r"\r?\n", match.group(2).replace('; ', ''))
info = regex_find_ints(r"(%D)", lines[0])
data = "".join(lines[1:-1])
if len(info) != 3:
logger.info(
f"MetadataError: Error parsing thumbnail header: {lines[0]}"
)
continue
if len(data) != info[2]:
logger.info(
f"MetadataError: Thumbnail Size Mismatch: "
f"detected {info[2]}, actual {len(data)}")
continue
dest_ext = FMT_CONV_MAP.get(ext, ext)
thumb_name = f"{thumb_base}-{info[0]}x{info[1]}.{dest_ext}"
thumb_path = os.path.join(thumb_dir, thumb_name)
rel_thumb_path = os.path.join(".thumbs", thumb_name)
if dest_ext != ext:
# Convert image. Format is determined by destination file
# extension. Only formats supported by Pillow should be used.
with Image.open(io.BytesIO(base64.b64decode(data.encode()))) as im:
im.save(thumb_path)
else:
with open(thumb_path, "wb") as f:
f.write(base64.b64decode(data.encode()))
parsed_matches.append({
'width': info[0], 'height': info[1],
'size': os.path.getsize(thumb_path),
'relative_path': rel_thumb_path})
if info[0] == 32 and info[1] == 32:
has_miniature = True
if not parsed_matches:
return None
if not has_miniature:
@@ -1039,7 +1038,6 @@ class KiriMoto(BaseSlicer):
)
READ_SIZE = 1024 * 1024 # 1 MiB
SUPPORTED_SLICERS: List[Type[BaseSlicer]] = [
PrusaSlicer, Slic3rPE, Slic3r, Cura, Simplify3D,
KISSlicer, IdeaMaker, IceSL, KiriMoto
@@ -1123,28 +1121,25 @@ def process_objects(file_path: str, slicer: BaseSlicer) -> bool:
return True
def get_slicer(file_path: str) -> BaseSlicer:
header_data = footer_data = ""
file_data = ""
slicer: Optional[BaseSlicer] = None
size = os.path.getsize(file_path)
with open(file_path, 'r') as f:
with open(file_path, 'rb') as f:
# read the default size, which should be enough to
# identify the slicer
header_data = f.read(READ_SIZE)
size = f.seek(0, os.SEEK_END)
f.seek(0)
file_data = f.read(READ_SIZE).decode(errors="ignore")
for impl in SUPPORTED_SLICERS:
slicer = impl(file_path)
if slicer.check_identity(header_data):
if slicer.check_identity(file_data):
break
else:
slicer = UnknownSlicer(file_path)
if size > READ_SIZE * 2:
f.seek(size - READ_SIZE)
footer_data = f.read()
elif size > READ_SIZE:
remaining = size - READ_SIZE
footer_data = header_data[remaining - READ_SIZE:] + f.read()
else:
footer_data = header_data
slicer.set_data(header_data, footer_data, size)
if size > READ_SIZE:
file_data += f.read().decode(errors="ignore")
slicer.set_data(file_data, size)
return slicer
def run_gcode_processors(