fix: extract zip locally with Python zipfile � no unzip required on remote

This commit is contained in:
2026-04-26 00:59:59 +00:00
parent ac60597e65
commit ace187d2b2

View File

@@ -1,4 +1,5 @@
import asyncio import asyncio
import zipfile
import json import json
import os import os
import tempfile import tempfile
@@ -205,17 +206,24 @@ async def upload_files(files: List[UploadFile] = File(...)):
tmp_path = tmp.name tmp_path = tmp.name
try: try:
if suffix == ".zip": if suffix == ".zip":
remote_tmp = f"/tmp/{file.filename}" extracted = []
ssh_manager.upload_file(tmp_path, remote_tmp) with zipfile.ZipFile(tmp_path) as zf:
input_dir = STAGE_DIRS["input"] for member in zf.infolist():
out, err, code = ssh_manager.execute( if member.is_dir():
f"unzip -o '{remote_tmp}' -d '{input_dir}' " continue
f"&& rm '{remote_tmp}'", name = Path(member.filename).name
use_conda=False, if not name or name.startswith("."):
) continue
if code != 0: with zf.open(member) as src, tempfile.NamedTemporaryFile(delete=False, suffix=Path(name).suffix) as out_tmp:
raise HTTPException(status_code=500, detail=f"Unzip failed for {file.filename}: {err}") out_tmp.write(src.read())
results.append({"file": file.filename, "action": "extracted"}) out_tmp_path = out_tmp.name
try:
remote_path = f"{STAGE_DIRS['input']}/{name}"
ssh_manager.upload_file(out_tmp_path, remote_path)
extracted.append(name)
finally:
os.unlink(out_tmp_path)
results.append({"file": file.filename, "action": "extracted", "files": extracted})
else: else:
remote_path = f"{STAGE_DIRS['input']}/{file.filename}" remote_path = f"{STAGE_DIRS['input']}/{file.filename}"
ssh_manager.upload_file(tmp_path, remote_path) ssh_manager.upload_file(tmp_path, remote_path)