Enable navigation by keyboard

This commit is contained in:
Timothy Farrell 2026-03-09 09:18:56 +00:00
parent 5387ce070c
commit a3e81068c1
2 changed files with 46 additions and 15 deletions

View File

@ -13,30 +13,44 @@
<body>
<img id="random-img" alt="Random image">
<script>
function loadImage(hash) {
document.getElementById('random-img').src = '/' + hash;
let currentData = null;
function loadImageSrc(hash) {
document.getElementById('random-img').src = '/' + hash + '/data';
history.replaceState(null, '', '#' + hash);
}
async function loadNewImage() {
const response = await fetch('/random?' + Date.now());
const data = await response.json();
loadImage(data.img);
async function loadInfo(hash) {
const response = await fetch('/' + hash);
currentData = await response.json();
loadImageSrc(currentData.img);
}
document.getElementById('random-img').addEventListener('click', loadNewImage);
async function loadRandom() {
const response = await fetch('/random?' + Date.now());
const data = await response.json();
await loadInfo(data.img);
}
document.getElementById('random-img').addEventListener('click', loadRandom);
document.addEventListener('keydown', function(e) {
if (e.code === 'Space') {
e.preventDefault();
loadNewImage();
loadRandom();
} else if (e.code === 'ArrowLeft' && currentData && currentData.previous) {
e.preventDefault();
loadInfo(currentData.previous);
} else if (e.code === 'ArrowRight' && currentData && currentData.next) {
e.preventDefault();
loadInfo(currentData.next);
}
});
const hash = window.location.hash.slice(1);
if (hash) {
loadImage(hash);
loadInfo(hash);
} else {
loadNewImage();
loadRandom();
}
</script>
</body>

27
main.py
View File

@ -110,16 +110,20 @@ async def root():
@app.get("/random")
async def get_random_file():
"""Get a random file hash from the mapping"""
"""Get random file hashes from the mapping"""
if not indexer.file_mapping:
raise HTTPException(status_code=404, detail="No files indexed")
random_hash = random.choice(list(indexer.file_mapping.keys()))
return {"img": random_hash}
keys = list(indexer.file_mapping.keys())
random_idx = random.randint(0, len(keys) - 1)
current = keys[random_idx]
next_hash = keys[(random_idx + 1) % len(keys)]
prev_hash = keys[random_idx - 1] if random_idx > 0 else keys[-1]
return {"img": current, "next": next_hash, "previous": prev_hash}
@app.get("/{file_hash}")
async def get_file_by_hash(file_hash: str):
@app.get("/{file_hash}/data")
async def get_file_data(file_hash: str):
"""Serve a specific file by its hash"""
if file_hash not in indexer.file_mapping:
raise HTTPException(status_code=404, detail="File not found")
@ -138,6 +142,19 @@ async def get_file_by_hash(file_hash: str):
)
@app.get("/{file_hash}")
async def get_file_info(file_hash: str):
"""Get file info by hash"""
if file_hash not in indexer.file_mapping:
raise HTTPException(status_code=404, detail="File not found")
keys = list(indexer.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]
return {"img": file_hash, "next": next_hash, "previous": prev_hash}
# Optional: Add a health check endpoint
@app.get("/health")
async def health_check():