abdullahsamra commited on
Commit
ac5e306
·
1 Parent(s): aec67c1

Remove relative file URLs from API responses

Browse files
Files changed (1) hide show
  1. api.py +11 -9
api.py CHANGED
@@ -106,13 +106,12 @@ def make_file_meta(
106
  base_url: str | None = None,
107
  ) -> Dict[str, str]:
108
  rel_path = file_path.relative_to(JOBS_DIR / job_id).as_posix()
109
- relative_url = f"/files/jobs/{job_id}/{rel_path}"
110
  resolved_base_url = (base_url or get_public_base_url(request)).rstrip("/")
111
 
112
  return {
113
  "filename": file_path.name,
114
- "relative_url": relative_url,
115
- "download_url": f"{resolved_base_url}{relative_url}",
116
  }
117
 
118
  def clean_record(record: Dict[str, Any]) -> Dict[str, Any]:
@@ -533,6 +532,7 @@ def run_generate_job(job_id: str, req_data: Dict[str, Any], base_url: str):
533
  "error": detail,
534
  }, base_url=base_url)
535
 
 
536
  def rewrite_public_urls(payload: Dict[str, Any], base_url: str) -> Dict[str, Any]:
537
  base_url = base_url.rstrip("/")
538
 
@@ -546,15 +546,17 @@ def rewrite_public_urls(payload: Dict[str, Any], base_url: str) -> Dict[str, Any
546
  if isinstance(files, dict):
547
  for meta in files.values():
548
  if isinstance(meta, dict):
549
- relative_url = meta.get("relative_url")
550
- if relative_url:
551
- meta["download_url"] = f"{base_url}{relative_url}"
 
552
 
553
  file_meta = payload.get("file")
554
  if isinstance(file_meta, dict):
555
- relative_url = file_meta.get("relative_url")
556
- if relative_url:
557
- file_meta["download_url"] = f"{base_url}{relative_url}"
 
558
 
559
  return payload
560
 
 
106
  base_url: str | None = None,
107
  ) -> Dict[str, str]:
108
  rel_path = file_path.relative_to(JOBS_DIR / job_id).as_posix()
109
+ file_url = f"/files/jobs/{job_id}/{rel_path}"
110
  resolved_base_url = (base_url or get_public_base_url(request)).rstrip("/")
111
 
112
  return {
113
  "filename": file_path.name,
114
+ "download_url": f"{resolved_base_url}{file_url}",
 
115
  }
116
 
117
  def clean_record(record: Dict[str, Any]) -> Dict[str, Any]:
 
532
  "error": detail,
533
  }, base_url=base_url)
534
 
535
+
536
  def rewrite_public_urls(payload: Dict[str, Any], base_url: str) -> Dict[str, Any]:
537
  base_url = base_url.rstrip("/")
538
 
 
546
  if isinstance(files, dict):
547
  for meta in files.values():
548
  if isinstance(meta, dict):
549
+ value = meta.get("download_url")
550
+ if isinstance(value, str) and value.startswith(("http://localhost", "http://127.0.0.1")):
551
+ path = "/" + value.split("/", 3)[3] if "/" in value[8:] else ""
552
+ meta["download_url"] = f"{base_url}{path}"
553
 
554
  file_meta = payload.get("file")
555
  if isinstance(file_meta, dict):
556
+ value = file_meta.get("download_url")
557
+ if isinstance(value, str) and value.startswith(("http://localhost", "http://127.0.0.1")):
558
+ path = "/" + value.split("/", 3)[3] if "/" in value[8:] else ""
559
+ file_meta["download_url"] = f"{base_url}{path}"
560
 
561
  return payload
562