Spaces:
Sleeping
Sleeping
Commit ·
5e71b41
1
Parent(s): 47956b3
Send-config: store config+pdf to SFTP
Browse files- backend/api.py +28 -0
backend/api.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
|
|
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
from pathlib import Path
|
|
@@ -101,6 +102,33 @@ async def send_config(request: Request):
|
|
| 101 |
encoding="utf-8",
|
| 102 |
)
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
pdf_path = UPLOADS_DIR / f"{pdf_id}.pdf"
|
| 105 |
return {"ok": True, "stored": str(out_path), "pdf_exists": pdf_path.exists()}
|
| 106 |
|
|
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
+
from backend.sftp_store import store_to_sftp
|
| 4 |
import json
|
| 5 |
import os
|
| 6 |
from pathlib import Path
|
|
|
|
| 102 |
encoding="utf-8",
|
| 103 |
)
|
| 104 |
|
| 105 |
+
|
| 106 |
+
# Store both config JSON + (if exists) PDF to SFTP for future pipelines.
|
| 107 |
+
# SFTP errors must NOT break the API response.
|
| 108 |
+
try:
|
| 109 |
+
cfg_bytes = out_path.read_bytes()
|
| 110 |
+
|
| 111 |
+
pdf_bytes = None
|
| 112 |
+
pdf_name = None
|
| 113 |
+
|
| 114 |
+
# API stores uploaded PDFs at UPLOADS_DIR/{pdf_id}.pdf
|
| 115 |
+
pdf_path = UPLOADS_DIR / f"{pdf_id}.pdf"
|
| 116 |
+
if pdf_path.exists():
|
| 117 |
+
pdf_bytes = pdf_path.read_bytes()
|
| 118 |
+
|
| 119 |
+
# optional friendly name if present
|
| 120 |
+
name_path = UPLOADS_DIR / f"{pdf_id}.name.txt"
|
| 121 |
+
pdf_name = name_path.read_text(encoding="utf-8").strip() if name_path.exists() else f"{pdf_id}.pdf"
|
| 122 |
+
|
| 123 |
+
store_to_sftp(
|
| 124 |
+
pdf_id=pdf_id,
|
| 125 |
+
template_id=template_id,
|
| 126 |
+
cfg_bytes=cfg_bytes,
|
| 127 |
+
pdf_bytes=pdf_bytes,
|
| 128 |
+
pdf_name=pdf_name or f"{pdf_id}.pdf",
|
| 129 |
+
)
|
| 130 |
+
except Exception as e:
|
| 131 |
+
print(f"[SFTP] store failed: {e}")
|
| 132 |
pdf_path = UPLOADS_DIR / f"{pdf_id}.pdf"
|
| 133 |
return {"ok": True, "stored": str(out_path), "pdf_exists": pdf_path.exists()}
|
| 134 |
|