Merge pull request 'feat: add session image retrieval endpoint' (#27) from dev into main
Reviewed-on: #27
This commit is contained in:
		
						commit
						df38e90ff2
					
				
					 1 changed files with 57 additions and 0 deletions
				
			
		
							
								
								
									
										57
									
								
								app.py
									
										
									
									
									
								
							
							
						
						
									
										57
									
								
								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."""
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue