Cleanup: odstrani duplikat upload-nextcloud endpoint
Endpoint /api/jobs/{id}/upload-nextcloud je že obstajal (commit dbb8ab3)
in deluje. Moja nova varianta je bila duplikat — odstranjena.
Aktivni endpoint: line 1593 (upload_nextcloud), uporablja
_nextcloud_upload() in _nextcloud_configured() helper-je.
Zdaj imamo:
- _safe_filename_for_nextcloud() helper (ostane, lahko pride prav)
- Frontend gumb '☁ Nextcloud' z 4 stanji (default/uploading/uploaded/failed)
- 14 reelov že uspešno uploadanih v /folxspeed/REELS/FOLX SLOVENIJA/
This commit is contained in:
parent
d03beddd0d
commit
8284181fb3
69
app/main.py
69
app/main.py
@ -1716,72 +1716,3 @@ def _safe_filename_for_nextcloud(name: str) -> str:
|
|||||||
# Strip control chars
|
# Strip control chars
|
||||||
name = ''.join(c for c in name if c.isprintable())
|
name = ''.join(c for c in name if c.isprintable())
|
||||||
return name.strip()[:200] or "reel.mp4"
|
return name.strip()[:200] or "reel.mp4"
|
||||||
|
|
||||||
|
|
||||||
def upload_to_nextcloud(local_path: Path, remote_filename: str) -> tuple[bool, str]:
|
|
||||||
"""Upload datoteko v Nextcloud preko WebDAV.
|
|
||||||
|
|
||||||
Vrne (success, message).
|
|
||||||
"""
|
|
||||||
if not NEXTCLOUD_PASS:
|
|
||||||
return False, "NEXTCLOUD_PASS env var ni nastavljen"
|
|
||||||
if not local_path.exists():
|
|
||||||
return False, f"Lokalna datoteka ne obstaja: {local_path}"
|
|
||||||
|
|
||||||
safe_name = _safe_filename_for_nextcloud(remote_filename)
|
|
||||||
|
|
||||||
# URL-encode posamezne segmente poti (presledki v "FOLX SLOVENIJA" → %20)
|
|
||||||
from urllib.parse import quote
|
|
||||||
folder_encoded = "/".join(quote(seg) for seg in NEXTCLOUD_FOLDER.split("/"))
|
|
||||||
name_encoded = quote(safe_name)
|
|
||||||
|
|
||||||
url = f"{NEXTCLOUD_URL.rstrip('/')}/remote.php/dav/files/{NEXTCLOUD_USER}/{folder_encoded}/{name_encoded}"
|
|
||||||
|
|
||||||
try:
|
|
||||||
import requests
|
|
||||||
with open(local_path, "rb") as f:
|
|
||||||
r = requests.put(
|
|
||||||
url,
|
|
||||||
data=f,
|
|
||||||
auth=(NEXTCLOUD_USER, NEXTCLOUD_PASS),
|
|
||||||
timeout=300, # 5 min za velike fajle
|
|
||||||
)
|
|
||||||
if r.status_code in (200, 201, 204):
|
|
||||||
return True, f"✅ Uploaded as {safe_name}"
|
|
||||||
return False, f"HTTP {r.status_code}: {r.text[:200]}"
|
|
||||||
except Exception as e:
|
|
||||||
return False, f"Upload error: {e}"
|
|
||||||
|
|
||||||
|
|
||||||
@app.post("/api/jobs/{job_id}/upload-nextcloud")
|
|
||||||
async def upload_job_to_nextcloud(job_id: str, user: str = Depends(check_auth)):
|
|
||||||
"""Naloži output reel v Nextcloud /folxspeed/REELS/."""
|
|
||||||
job = load_job(job_id)
|
|
||||||
if not job:
|
|
||||||
raise HTTPException(404, "Ne obstaja")
|
|
||||||
|
|
||||||
if job.get("status") != "done":
|
|
||||||
raise HTTPException(400, "Reel še ni gotov (status != done)")
|
|
||||||
|
|
||||||
output_path = OUTPUT_DIR / f"{job_id}.mp4"
|
|
||||||
if not output_path.exists():
|
|
||||||
raise HTTPException(404, "Output mp4 ne obstaja")
|
|
||||||
|
|
||||||
# Naredi smiseln filename
|
|
||||||
download_name = build_download_filename(job) # npr. "FEHTARJI - GORENJSKA LJUBLJENA.mp4"
|
|
||||||
|
|
||||||
# Async background upload (ne čakaj v requestu — file je lahko 30 MB)
|
|
||||||
update_job(job_id, nextcloud_status="uploading")
|
|
||||||
|
|
||||||
def _do_upload():
|
|
||||||
success, msg = upload_to_nextcloud(output_path, download_name)
|
|
||||||
if success:
|
|
||||||
update_job(job_id, nextcloud_status="uploaded", nextcloud_filename=_safe_filename_for_nextcloud(download_name))
|
|
||||||
print(f"☁ Nextcloud upload OK: {job_id} → {download_name}", flush=True)
|
|
||||||
else:
|
|
||||||
update_job(job_id, nextcloud_status="failed", nextcloud_error=msg)
|
|
||||||
print(f"❌ Nextcloud upload failed: {job_id} → {msg}", flush=True)
|
|
||||||
|
|
||||||
threading.Thread(target=_do_upload, daemon=True).start()
|
|
||||||
|
|
||||||
return {"status": "uploading", "filename": _safe_filename_for_nextcloud(download_name)}
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user