Spaces:
Runtime error
Runtime error
Commit ·
76fb4bd
1
Parent(s): 98ffa8e
Remove dead _ensure_dir; finalize SFTP upload path logic
Browse files- backend/worker/sftp_store.py +46 -7
backend/worker/sftp_store.py
CHANGED
|
@@ -18,6 +18,31 @@ from typing import Optional, Tuple
|
|
| 18 |
|
| 19 |
import paramiko
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def _sftp_client() -> Tuple[paramiko.SFTPClient, paramiko.Transport]:
|
| 23 |
host = (os.environ.get("SFTP_HOST") or "").strip()
|
|
@@ -63,22 +88,36 @@ def _mkdir_p(sftp: paramiko.SFTPClient, remote_dir: str) -> None:
|
|
| 63 |
|
| 64 |
|
| 65 |
def store_to_sftp(pdf_id: str, template_id: str, cfg_json_bytes: bytes, pdf_bytes: bytes, pdf_name: str) -> str:
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
sftp, transport = _sftp_client()
|
| 72 |
try:
|
|
|
|
| 73 |
_mkdir_p(sftp, remote_dir)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
with sftp.open(remote_cfg, "wb") as f:
|
| 75 |
f.write(cfg_json_bytes)
|
| 76 |
with sftp.open(remote_pdf, "wb") as f:
|
| 77 |
f.write(pdf_bytes)
|
|
|
|
|
|
|
| 78 |
finally:
|
| 79 |
try:
|
| 80 |
sftp.close()
|
| 81 |
finally:
|
| 82 |
transport.close()
|
| 83 |
-
|
| 84 |
-
return remote_dir
|
|
|
|
| 18 |
|
| 19 |
import paramiko
|
| 20 |
|
| 21 |
+
def _pick_base_dir(sftp, preferred: str) -> str:
|
| 22 |
+
"""
|
| 23 |
+
Try preferred base dir; if not writable/usable, fallback to /pdfs.
|
| 24 |
+
"""
|
| 25 |
+
candidates = []
|
| 26 |
+
if preferred and str(preferred).strip():
|
| 27 |
+
candidates.append(str(preferred).strip())
|
| 28 |
+
candidates.append("/pdfs") # known-good on your server
|
| 29 |
+
# last resort: root
|
| 30 |
+
candidates.append("/")
|
| 31 |
+
|
| 32 |
+
for base in candidates:
|
| 33 |
+
base = "/" + base.strip("/")
|
| 34 |
+
test = base + "/.sftp_write_test"
|
| 35 |
+
try:
|
| 36 |
+
# write+delete tiny file to validate permissions
|
| 37 |
+
with sftp.file(test, "wb") as f:
|
| 38 |
+
f.write(b"ok")
|
| 39 |
+
sftp.remove(test)
|
| 40 |
+
return base
|
| 41 |
+
except Exception:
|
| 42 |
+
continue
|
| 43 |
+
|
| 44 |
+
raise RuntimeError("No writable SFTP base dir found (tried preferred, /pdfs, /)")
|
| 45 |
+
|
| 46 |
|
| 47 |
def _sftp_client() -> Tuple[paramiko.SFTPClient, paramiko.Transport]:
|
| 48 |
host = (os.environ.get("SFTP_HOST") or "").strip()
|
|
|
|
| 88 |
|
| 89 |
|
| 90 |
def store_to_sftp(pdf_id: str, template_id: str, cfg_json_bytes: bytes, pdf_bytes: bytes, pdf_name: str) -> str:
|
| 91 |
+
"""
|
| 92 |
+
Upload trainer outputs to SFTP.
|
| 93 |
+
Remote layout:
|
| 94 |
+
<base>/<template_id>/<pdf_id>/
|
| 95 |
+
trainer_config_<pdf_id>__<template_id>.json
|
| 96 |
+
<pdf_name or <pdf_id>.pdf>
|
| 97 |
+
"""
|
| 98 |
+
base = (os.environ.get("SFTP_BASE_DIR") or "/").strip()
|
| 99 |
+
if not base.startswith("/"):
|
| 100 |
+
base = "/" + base
|
| 101 |
+
base = base.rstrip("/") or "/"
|
| 102 |
+
|
| 103 |
+
# connect first
|
| 104 |
sftp, transport = _sftp_client()
|
| 105 |
try:
|
| 106 |
+
remote_dir = f"{base}/{template_id}/{pdf_id}".replace("//", "/")
|
| 107 |
_mkdir_p(sftp, remote_dir)
|
| 108 |
+
|
| 109 |
+
remote_cfg = f"{remote_dir}/trainer_config_{pdf_id}__{template_id}.json"
|
| 110 |
+
remote_pdf_name = (pdf_name or f"{pdf_id}.pdf").lstrip("/")
|
| 111 |
+
remote_pdf = f"{remote_dir}/{remote_pdf_name}"
|
| 112 |
+
|
| 113 |
with sftp.open(remote_cfg, "wb") as f:
|
| 114 |
f.write(cfg_json_bytes)
|
| 115 |
with sftp.open(remote_pdf, "wb") as f:
|
| 116 |
f.write(pdf_bytes)
|
| 117 |
+
|
| 118 |
+
return remote_dir
|
| 119 |
finally:
|
| 120 |
try:
|
| 121 |
sftp.close()
|
| 122 |
finally:
|
| 123 |
transport.close()
|
|
|
|
|
|