This repository has been archived on 2026-11-11. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files

132 lines
3.8 KiB
Python

from pathlib import Path
from fastapi import FastAPI, Request, Form, HTTPException
from fastapi.responses import HTMLResponse, RedirectResponse, FileResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from .. import db, config
from ..search import engine
from . import routing
app = FastAPI(title="VideoVault")
BASE = Path(__file__).resolve().parent.parent.parent
templates = Jinja2Templates(directory=str(BASE / "app" / "web" / "templates"))
app.mount("/static", StaticFiles(directory=str(BASE / "app" / "web" / "static")), name="static")
@app.get("/health")
def health():
return {"ok": True, "search": engine.available()}
@app.post("/add")
async def add(url: str = Form(...)):
url = url.strip()
if not url:
raise HTTPException(400, "Tom URL")
vid = db.add_job(url)
return RedirectResponse("/", status_code=303)
@app.get("/", response_class=HTMLResponse)
def index(request: Request):
return render_home(request)
@app.get("/video/{vid_id}", response_class=HTMLResponse)
def video(request: Request, vid_id: int):
v = db.get(vid_id)
if not v:
raise HTTPException(404)
return templates.TemplateResponse(request, "video.html", {
"v": v,
"cats": (v["categories"] or "").split(","),
"tags": (v["tags"] or "").split(","),
"stats": _stats(),
"primaries": ["Teen 18+", "Petite", "Skinny", "Bondage"],
"media_url": routing.media_url,
})
@app.get("/media/{vid_id}/{kind}")
def media(vid_id: int, kind: str):
v = db.get(vid_id)
if not v:
raise HTTPException(404)
key = {"video": "optimized_path", "source": "download_path",
"poster": "poster_path", "thumb": "thumb_path",
"preview": "preview_path"}.get(kind)
if not key:
raise HTTPException(400)
path = v.get(key)
if not path or not Path(path).exists():
raise HTTPException(404)
return FileResponse(path)
@app.post("/video/{vid_id}/delete")
def delete(vid_id: int):
files = db.delete_video(vid_id)
if files is None:
raise HTTPException(404)
import os
for f in files:
if f:
try:
os.remove(f)
except OSError:
pass
engine.delete_video(vid_id)
return RedirectResponse("/", status_code=303)
@app.get("/reindex")
def reindex():
from ..pipeline import index_all
index_all()
return RedirectResponse("/", status_code=303)
def _stats():
conn = db.connect()
st = db.stats(conn)
conn.close()
return st
def render_home(request):
params = request.query_params
q = params.get("q", "").strip()
cat = params.get("cat", "").strip()
tag = params.get("tag", "").strip()
order = params.get("order", "added_at DESC")
status = params.get("status", "ready")
if q and engine.available():
ids = engine.search(q)
if ids is None:
ids = None
rows, total = db.list_videos(status=status, category=cat, tag=tag,
q=q if ids is None else None)
hits = set(ids or [])
rows = [r for r in rows if r["id"] in hits] if ids else rows
else:
rows, total = db.list_videos(status=status, category=cat, tag=tag, q=q,
order=order)
conn = db.connect()
cats_list = db.categories(conn)
tags_list = db.tags_for(conn)
st = db.stats(conn)
active = db.queued(limit=12)
conn.close()
return templates.TemplateResponse(request, "index.html", {
"videos": rows, "cats": cats_list, "nisch_tags": tags_list,
"stats": st, "active": active, "q": q, "cat": cat, "tag": tag,
"order": order, "total": total, "search": engine.available(),
"primaries": ["Teen 18+", "Petite", "Skinny", "Bondage"],
"media_url": routing.media_url,
})