Enable navigation by keyboard
This commit is contained in:
parent
5387ce070c
commit
a3e81068c1
@ -13,30 +13,44 @@
|
|||||||
<body>
|
<body>
|
||||||
<img id="random-img" alt="Random image">
|
<img id="random-img" alt="Random image">
|
||||||
<script>
|
<script>
|
||||||
function loadImage(hash) {
|
let currentData = null;
|
||||||
document.getElementById('random-img').src = '/' + hash;
|
|
||||||
|
function loadImageSrc(hash) {
|
||||||
|
document.getElementById('random-img').src = '/' + hash + '/data';
|
||||||
history.replaceState(null, '', '#' + hash);
|
history.replaceState(null, '', '#' + hash);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function loadNewImage() {
|
async function loadInfo(hash) {
|
||||||
const response = await fetch('/random?' + Date.now());
|
const response = await fetch('/' + hash);
|
||||||
const data = await response.json();
|
currentData = await response.json();
|
||||||
loadImage(data.img);
|
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) {
|
document.addEventListener('keydown', function(e) {
|
||||||
if (e.code === 'Space') {
|
if (e.code === 'Space') {
|
||||||
e.preventDefault();
|
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);
|
const hash = window.location.hash.slice(1);
|
||||||
if (hash) {
|
if (hash) {
|
||||||
loadImage(hash);
|
loadInfo(hash);
|
||||||
} else {
|
} else {
|
||||||
loadNewImage();
|
loadRandom();
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
27
main.py
27
main.py
@ -110,16 +110,20 @@ async def root():
|
|||||||
|
|
||||||
@app.get("/random")
|
@app.get("/random")
|
||||||
async def get_random_file():
|
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:
|
if not indexer.file_mapping:
|
||||||
raise HTTPException(status_code=404, detail="No files indexed")
|
raise HTTPException(status_code=404, detail="No files indexed")
|
||||||
|
|
||||||
random_hash = random.choice(list(indexer.file_mapping.keys()))
|
keys = list(indexer.file_mapping.keys())
|
||||||
return {"img": random_hash}
|
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}")
|
@app.get("/{file_hash}/data")
|
||||||
async def get_file_by_hash(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"""
|
||||||
if file_hash not in indexer.file_mapping:
|
if file_hash not in indexer.file_mapping:
|
||||||
raise HTTPException(status_code=404, detail="File not found")
|
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
|
# Optional: Add a health check endpoint
|
||||||
@app.get("/health")
|
@app.get("/health")
|
||||||
async def health_check():
|
async def health_check():
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user