Format code with black
This commit is contained in:
parent
4cf492c37a
commit
35afd5039b
56
main.py
56
main.py
@ -1,3 +1,4 @@
|
||||
from typing import Annotated
|
||||
import argparse
|
||||
import hashlib
|
||||
import mimetypes
|
||||
@ -13,7 +14,12 @@ from pathlib import Path
|
||||
|
||||
from fastapi import FastAPI, HTTPException, Depends
|
||||
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
||||
from fastapi.responses import FileResponse, HTMLResponse, RedirectResponse, StreamingResponse
|
||||
from fastapi.responses import (
|
||||
FileResponse,
|
||||
HTMLResponse,
|
||||
RedirectResponse,
|
||||
StreamingResponse,
|
||||
)
|
||||
|
||||
app = FastAPI()
|
||||
file_mapping = {}
|
||||
@ -209,32 +215,43 @@ def _get_navigation_data(file_hash: str):
|
||||
|
||||
|
||||
def _render_page(
|
||||
navigation_data: dict, extra_meta: str = "", image_click_url: str = "", play_button: str = "", current_order: str | None = None, current_delay: int | None = None
|
||||
navigation_data: dict,
|
||||
extra_meta: str = "",
|
||||
image_click_url: str = "",
|
||||
play_button: str = "",
|
||||
current_order: str | None = None,
|
||||
current_delay: int | None = None,
|
||||
) -> HTMLResponse:
|
||||
"""Render the frontend page with navigation data"""
|
||||
with open("frontend.html", "r") as f:
|
||||
content = f.read()
|
||||
|
||||
template = string.Template(content)
|
||||
|
||||
|
||||
# Generate navigation URLs based on current mode
|
||||
if current_order is not None:
|
||||
# Timer mode: preserve current order and delay
|
||||
next_url = "/{order}/{delay}/{next_hash}".format(
|
||||
order=current_order, delay=current_delay, next_hash=navigation_data["next_hash"]
|
||||
order=current_order,
|
||||
delay=current_delay,
|
||||
next_hash=navigation_data["next_hash"],
|
||||
)
|
||||
prev_url = "/{order}/{delay}/{prev_hash}".format(
|
||||
order=current_order, delay=current_delay, prev_hash=navigation_data["prev_hash"]
|
||||
order=current_order,
|
||||
delay=current_delay,
|
||||
prev_hash=navigation_data["prev_hash"],
|
||||
)
|
||||
else:
|
||||
# Browse mode: generate browse mode URLs
|
||||
next_url = "/{next_hash}".format(next_hash=navigation_data["next_hash"])
|
||||
prev_url = "/{prev_hash}".format(prev_hash=navigation_data["prev_hash"])
|
||||
|
||||
|
||||
content = template.substitute(
|
||||
img_url="/api/{file_hash}/data".format(file_hash=navigation_data["file_hash"]),
|
||||
image_click_url=image_click_url
|
||||
or "/{next_random_hash}".format(next_random_hash=navigation_data["next_random_hash"]),
|
||||
or "/{next_random_hash}".format(
|
||||
next_random_hash=navigation_data["next_random_hash"]
|
||||
),
|
||||
next_url=next_url,
|
||||
prev_url=prev_url,
|
||||
filename=navigation_data["filename"],
|
||||
@ -255,7 +272,9 @@ async def hash_page(file_hash: str, username: str = Depends(get_current_username
|
||||
play_button = '<a href="/next/5/{file_hash}" class="play-btn" title="Play next 5">⏵</a>'.format(
|
||||
file_hash=file_hash
|
||||
)
|
||||
return _render_page(navigation_data, play_button=play_button, current_order=None, current_delay=None)
|
||||
return _render_page(
|
||||
navigation_data, play_button=play_button, current_order=None, current_delay=None
|
||||
)
|
||||
|
||||
|
||||
@app.get("/{order}/{delay}/{file_hash}")
|
||||
@ -265,7 +284,9 @@ async def hash_page_with_refresh(order: str, delay: int, file_hash: str, usernam
|
||||
raise HTTPException(status_code=404, detail="File not found")
|
||||
|
||||
if order not in ("next", "random"):
|
||||
raise HTTPException(status_code=400, detail="Invalid order. Must be 'next' or 'random'")
|
||||
raise HTTPException(
|
||||
status_code=400, detail="Invalid order. Must be 'next' or 'random'"
|
||||
)
|
||||
|
||||
navigation_data = _get_navigation_data(file_hash)
|
||||
|
||||
@ -275,7 +296,9 @@ async def hash_page_with_refresh(order: str, delay: int, file_hash: str, usernam
|
||||
)
|
||||
else:
|
||||
refresh_url = "/{order}/{delay}/{next_random_hash}".format(
|
||||
order=order, delay=delay, next_random_hash=navigation_data["next_random_hash"]
|
||||
order=order,
|
||||
delay=delay,
|
||||
next_random_hash=navigation_data["next_random_hash"],
|
||||
)
|
||||
|
||||
refresh_meta = f'<meta http-equiv="refresh" content="{delay};url={refresh_url}">'
|
||||
@ -286,7 +309,14 @@ async def hash_page_with_refresh(order: str, delay: int, file_hash: str, usernam
|
||||
file_hash=file_hash
|
||||
)
|
||||
|
||||
return _render_page(navigation_data, refresh_meta, image_click_url, play_button=pause_button, current_order=order, current_delay=delay)
|
||||
return _render_page(
|
||||
navigation_data,
|
||||
refresh_meta,
|
||||
image_click_url,
|
||||
play_button=pause_button,
|
||||
current_order=order,
|
||||
current_delay=delay,
|
||||
)
|
||||
|
||||
|
||||
def _find_indexer_for_hash(file_hash: str):
|
||||
@ -314,7 +344,9 @@ if __name__ == "__main__":
|
||||
)
|
||||
parser.add_argument("--host", type=str, default="0.0.0.0", help="Host to bind to")
|
||||
parser.add_argument("--port", type=int, default=8000, help="Port to bind to")
|
||||
parser.add_argument("--salt", type=str, default=None, help="Salt for hashing file paths")
|
||||
parser.add_argument(
|
||||
"--salt", type=str, default=None, help="Salt for hashing file paths"
|
||||
)
|
||||
parser.add_argument("--password", type=str, default=None, help="Password for Basic Authentication")
|
||||
args = parser.parse_args()
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user