Fix bug where image click only goes to the next image
This commit is contained in:
parent
7b96ca27d8
commit
ef0c0549b5
@ -34,7 +34,7 @@
|
||||
</head>
|
||||
<body>
|
||||
<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">‹</a>
|
||||
<a href="$next_url" class="chevron right" id="next-btn">›</a>
|
||||
</div>
|
||||
|
||||
18
main.py
18
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"""
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user