Full source for the-third-rev: Discord bot (discord.py), FastAPI web UI (React/TS/Vite/Tailwind), ComfyUI integration, generation history DB, preset manager, workflow inspector, and all supporting modules. Excluded from tracking: .env, invite_tokens.json, *.db (SQLite), current-workflow-changes.json, user_settings/, presets/, logs/, web-static/ (build output), frontend/node_modules/. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
86 lines
2.7 KiB
Python
86 lines
2.7 KiB
Python
"""GET /api/share/{token}; GET /api/share/{token}/file/{filename}"""
|
|
from __future__ import annotations
|
|
|
|
import base64
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Request
|
|
from fastapi.responses import Response
|
|
|
|
from web.auth import require_auth
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/{token}")
|
|
async def get_share(token: str, _: dict = Depends(require_auth)):
|
|
"""Fetch share metadata and images. Any authenticated user may view a valid share link."""
|
|
from generation_db import get_share_by_token, get_files
|
|
gen = get_share_by_token(token)
|
|
if gen is None:
|
|
raise HTTPException(404, "Share not found or revoked")
|
|
files = get_files(gen["prompt_id"])
|
|
return {
|
|
"prompt_id": gen["prompt_id"],
|
|
"created_at": gen["created_at"],
|
|
"overrides": gen["overrides"],
|
|
"seed": gen["seed"],
|
|
"images": [
|
|
{
|
|
"filename": f["filename"],
|
|
"data": base64.b64encode(f["data"]).decode() if not f["mime_type"].startswith("video/") else None,
|
|
"mime_type": f["mime_type"],
|
|
}
|
|
for f in files
|
|
],
|
|
}
|
|
|
|
|
|
@router.get("/{token}/file/{filename}")
|
|
async def get_share_file(
|
|
token: str,
|
|
filename: str,
|
|
request: Request,
|
|
_: dict = Depends(require_auth),
|
|
):
|
|
"""Stream a single output file via share token, with HTTP range support for video seeking."""
|
|
from generation_db import get_share_by_token, get_files
|
|
gen = get_share_by_token(token)
|
|
if gen is None:
|
|
raise HTTPException(404, "Share not found or revoked")
|
|
files = get_files(gen["prompt_id"])
|
|
matched = next((f for f in files if f["filename"] == filename), None)
|
|
if matched is None:
|
|
raise HTTPException(404, f"File {filename!r} not found")
|
|
|
|
data: bytes = matched["data"]
|
|
mime: str = matched["mime_type"]
|
|
total = len(data)
|
|
|
|
range_header = request.headers.get("range")
|
|
if range_header:
|
|
range_val = range_header.replace("bytes=", "")
|
|
start_str, _, end_str = range_val.partition("-")
|
|
start = int(start_str) if start_str else 0
|
|
end = int(end_str) if end_str else total - 1
|
|
end = min(end, total - 1)
|
|
chunk = data[start : end + 1]
|
|
return Response(
|
|
content=chunk,
|
|
status_code=206,
|
|
media_type=mime,
|
|
headers={
|
|
"Content-Range": f"bytes {start}-{end}/{total}",
|
|
"Accept-Ranges": "bytes",
|
|
"Content-Length": str(len(chunk)),
|
|
},
|
|
)
|
|
|
|
return Response(
|
|
content=data,
|
|
media_type=mime,
|
|
headers={
|
|
"Accept-Ranges": "bytes",
|
|
"Content-Length": str(total),
|
|
},
|
|
)
|