From 933592a2e2f8e9111fe962ca37625f686d0ce240 Mon Sep 17 00:00:00 2001 From: Timothy Farrell Date: Thu, 12 Mar 2026 08:51:53 +0000 Subject: [PATCH] Fix routing order issue. --- main.py | 59 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/main.py b/main.py index 90a4831..9a40477 100644 --- a/main.py +++ b/main.py @@ -122,6 +122,35 @@ def initialize_server(args: argparse.Namespace): print(f"Indexed {len(file_mapping)} files from {len(indexers)} source(s)") +@app.get("/api/health") +async def health_check(): + return {"status": "healthy", "file_count": len(file_mapping)} + + +@app.get("/api/{file_hash}/data") +async def get_file_data(file_hash: str): + """Serve a specific file by its hash""" + if file_hash not in file_mapping: + raise HTTPException(status_code=404, detail="File not found") + + indexer = _find_indexer_for_hash(file_hash) + if not indexer: + raise HTTPException(status_code=404, detail="File not found") + + filename = indexer.get_filename_by_hash(file_hash) + content_type, _ = mimetypes.guess_type(filename or "") + if not content_type: + content_type = "application/octet-stream" + + return StreamingResponse( + indexer.get_file_by_hash(file_hash), + media_type=content_type, + headers={ + "Content-Disposition": f"inline; filename={os.path.basename(filename or '')}", + }, + ) + + @app.get("/") async def root(): """Redirect to a random file hash""" @@ -217,36 +246,6 @@ def _get_random_hash() -> str: 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""" - if file_hash not in file_mapping: - raise HTTPException(status_code=404, detail="File not found") - - indexer = _find_indexer_for_hash(file_hash) - if not indexer: - raise HTTPException(status_code=404, detail="File not found") - - filename = indexer.get_filename_by_hash(file_hash) - content_type, _ = mimetypes.guess_type(filename or "") - if not content_type: - content_type = "application/octet-stream" - - return StreamingResponse( - indexer.get_file_by_hash(file_hash), - media_type=content_type, - headers={ - "Content-Disposition": f"inline; filename={os.path.basename(filename or '')}", - }, - ) - - -# Optional: Add a health check endpoint -@app.get("/api/health") -async def health_check(): - return {"status": "healthy", "file_count": len(file_mapping)} - - if __name__ == "__main__": parser = argparse.ArgumentParser(description="Run the file server") parser.add_argument(