select contents for four-state zip package
This commit is contained in:
@@ -588,7 +588,60 @@ def file_metadata(file_path):
|
||||
}
|
||||
|
||||
|
||||
def prepare_deformation_zip(job_id, target):
|
||||
def zip_selected_deformation_outputs(job_root, outputs, previews, package_options):
|
||||
dicom_keys = package_options.get("dicom") or []
|
||||
image_keys = package_options.get("images") or []
|
||||
if not dicom_keys and not image_keys:
|
||||
raise RuntimeError("请至少选择一个要打包的内容。")
|
||||
|
||||
zip_path = Path(job_root) / f"head_ct_morph_selected_{Path(job_root).name}.zip"
|
||||
if zip_path.exists():
|
||||
zip_path.unlink()
|
||||
|
||||
dicom_names = {
|
||||
"original": "dicom/ct_original",
|
||||
"hard_boundary": "dicom/ct_hard_boundary",
|
||||
"gaussian_smooth": "dicom/ct_gaussian_smooth",
|
||||
"soft_transition": "dicom/ct_soft_transition",
|
||||
}
|
||||
image_files = {
|
||||
"comparison": Path(previews.get("comparison", "")),
|
||||
"original": Path(previews.get("screenshots", "")) / "original.png",
|
||||
"hard_boundary": Path(previews.get("screenshots", "")) / "hard_boundary.png",
|
||||
"gaussian_smooth": Path(previews.get("screenshots", "")) / "gaussian_smooth.png",
|
||||
"soft_transition": Path(previews.get("screenshots", "")) / "soft_transition.png",
|
||||
}
|
||||
image_names = {
|
||||
"comparison": "images/process_comparison_4states.png",
|
||||
"original": "images/original.png",
|
||||
"hard_boundary": "images/hard_boundary.png",
|
||||
"gaussian_smooth": "images/gaussian_smooth.png",
|
||||
"soft_transition": "images/soft_transition.png",
|
||||
}
|
||||
|
||||
with zipfile.ZipFile(zip_path, "w", compression=zipfile.ZIP_DEFLATED) as archive:
|
||||
for key in dicom_keys:
|
||||
if key not in dicom_names:
|
||||
continue
|
||||
source_dir = Path(outputs.get(key, ""))
|
||||
if not source_dir.exists():
|
||||
raise RuntimeError(f"{key} 的 DICOM 输出目录不存在。")
|
||||
for file_path in source_dir.rglob("*"):
|
||||
if file_path.is_file():
|
||||
archive.write(file_path, Path(dicom_names[key]) / file_path.relative_to(source_dir))
|
||||
|
||||
for key in image_keys:
|
||||
if key not in image_files:
|
||||
continue
|
||||
source_file = image_files[key]
|
||||
if not source_file.exists():
|
||||
raise RuntimeError(f"{key} 的图片输出不存在。")
|
||||
archive.write(source_file, image_names[key])
|
||||
|
||||
return zip_path
|
||||
|
||||
|
||||
def prepare_deformation_zip(job_id, target, package_options=None):
|
||||
job = get_job(job_id)
|
||||
if not job:
|
||||
raise RuntimeError("任务不存在。")
|
||||
@@ -599,8 +652,11 @@ def prepare_deformation_zip(job_id, target):
|
||||
|
||||
result = job.get("result") or {}
|
||||
outputs = result.get("outputs") or {}
|
||||
previews = result.get("previews") or {}
|
||||
job_root = RESULT_DIR / job_id
|
||||
if target == "all":
|
||||
if package_options:
|
||||
return zip_selected_deformation_outputs(job_root, outputs, previews, package_options)
|
||||
output_dir = job_root / "four_state_output"
|
||||
if not output_dir.exists():
|
||||
raise RuntimeError("四状态输出目录不存在。")
|
||||
@@ -736,12 +792,13 @@ class Handler(BaseHTTPRequestHandler):
|
||||
if parsed.path == "/api/deformation/package":
|
||||
source_job_id = body["jobId"]
|
||||
target = body.get("target", "all")
|
||||
package_options = body.get("packageOptions")
|
||||
username = normalized_username(body.get("username"))
|
||||
|
||||
def worker(job_id):
|
||||
label = "四状态总 ZIP" if target == "all" else "本状态 DICOM ZIP"
|
||||
set_job(job_id, message=f"正在打包{label}...", progress=10)
|
||||
zip_path = prepare_deformation_zip(source_job_id, target)
|
||||
zip_path = prepare_deformation_zip(source_job_id, target, package_options)
|
||||
set_job(job_id, message="打包完成,准备下载...", progress=95)
|
||||
return {"file": file_metadata(zip_path)}
|
||||
|
||||
@@ -753,6 +810,7 @@ class Handler(BaseHTTPRequestHandler):
|
||||
params={
|
||||
"sourceJobId": source_job_id,
|
||||
"target": target,
|
||||
"packageOptions": package_options,
|
||||
},
|
||||
remember_user_task=False,
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user