From dce9c8bc0673638eb529458642d81ae28f91094a Mon Sep 17 00:00:00 2001 From: maziggy Date: Sun, 1 Feb 2026 16:08:49 +0100 Subject: [PATCH] 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 --- backend/app/api/routes/printers.py | 18 ++++++++++++++++++ backend/app/main.py | 12 +++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/backend/app/api/routes/printers.py b/backend/app/api/routes/printers.py index 3f924859f..fa890f1d2 100644 --- a/backend/app/api/routes/printers.py +++ b/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) diff --git a/backend/app/main.py b/backend/app/main.py index 5f534411e..e36d5cc89 100644 --- a/backend/app/main.py +++ b/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)