Fix bug where image click only goes to the next image

This commit is contained in:
Timothy Farrell 2026-03-10 03:01:58 +00:00
parent 7b96ca27d8
commit 5a25b28853
2 changed files with 13 additions and 7 deletions

View File

@ -34,7 +34,7 @@
</head> </head>
<body> <body>
<div id="container"> <div id="container">
<a href="$next_url"><img id="img" src="$img_url" title="$filename"></a> <a href="$next_random_url"><img id="img" src="$img_url" title="$filename"></a>
<a href="$prev_url" class="chevron left" id="prev-btn">&#8249;</a> <a href="$prev_url" class="chevron left" id="prev-btn">&#8249;</a>
<a href="$next_url" class="chevron right" id="next-btn">&#8250;</a> <a href="$next_url" class="chevron right" id="next-btn">&#8250;</a>
</div> </div>

18
main.py
View File

@ -125,12 +125,8 @@ def initialize_server(args: argparse.Namespace):
@app.get("/") @app.get("/")
async def root(): async def root():
"""Redirect to a random file hash""" """Redirect to a random file hash"""
if not file_mapping: random_hash = _get_random_hash()
raise HTTPException(status_code=404, detail="No files indexed") return RedirectResponse(url="/{hash}".format(hash=random_hash))
keys = list(file_mapping.keys())
random_idx = random.randint(0, len(keys) - 1)
return RedirectResponse(url="/{keys}".format(keys=keys[random_idx]))
@app.get("/{file_hash}") @app.get("/{file_hash}")
@ -143,6 +139,7 @@ async def hash_page(file_hash: str):
idx = keys.index(file_hash) idx = keys.index(file_hash)
next_hash = keys[(idx + 1) % len(keys)] next_hash = keys[(idx + 1) % len(keys)]
prev_hash = keys[idx - 1] if idx > 0 else keys[-1] prev_hash = keys[idx - 1] if idx > 0 else keys[-1]
next_random_hash=_get_random_hash()
indexer = _find_indexer_for_hash(file_hash) indexer = _find_indexer_for_hash(file_hash)
filename = indexer.get_filename_by_hash(file_hash) if indexer else "" filename = indexer.get_filename_by_hash(file_hash) if indexer else ""
@ -153,6 +150,7 @@ async def hash_page(file_hash: str):
template = string.Template(content) template = string.Template(content)
content = template.substitute( content = template.substitute(
img_url="/api/{file_hash}/data".format(file_hash=file_hash), img_url="/api/{file_hash}/data".format(file_hash=file_hash),
next_random_url="/{next_random_hash}".format(next_random_hash=next_random_hash)
next_url="/{next_hash}".format(next_hash=next_hash), next_url="/{next_hash}".format(next_hash=next_hash),
prev_url="/{prev_hash}".format(prev_hash=prev_hash), prev_url="/{prev_hash}".format(prev_hash=prev_hash),
filename=filename, filename=filename,
@ -169,6 +167,14 @@ def _find_indexer_for_hash(file_hash: str):
return None return None
def _get_random_hash() -> str:
"""Get a random file hash from the indexed files"""
if not file_mapping:
raise HTTPException(status_code=404, detail="No files indexed")
keys = list(file_mapping.keys())
return random.choice(keys)
@app.get("/api/{file_hash}/data") @app.get("/api/{file_hash}/data")
async def get_file_data(file_hash: str): async def get_file_data(file_hash: str):
"""Serve a specific file by its hash""" """Serve a specific file by its hash"""