34 lines
1016 B
Python
34 lines
1016 B
Python
"""Shared web UI resources for EPUB search."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
from typing import TYPE_CHECKING
|
|
|
|
from fastapi.templating import Jinja2Templates
|
|
|
|
if TYPE_CHECKING:
|
|
from fastapi import Request
|
|
from fastapi.responses import HTMLResponse
|
|
|
|
PACKAGE_DIR = Path(__file__).resolve().parent
|
|
TEMPLATE_DIR = PACKAGE_DIR / "templates"
|
|
STATIC_DIR = PACKAGE_DIR / "static"
|
|
|
|
|
|
def static_version(filename: str) -> int:
|
|
"""Return a cache-busting token for a static file based on its modification time."""
|
|
try:
|
|
return int((STATIC_DIR / filename).stat().st_mtime)
|
|
except OSError:
|
|
return 0
|
|
|
|
|
|
templates = Jinja2Templates(directory=TEMPLATE_DIR)
|
|
templates.env.globals["static_version"] = static_version
|
|
|
|
|
|
def error_response(request: Request, message: object) -> HTMLResponse:
|
|
"""Render the shared error partial for a failed UI request."""
|
|
return templates.TemplateResponse(request, "partials/error.html", {"message": str(message)}, status_code=500)
|