fix: extract zip locally with Python zipfile � no unzip required on remote
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import asyncio
|
||||
import zipfile
|
||||
import json
|
||||
import os
|
||||
import tempfile
|
||||
@@ -205,17 +206,24 @@ async def upload_files(files: List[UploadFile] = File(...)):
|
||||
tmp_path = tmp.name
|
||||
try:
|
||||
if suffix == ".zip":
|
||||
remote_tmp = f"/tmp/{file.filename}"
|
||||
ssh_manager.upload_file(tmp_path, remote_tmp)
|
||||
input_dir = STAGE_DIRS["input"]
|
||||
out, err, code = ssh_manager.execute(
|
||||
f"unzip -o '{remote_tmp}' -d '{input_dir}' "
|
||||
f"&& rm '{remote_tmp}'",
|
||||
use_conda=False,
|
||||
)
|
||||
if code != 0:
|
||||
raise HTTPException(status_code=500, detail=f"Unzip failed for {file.filename}: {err}")
|
||||
results.append({"file": file.filename, "action": "extracted"})
|
||||
extracted = []
|
||||
with zipfile.ZipFile(tmp_path) as zf:
|
||||
for member in zf.infolist():
|
||||
if member.is_dir():
|
||||
continue
|
||||
name = Path(member.filename).name
|
||||
if not name or name.startswith("."):
|
||||
continue
|
||||
with zf.open(member) as src, tempfile.NamedTemporaryFile(delete=False, suffix=Path(name).suffix) as out_tmp:
|
||||
out_tmp.write(src.read())
|
||||
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:
|
||||
remote_path = f"{STAGE_DIRS['input']}/{file.filename}"
|
||||
ssh_manager.upload_file(tmp_path, remote_path)
|
||||
|
||||
Reference in New Issue
Block a user