Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions flashforge/tcp/parsers/thumbnail_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

Handles the parsing, storage, and manipulation of 3D print file thumbnail images.
"""
import asyncio
import base64
from pathlib import Path
from typing import Optional
Expand Down Expand Up @@ -119,6 +120,12 @@ def to_base64_data_url(self) -> Optional[str]:
base64_data = base64.b64encode(self._image_data).decode('ascii')
return f"data:image/png;base64,{base64_data}"

@staticmethod
def _write_file_sync(file_path: str, data: bytes) -> None:
"""Helper to write bytes to a file synchronously."""
with open(file_path, 'wb') as f:
f.write(data)

async def save_to_file(self, file_path: Optional[str] = None) -> bool:
"""
Saves the thumbnail image data to a file.
Expand Down Expand Up @@ -149,9 +156,9 @@ async def save_to_file(self, file_path: Optional[str] = None) -> bool:
print("ThumbnailInfo: No file path provided and no filename to generate one from")
return False

# Write the bytes to file
with open(file_path, 'wb') as f:
f.write(self._image_data)
# Write the bytes to file in a separate thread to avoid blocking the event loop
loop = asyncio.get_running_loop()
await loop.run_in_executor(None, self._write_file_sync, file_path, self._image_data)

print(f"ThumbnailInfo: Saved thumbnail to {file_path}")
return True
Expand Down