Code-API commited on
Commit
a81801b
·
1 Parent(s): 64a008c

deploy: auto-deploy 17:13:13

Browse files
Files changed (1) hide show
  1. app/api/v1/reconcile.py +43 -11
app/api/v1/reconcile.py CHANGED
@@ -88,14 +88,32 @@ def _failed_result(pair_index: int, error: str) -> Dict[str, Any]:
88
 
89
 
90
  def _validate_extensions(src_ext: str, dst_ext: str, idx: int) -> None:
 
 
 
 
91
  if src_ext not in SUPPORTED_EXTENSIONS:
92
- raise HTTPException(status_code=400, detail={"success": False, "message": f"Pair {idx}: Unsupported source format '{src_ext}'."})
93
  if dst_ext not in SUPPORTED_EXTENSIONS:
94
- raise HTTPException(status_code=400, detail={"success": False, "message": f"Pair {idx}: Unsupported destination format '{dst_ext}'."})
95
  if src_ext != dst_ext:
96
  raise HTTPException(status_code=400, detail={"success": False, "message": f"Pair {idx}: File type mismatch - source '{src_ext}' != destination '{dst_ext}'."})
97
 
98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  def _build_response(results: List[Dict[str, Any]], start_time: float) -> ReconciliationResponse:
100
  failed = sum(1 for r in results if r["status"] == "FAILED")
101
  return ReconciliationResponse(
@@ -240,10 +258,16 @@ async def reconcile_files(
240
  for idx, (src, dst, cm) in enumerate(files, 1):
241
  if src is None and dst is None:
242
  continue
243
- if src is None or dst is None:
244
- raise HTTPException(status_code=400, detail={"success": False, "message": f"Pair {idx}: Both source and destination files required."})
245
- src_ext = extract_extension(src.filename or "")
246
- dst_ext = extract_extension(dst.filename or "")
 
 
 
 
 
 
247
  _validate_extensions(src_ext, dst_ext, idx)
248
  pairs.append((src, dst, cm, idx, src_ext, dst_ext))
249
 
@@ -254,8 +278,8 @@ async def reconcile_files(
254
  async with semaphore_jobs:
255
  src_data = await src.read()
256
  dst_data = await dst.read()
257
- if len(src_data) > _MAX_UPLOAD_BYTES or len(dst_data) > _MAX_UPLOAD_BYTES:
258
- return _failed_result(idx, "File size exceeds maximum allowed limit")
259
  mapping = _parse_column_mapping_str(cm_str)
260
  return await _process_file_pair(src_data, dst_data, src_ext, dst_ext, mapping, idx)
261
 
@@ -280,7 +304,15 @@ async def reconcile_urls(
280
  raise HTTPException(status_code=400, detail={"success": False, "message": "At least 1 pair is required."})
281
 
282
  for idx, pair in enumerate(body.pairs, 1):
283
- _validate_extensions(extract_extension(pair.source), extract_extension(pair.destination), idx)
 
 
 
 
 
 
 
 
284
 
285
  async with aiohttp.ClientSession() as session:
286
  async def process_url_pair(pair: ReconciliationPair, idx: int) -> Dict[str, Any]:
@@ -296,8 +328,8 @@ async def reconcile_urls(
296
  except Exception as e:
297
  return _failed_result(idx, str(e))
298
 
299
- if len(src_data) > _MAX_UPLOAD_BYTES or len(dst_data) > _MAX_UPLOAD_BYTES:
300
- return _failed_result(idx, "File size exceeds maximum allowed limit")
301
 
302
  mapping = _parse_column_mapping_model(pair.column_mapping)
303
  return await _process_file_pair(src_data, dst_data, src_ext, dst_ext, mapping, idx)
 
88
 
89
 
90
  def _validate_extensions(src_ext: str, dst_ext: str, idx: int) -> None:
91
+ if not src_ext:
92
+ raise HTTPException(status_code=400, detail={"success": False, "message": f"Pair {idx}: Source file has no recognizable extension."})
93
+ if not dst_ext:
94
+ raise HTTPException(status_code=400, detail={"success": False, "message": f"Pair {idx}: Destination file has no recognizable extension."})
95
  if src_ext not in SUPPORTED_EXTENSIONS:
96
+ raise HTTPException(status_code=400, detail={"success": False, "message": f"Pair {idx}: Unsupported source format '{src_ext}'. Supported: {', '.join(sorted(SUPPORTED_EXTENSIONS))}."})
97
  if dst_ext not in SUPPORTED_EXTENSIONS:
98
+ raise HTTPException(status_code=400, detail={"success": False, "message": f"Pair {idx}: Unsupported destination format '{dst_ext}'. Supported: {', '.join(sorted(SUPPORTED_EXTENSIONS))}."})
99
  if src_ext != dst_ext:
100
  raise HTTPException(status_code=400, detail={"success": False, "message": f"Pair {idx}: File type mismatch - source '{src_ext}' != destination '{dst_ext}'."})
101
 
102
 
103
+ def _validate_file_data(data: bytes, label: str, pair_index: int) -> None:
104
+ if not data:
105
+ raise HTTPException(status_code=400, detail={"success": False, "message": f"Pair {pair_index}: {label} file is empty."})
106
+ if len(data) > _MAX_UPLOAD_BYTES:
107
+ raise HTTPException(status_code=413, detail={"success": False, "message": f"Pair {pair_index}: {label} file exceeds {_settings.max_upload_mb} MB limit."})
108
+
109
+
110
+ def _validate_url(url: str, label: str, pair_index: int) -> None:
111
+ if not url or not url.strip():
112
+ raise HTTPException(status_code=400, detail={"success": False, "message": f"Pair {pair_index}: {label} URL is empty."})
113
+ if not url.lower().startswith(("http://", "https://")):
114
+ raise HTTPException(status_code=400, detail={"success": False, "message": f"Pair {pair_index}: {label} URL must start with http:// or https://."})
115
+
116
+
117
  def _build_response(results: List[Dict[str, Any]], start_time: float) -> ReconciliationResponse:
118
  failed = sum(1 for r in results if r["status"] == "FAILED")
119
  return ReconciliationResponse(
 
258
  for idx, (src, dst, cm) in enumerate(files, 1):
259
  if src is None and dst is None:
260
  continue
261
+ if src is None:
262
+ raise HTTPException(status_code=400, detail={"success": False, "message": f"Pair {idx}: Source file is missing."})
263
+ if dst is None:
264
+ raise HTTPException(status_code=400, detail={"success": False, "message": f"Pair {idx}: Destination file is missing."})
265
+ if not src.filename:
266
+ raise HTTPException(status_code=400, detail={"success": False, "message": f"Pair {idx}: Source file has no filename."})
267
+ if not dst.filename:
268
+ raise HTTPException(status_code=400, detail={"success": False, "message": f"Pair {idx}: Destination file has no filename."})
269
+ src_ext = extract_extension(src.filename)
270
+ dst_ext = extract_extension(dst.filename)
271
  _validate_extensions(src_ext, dst_ext, idx)
272
  pairs.append((src, dst, cm, idx, src_ext, dst_ext))
273
 
 
278
  async with semaphore_jobs:
279
  src_data = await src.read()
280
  dst_data = await dst.read()
281
+ _validate_file_data(src_data, f"Pair {idx} source", idx)
282
+ _validate_file_data(dst_data, f"Pair {idx} destination", idx)
283
  mapping = _parse_column_mapping_str(cm_str)
284
  return await _process_file_pair(src_data, dst_data, src_ext, dst_ext, mapping, idx)
285
 
 
304
  raise HTTPException(status_code=400, detail={"success": False, "message": "At least 1 pair is required."})
305
 
306
  for idx, pair in enumerate(body.pairs, 1):
307
+ if not pair.source or not pair.source.strip():
308
+ raise HTTPException(status_code=400, detail={"success": False, "message": f"Pair {idx}: Source URL is empty."})
309
+ if not pair.destination or not pair.destination.strip():
310
+ raise HTTPException(status_code=400, detail={"success": False, "message": f"Pair {idx}: Destination URL is empty."})
311
+ _validate_url(pair.source, "Source", idx)
312
+ _validate_url(pair.destination, "Destination", idx)
313
+ src_ext = extract_extension(pair.source)
314
+ dst_ext = extract_extension(pair.destination)
315
+ _validate_extensions(src_ext, dst_ext, idx)
316
 
317
  async with aiohttp.ClientSession() as session:
318
  async def process_url_pair(pair: ReconciliationPair, idx: int) -> Dict[str, Any]:
 
328
  except Exception as e:
329
  return _failed_result(idx, str(e))
330
 
331
+ _validate_file_data(src_data, f"Pair {idx} source", idx)
332
+ _validate_file_data(dst_data, f"Pair {idx} destination", idx)
333
 
334
  mapping = _parse_column_mapping_model(pair.column_mapping)
335
  return await _process_file_pair(src_data, dst_data, src_ext, dst_ext, mapping, idx)