1
0
Fork 0
mirror of https://github.com/maziggy/bambuddy.git synced 2026-05-09 08:25:54 +02:00

[GH-ISSUE #846] [Bug]: scan_external_folder stores bytes in file_metadata, causing 500 error #576

Closed
opened 2026-05-07 00:11:47 +02:00 by BreizhHardware · 2 comments

Originally created by @SMAW on GitHub (Mar 29, 2026).
Original GitHub issue: https://github.com/maziggy/bambuddy/issues/846

Originally assigned to: @maziggy on GitHub.

Bug Description

When scanning an external folder that contains a .3mf file, Bambuddy raises a 500 server error due to a TypeError: 'Object of type bytes is not JSON serializable'. The root cause is that the metadata parsed from ThreeMFParser.parse() is stored in file_metadata without being cleaned, and still contains a _thumbnail_data key with raw bytes. These bytes are not JSON serializable, which causes the failure.

Other code paths (upload_file, extract_zip_file) use a clean_metadata() helper to remove bytes before storing, but scan_external_folder does not. Additionally, scan_external_folder tries to call parser.extract_thumbnail(), but this method does not exist as a public API, causing additional issues.

Expected Behavior

scan_external_folder and the resulting file_metadata should work like other endpoints: metadata should be cleaned of all bytes fields (including _thumbnail_data and _thumbnail_ext) before being stored in the database. The scan should succeed, and .3mf metadata should be JSON serializable.

Steps to Reproduce

  1. Place a .3mf file in an external folder managed by Bambuddy.
  2. Trigger scan_external_folder via the Web-gui
  3. Observe a 500 Internal Server Error and a traceback mentioning 'Object of type bytes is not JSON serializable'.

Printer Model

P1S

Bambuddy Version

0.2.2.2

Printer Firmware Version

N/A (not relevant to this bug)

Installation Method

Docker

Operating System

Docker

Relevant Logs / Support Package

Example error:
Traceback (most recent call last):
...
TypeError: Object of type bytes is not JSON serializable
...

Screenshots

No response

Additional Context

Root Cause Analysis

Affected codescan_external_folder in backend/app/api/routes/library.py (around line 838):

if file_type == "3mf":
    try:
        parser = ThreeMFParser(str(filepath))
        meta = parser.parse()
        if meta:
            file_metadata = meta       # ← raw metadata, still contains _thumbnail_data (bytes)!
        thumb_data = parser.extract_thumbnail()  # ← this method does not exist as public API

ThreeMFParser.parse() calls _extract_thumbnail() internally, which stores raw PNG bytes in self.metadata["_thumbnail_data"]. The returned dict is assigned directly to file_metadata without cleanup, causing json.dumps() to fail when SQLAlchemy tries to serialize it into the JSON column.

Working codeextract_zip_file and upload_file in the same file use a clean_metadata() helper (around line 1304):

def clean_metadata(obj):
    if isinstance(obj, dict):
        return {
            k: clean_metadata(v)
            for k, v in obj.items()
            if not isinstance(v, bytes) and k not in ("_thumbnail_data", "_thumbnail_ext")
        }
    elif isinstance(obj, list):
        return [clean_metadata(i) for i in obj if not isinstance(i, bytes)]
    elif isinstance(obj, bytes):
        return None
    return obj

metadata = clean_metadata(raw_metadata)

Suggested PR plan

  • Extract _thumbnail_data / _thumbnail_ext from the parsed metadata to save the thumbnail file (same pattern as other endpoints).
  • Apply the same clean_metadata() helper before storing file_metadata in the database.
  • Remove or fix the call to parser.extract_thumbnail() — it does not exist as a public method; thumbnail data is already present in metadata after parse().

Checklist

  • I have searched existing issues to ensure this bug hasn't already been reported
  • I am using the latest version of Bambuddy
  • My printer is set to LAN Only mode
  • My printer has Developer Mode enabled
Originally created by @SMAW on GitHub (Mar 29, 2026). Original GitHub issue: https://github.com/maziggy/bambuddy/issues/846 Originally assigned to: @maziggy on GitHub. ### Bug Description When scanning an external folder that contains a .3mf file, Bambuddy raises a 500 server error due to a TypeError: 'Object of type bytes is not JSON serializable'. The root cause is that the metadata parsed from ThreeMFParser.parse() is stored in file_metadata without being cleaned, and still contains a `_thumbnail_data` key with raw bytes. These bytes are not JSON serializable, which causes the failure. Other code paths (upload_file, extract_zip_file) use a `clean_metadata()` helper to remove bytes before storing, but `scan_external_folder` does not. Additionally, `scan_external_folder` tries to call `parser.extract_thumbnail()`, but this method does not exist as a public API, causing additional issues. ### Expected Behavior scan_external_folder and the resulting file_metadata should work like other endpoints: metadata should be cleaned of all bytes fields (including _thumbnail_data and _thumbnail_ext) before being stored in the database. The scan should succeed, and .3mf metadata should be JSON serializable. ### Steps to Reproduce 1. Place a .3mf file in an external folder managed by Bambuddy. 2. Trigger scan_external_folder via the Web-gui 3. Observe a 500 Internal Server Error and a traceback mentioning 'Object of type bytes is not JSON serializable'. ### Printer Model P1S ### Bambuddy Version 0.2.2.2 ### Printer Firmware Version N/A (not relevant to this bug) ### Installation Method Docker ### Operating System Docker ### Relevant Logs / Support Package Example error: Traceback (most recent call last): ... TypeError: Object of type bytes is not JSON serializable ... ### Screenshots _No response_ ### Additional Context ### Root Cause Analysis **Affected code** — `scan_external_folder` in `backend/app/api/routes/library.py` (around line 838): ```python if file_type == "3mf": try: parser = ThreeMFParser(str(filepath)) meta = parser.parse() if meta: file_metadata = meta # ← raw metadata, still contains _thumbnail_data (bytes)! thumb_data = parser.extract_thumbnail() # ← this method does not exist as public API ``` `ThreeMFParser.parse()` calls `_extract_thumbnail()` internally, which stores raw PNG bytes in `self.metadata["_thumbnail_data"]`. The returned dict is assigned directly to `file_metadata` without cleanup, causing `json.dumps()` to fail when SQLAlchemy tries to serialize it into the JSON column. **Working code** — `extract_zip_file` and `upload_file` in the same file use a `clean_metadata()` helper (around line 1304): ```python def clean_metadata(obj): if isinstance(obj, dict): return { k: clean_metadata(v) for k, v in obj.items() if not isinstance(v, bytes) and k not in ("_thumbnail_data", "_thumbnail_ext") } elif isinstance(obj, list): return [clean_metadata(i) for i in obj if not isinstance(i, bytes)] elif isinstance(obj, bytes): return None return obj metadata = clean_metadata(raw_metadata) ``` ### Suggested PR plan - Extract `_thumbnail_data` / `_thumbnail_ext` from the parsed metadata to save the thumbnail file (same pattern as other endpoints). - Apply the same `clean_metadata()` helper before storing `file_metadata` in the database. - Remove or fix the call to `parser.extract_thumbnail()` — it does not exist as a public method; thumbnail data is already present in metadata after `parse()`. ### Checklist - [x] I have searched existing issues to ensure this bug hasn't already been reported - [x] I am using the latest version of Bambuddy - [x] My printer is set to LAN Only mode - [x] My printer has Developer Mode enabled
BreizhHardware 2026-05-07 00:11:47 +02:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@SMAW commented on GitHub (Mar 29, 2026):

Same as #844 I think, only in english :) IT just generates an 500 internal server error when scanning the folder

<!-- gh-comment-id:4150580257 --> @SMAW commented on GitHub (Mar 29, 2026): Same as #844 I think, only in english :) IT just generates an 500 internal server error when scanning the folder
Author
Owner

@maziggy commented on GitHub (Mar 30, 2026):

Available/Fixed in branch dev and available with the next release or daily build.


If you find Bambuddy useful, please consider giving it a on GitHub — it helps others discover the project!

<!-- gh-comment-id:4152627579 --> @maziggy commented on GitHub (Mar 30, 2026): Available/Fixed in branch dev and available with the next release or daily build. ----- If you find Bambuddy useful, please consider giving it a ⭐ on [GitHub](https://github.com/maziggy/bambuddy) — it helps others discover the project!
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/bambuddy-maziggy-1#576
No description provided.