Add refresh and sequence

This commit is contained in:
Timothy Farrell 2026-03-12 08:38:27 +00:00
parent ef0c0549b5
commit 362b6fa165
2 changed files with 61 additions and 18 deletions

View File

@ -4,6 +4,7 @@
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>Image Server</title> <title>Image Server</title>
$extra_meta
<style> <style>
* { margin: 0; padding: 0; box-sizing: border-box; } * { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; overflow: hidden; background: #1a1a1a; } html, body { height: 100%; overflow: hidden; background: #1a1a1a; }

76
main.py
View File

@ -129,34 +129,76 @@ async def root():
return RedirectResponse(url="/{hash}".format(hash=random_hash)) return RedirectResponse(url="/{hash}".format(hash=random_hash))
def _get_navigation_data(file_hash: str):
"""Get navigation data for a file hash"""
keys = list(file_mapping.keys())
idx = keys.index(file_hash)
next_hash = keys[(idx + 1) % len(keys)]
prev_hash = keys[idx - 1] if idx > 0 else keys[-1]
next_random_hash = _get_random_hash()
indexer = _find_indexer_for_hash(file_hash)
filename = indexer.get_filename_by_hash(file_hash) if indexer else ""
return {
"file_hash": file_hash,
"next_hash": next_hash,
"prev_hash": prev_hash,
"next_random_hash": next_random_hash,
"filename": filename,
}
def _render_page(navigation_data: dict, extra_meta: str = "") -> HTMLResponse:
"""Render the frontend page with navigation data"""
with open("frontend.html", "r") as f:
content = f.read()
template = string.Template(content)
content = template.substitute(
img_url="/api/{file_hash}/data".format(file_hash=navigation_data["file_hash"]),
next_random_url="/{next_random_hash}".format(
next_random_hash=navigation_data["next_random_hash"]
),
next_url="/{next_hash}".format(next_hash=navigation_data["next_hash"]),
prev_url="/{prev_hash}".format(prev_hash=navigation_data["prev_hash"]),
filename=navigation_data["filename"],
extra_meta=extra_meta,
)
return HTMLResponse(content=content)
@app.get("/{file_hash}") @app.get("/{file_hash}")
async def hash_page(file_hash: str): async def hash_page(file_hash: str):
"""Serve a page for a specific file hash with navigation""" """Serve a page for a specific file hash with navigation"""
if file_hash not in file_mapping: if file_hash not in file_mapping:
raise HTTPException(status_code=404, detail="File not found") raise HTTPException(status_code=404, detail="File not found")
keys = list(file_mapping.keys()) navigation_data = _get_navigation_data(file_hash)
idx = keys.index(file_hash) return _render_page(navigation_data)
next_hash = keys[(idx + 1) % len(keys)]
prev_hash = keys[idx - 1] if idx > 0 else keys[-1]
next_random_hash=_get_random_hash()
indexer = _find_indexer_for_hash(file_hash)
filename = indexer.get_filename_by_hash(file_hash) if indexer else ""
with open("frontend.html", "r") as f: @app.get("/{order}/{delay}/{file_hash}")
content = f.read() async def hash_page_with_refresh(order: str, delay: int, file_hash: str):
"""Serve a page for a specific file hash with auto-refresh navigation"""
if file_hash not in file_mapping:
raise HTTPException(status_code=404, detail="File not found")
template = string.Template(content) if order not in ("next", "random"):
content = template.substitute( raise HTTPException(status_code=400, detail="Invalid order. Must be 'next' or 'random'")
img_url="/api/{file_hash}/data".format(file_hash=file_hash),
next_random_url="/{next_random_hash}".format(next_random_hash=next_random_hash), navigation_data = _get_navigation_data(file_hash)
next_url="/{next_hash}".format(next_hash=next_hash),
prev_url="/{prev_hash}".format(prev_hash=prev_hash), if order == "next":
filename=filename, refresh_url = "/{next_hash}".format(next_hash=navigation_data["next_hash"])
else:
refresh_url = "/{next_random_hash}".format(
next_random_hash=navigation_data["next_random_hash"]
) )
return HTMLResponse(content=content) refresh_meta = f'<meta http-equiv="refresh" content="{delay};url={refresh_url}">'
return _render_page(navigation_data, refresh_meta)
def _find_indexer_for_hash(file_hash: str): def _find_indexer_for_hash(file_hash: str):