From b2e7bc499d5edbaab724fc0e596ef8824671b9ac Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Wed, 1 Oct 2025 01:27:12 +0700 Subject: [PATCH] feat: add session image retrieval endpoint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add HTTP endpoint to retrieve saved session images by session ID. Images are saved during car_fueling progression stage. - Add GET /session-image/{session_id} endpoint - Search images directory for files matching session ID pattern - Return most recent image if multiple exist - Proper error handling (404 for not found, 500 for errors) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- app.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/app.py b/app.py index 7b82d23..21d89db 100644 --- a/app.py +++ b/app.py @@ -302,6 +302,63 @@ async def get_camera_image(camera_id: str): raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}") +@app.get("/session-image/{session_id}") +async def get_session_image(session_id: int): + """ + HTTP endpoint to retrieve the saved session image by session ID. + + Args: + session_id: The session ID to retrieve the image for + + Returns: + JPEG image as binary response + + Raises: + HTTPException: 404 if no image found for the session + HTTPException: 500 if reading image fails + """ + try: + from pathlib import Path + import glob + + # Images directory + images_dir = Path("images") + + if not images_dir.exists(): + logger.warning(f"Images directory does not exist") + raise HTTPException( + status_code=404, + detail=f"No images directory found" + ) + + # Search for files matching session ID pattern: {session_id}_* + pattern = str(images_dir / f"{session_id}_*.jpg") + matching_files = glob.glob(pattern) + + if not matching_files: + logger.warning(f"No image found for session {session_id}") + raise HTTPException( + status_code=404, + detail=f"No image found for session {session_id}" + ) + + # Get the most recent file if multiple exist + most_recent_file = max(matching_files, key=os.path.getmtime) + logger.info(f"Found session image for session {session_id}: {most_recent_file}") + + # Read the image file + image_data = open(most_recent_file, 'rb').read() + + # Return image as binary response + return Response(content=image_data, media_type="image/jpeg") + + except HTTPException: + raise + except Exception as e: + logger.error(f"Error retrieving session image for session {session_id}: {str(e)}", exc_info=True) + raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}") + + @app.get("/health") async def health_check(): """Health check endpoint for monitoring."""