diff --git a/frontend.html b/frontend.html index f60fd0c..9e4f968 100644 --- a/frontend.html +++ b/frontend.html @@ -34,7 +34,7 @@
diff --git a/main.py b/main.py index fa2cc84..22c3ecb 100644 --- a/main.py +++ b/main.py @@ -125,12 +125,8 @@ def initialize_server(args: argparse.Namespace): @app.get("/") async def root(): """Redirect to a random file hash""" - if not file_mapping: - raise HTTPException(status_code=404, detail="No files indexed") - - keys = list(file_mapping.keys()) - random_idx = random.randint(0, len(keys) - 1) - return RedirectResponse(url="/{keys}".format(keys=keys[random_idx])) + random_hash = _get_random_hash() + return RedirectResponse(url="/{hash}".format(hash=random_hash)) @app.get("/{file_hash}") @@ -143,6 +139,7 @@ async def hash_page(file_hash: str): 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 "" @@ -153,6 +150,7 @@ async def hash_page(file_hash: str): template = string.Template(content) content = template.substitute( 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), prev_url="/{prev_hash}".format(prev_hash=prev_hash), filename=filename, @@ -169,6 +167,14 @@ def _find_indexer_for_hash(file_hash: str): 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") async def get_file_data(file_hash: str): """Serve a specific file by its hash"""