瀏覽代碼

Fix filename matching for files with spaces (Issue #218)

Bambu Studio converts spaces to underscores when saving files to the
printer, but MQTT reports the original name with spaces. This caused
FTP downloads to fail with "550 Failed to open file" because we were
searching for "Battery Storage_giesela.gcode.3mf" but the actual file
was "Battery_Storage_giesela.gcode.3mf".

Changes:
- Add underscore variants to direct download path attempts
- Normalize spaces/underscores in fallback directory search
- Apply fix to archive download, cover extraction, and objects reload

Closes #218
maziggy 3 月之前
父節點
當前提交
a2571aaf2e
共有 2 個文件被更改,包括 29 次插入1 次删除
  1. 18 0
      backend/app/api/routes/printers.py
  2. 11 1
      backend/app/main.py

+ 18 - 0
backend/app/api/routes/printers.py

@@ -595,6 +595,15 @@ async def get_printer_cover(
         possible_filenames.append(f"{subtask_name}.gcode.3mf")
         possible_filenames.append(f"{subtask_name}.3mf")
 
+    # Also try with spaces converted to underscores (Bambu Studio may normalize filenames)
+    if " " in subtask_name:
+        normalized = subtask_name.replace(" ", "_")
+        if normalized.endswith(".3mf"):
+            possible_filenames.append(normalized)
+        else:
+            possible_filenames.append(f"{normalized}.gcode.3mf")
+            possible_filenames.append(f"{normalized}.3mf")
+
     # Build list of all remote paths to try
     remote_paths = []
     for filename in possible_filenames:
@@ -1574,6 +1583,15 @@ async def get_printable_objects(
                 possible_filenames.append(f"{subtask_name}.gcode.3mf")
                 possible_filenames.append(f"{subtask_name}.3mf")
 
+            # Also try with spaces converted to underscores (Bambu Studio may normalize filenames)
+            if " " in subtask_name:
+                normalized = subtask_name.replace(" ", "_")
+                if normalized.endswith(".3mf"):
+                    possible_filenames.append(normalized)
+                else:
+                    possible_filenames.append(f"{normalized}.gcode.3mf")
+                    possible_filenames.append(f"{normalized}.3mf")
+
             # Download 3MF from printer
             temp_path = settings.archive_dir / "temp" / f"objects_{printer_id}_{possible_filenames[0]}"
             temp_path.parent.mkdir(parents=True, exist_ok=True)

+ 11 - 1
backend/app/main.py

@@ -1002,6 +1002,13 @@ async def on_print_start(printer_id: int, data: dict):
                 possible_names.append(f"{fname}.gcode.3mf")
                 possible_names.append(f"{fname}.3mf")
 
+        # Also try with spaces converted to underscores (Bambu Studio may normalize filenames)
+        space_variants = []
+        for name in possible_names:
+            if " " in name:
+                space_variants.append(name.replace(" ", "_"))
+        possible_names.extend(space_variants)
+
         # Remove duplicates while preserving order
         seen = set()
         possible_names = [x for x in possible_names if not (x in seen or seen.add(x))]
@@ -1085,7 +1092,10 @@ async def on_print_start(printer_id: int, data: dict):
                         if f.get("is_directory"):
                             continue
                         fname = f.get("name", "")
-                        if fname.endswith(".3mf") and search_term in fname.lower():
+                        # Normalize both for comparison (spaces and underscores are equivalent)
+                        fname_normalized = fname.lower().replace(" ", "_")
+                        search_normalized = search_term.replace(" ", "_")
+                        if fname.endswith(".3mf") and search_normalized in fname_normalized:
                             logger.info(f"Found matching file in {search_dir}: {fname}")
                             temp_path = app_settings.archive_dir / "temp" / fname
                             temp_path.parent.mkdir(parents=True, exist_ok=True)