diff --git a/frontend.html b/frontend.html index a52514d..9bac032 100644 --- a/frontend.html +++ b/frontend.html @@ -11,11 +11,19 @@ - Random image + Random image diff --git a/main.py b/main.py index bfb09ac..a54a241 100644 --- a/main.py +++ b/main.py @@ -110,28 +110,12 @@ async def root(): @app.get("/random") async def get_random_file(): - """Serve a random file from the mapping""" + """Get a random file hash 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())) - - filename = indexer.get_filename_by_hash(random_hash) - content_type, _ = mimetypes.guess_type(filename) - if not content_type: - content_type = "application/octet-stream" - buffer = indexer.get_file_by_hash(random_hash) - response = StreamingResponse( - buffer, - media_type=content_type, - headers={ - "Content-Disposition": f"inline; filename={os.path.basename(filename)}", - "Cache-Control": "no-cache, no-store, must-revalidate", - "Pragma": "no-cache", - "Expires": "0", - }, - ) - return response + return {"img": random_hash} @app.get("/{file_hash}") @@ -140,13 +124,16 @@ async def get_file_by_hash(file_hash: str): if file_hash not in indexer.file_mapping: raise HTTPException(status_code=404, detail="File not found") - file_path, content_type, buffer = indexer.get_file_by_hash(file_hash) + filename = indexer.get_filename_by_hash(file_hash) + content_type, _ = mimetypes.guess_type(filename) + if not content_type: + content_type = "application/octet-stream" return StreamingResponse( - buffer, + indexer.get_file_by_hash(file_hash), media_type=content_type, headers={ - "Content-Disposition": f"inline; filename={os.path.basename(file_path)}", + "Content-Disposition": f"inline; filename={os.path.basename(filename)}", }, )