From 72f3d4a55e7a75abb06acffd8235d5ca27e08406 Mon Sep 17 00:00:00 2001 From: Phil Skentelbery Date: Tue, 16 Dec 2025 18:06:51 -0700 Subject: [PATCH] fix(media): Save failed uploads to debug/ for analysis - v1.4.2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When an image fails both Pillow and HEIC parsing, save the file to data/debug/ for manual analysis. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- starpunk/media.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/starpunk/media.py b/starpunk/media.py index 216ccee..2a8073a 100644 --- a/starpunk/media.py +++ b/starpunk/media.py @@ -122,13 +122,19 @@ def validate_image(file_data: bytes, filename: str) -> Tuple[bytes, str, int, in # Mark as HEIF so conversion happens below img.format = 'HEIF' except Exception as heic_error: - # Log the magic bytes for debugging (if in app context) + # Log the magic bytes and save file for debugging (if in app context) try: magic = file_data[:12].hex() if len(file_data) >= 12 else file_data.hex() current_app.logger.warning( f'Media upload failed both Pillow and HEIC: filename="{filename}", ' f'magic_bytes={magic}, pillow_error="{e}", heic_error="{heic_error}"' ) + # Save failed file for analysis + debug_dir = Path(current_app.config.get('DATA_PATH', 'data')) / 'debug' + debug_dir.mkdir(parents=True, exist_ok=True) + debug_file = debug_dir / f"failed_{datetime.now().strftime('%Y%m%d_%H%M%S')}_{filename}" + debug_file.write_bytes(file_data) + current_app.logger.info(f'Saved failed upload for analysis: {debug_file}') except RuntimeError: pass # Outside app context (e.g., tests) raise ValueError(f"Invalid or corrupted image: {e}")