|
13 | 13 | from fastapi.responses import JSONResponse |
14 | 14 | from gold_module.gold_container import GoldContainer |
15 | 15 | from gold_module.services.image_service import ImageService |
16 | | -from gold_module.models.gold_image_request import ImageAnalysisRequest |
| 16 | +from gold_module.models.gold_image_request import ( |
| 17 | + ImageAnalysisRequest, |
| 18 | + AdhocImageUploadRequest, |
| 19 | +) |
17 | 20 |
|
18 | 21 | image_controller = APIRouter() |
19 | 22 |
|
@@ -88,3 +91,68 @@ async def process_image( |
88 | 91 | status_code=status.HTTP_200_OK, |
89 | 92 | content=response_formatter.buildSuccessResponse(result), |
90 | 93 | ) |
| 94 | + |
| 95 | + |
| 96 | +@image_controller.post('/historical_images') |
| 97 | +@inject |
| 98 | +async def upload_historical_images( |
| 99 | + request: AdhocImageUploadRequest, |
| 100 | + image_service: ImageService = Depends(Provide[GoldContainer.image_service]), |
| 101 | + response_formatter: ResponseFormatter = Depends( |
| 102 | + Provide[CommonContainer.response_formatter] |
| 103 | + ), |
| 104 | +): |
| 105 | + image_str = request.image |
| 106 | + image_name = request.loan_id |
| 107 | + |
| 108 | + gold_image = None |
| 109 | + # Check for data URL (base64 with MIME) |
| 110 | + data_url_pattern = r'^data:(image/\w+);base64,(.+)' |
| 111 | + match = re.match(data_url_pattern, image_str) |
| 112 | + if match: |
| 113 | + try: |
| 114 | + gold_image = base64.b64decode(match.group(2)) |
| 115 | + except Exception: |
| 116 | + return JSONResponse( |
| 117 | + status_code=status.HTTP_400_BAD_REQUEST, |
| 118 | + content=response_formatter.buildErrorResponse( |
| 119 | + 'Invalid base64 image encoding' |
| 120 | + ), |
| 121 | + ) |
| 122 | + elif image_str.startswith('http://') or image_str.startswith('https://'): |
| 123 | + # Download the image from the URL |
| 124 | + try: |
| 125 | + async with httpx.AsyncClient() as client: |
| 126 | + resp = await client.get(image_str) |
| 127 | + if resp.status_code != 200: |
| 128 | + return JSONResponse( |
| 129 | + status_code=status.HTTP_400_BAD_REQUEST, |
| 130 | + content=response_formatter.buildErrorResponse( |
| 131 | + 'Failed to download image from URL' |
| 132 | + ), |
| 133 | + ) |
| 134 | + gold_image = resp.content |
| 135 | + except Exception: |
| 136 | + return JSONResponse( |
| 137 | + status_code=status.HTTP_400_BAD_REQUEST, |
| 138 | + content=response_formatter.buildErrorResponse( |
| 139 | + 'Error downloading image from URL' |
| 140 | + ), |
| 141 | + ) |
| 142 | + else: |
| 143 | + return JSONResponse( |
| 144 | + status_code=status.HTTP_400_BAD_REQUEST, |
| 145 | + content=response_formatter.buildErrorResponse( |
| 146 | + 'Image must be a data URL or a direct image URL' |
| 147 | + ), |
| 148 | + ) |
| 149 | + if not gold_image: |
| 150 | + return JSONResponse( |
| 151 | + status_code=status.HTTP_400_BAD_REQUEST, |
| 152 | + content=response_formatter.buildErrorResponse('Empty image file'), |
| 153 | + ) |
| 154 | + result = await image_service.save_image(gold_image, image_name) |
| 155 | + return JSONResponse( |
| 156 | + status_code=status.HTTP_200_OK, |
| 157 | + content=response_formatter.buildSuccessResponse(result), |
| 158 | + ) |
0 commit comments