From 797d9de7e40ff77402ef55c5e8db6369afab33a3 Mon Sep 17 00:00:00 2001 From: Profitroll <47523801+profitrollgame@users.noreply.github.com> Date: Sat, 17 Dec 2022 22:31:30 +0100 Subject: [PATCH] Also handling HEAD requests now --- api_avatars.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/api_avatars.py b/api_avatars.py index d162076..0ec1916 100644 --- a/api_avatars.py +++ b/api_avatars.py @@ -1,6 +1,6 @@ from os import makedirs, path, sep from fastapi import FastAPI, HTTPException -from fastapi.responses import FileResponse, JSONResponse +from fastapi.responses import FileResponse, JSONResponse, Response from starlette.status import HTTP_404_NOT_FOUND from modules.utils import configGet @@ -9,12 +9,20 @@ makedirs(f'{configGet("cache", "locations")}{sep}avatars', exist_ok=True) app = FastAPI(title="HoloUA Avatars API", docs_url=None, redoc_url=None, version="1.0") @app.get("/check", response_class=JSONResponse, include_in_schema=False) +@app.head("/check", response_class=JSONResponse, include_in_schema=False) async def check(): return JSONResponse({"detail": "I'm alright, thank you"}) @app.get("/", response_class=FileResponse, include_in_schema=False) -async def favicon(avatar_id: str): +async def avatar_get(avatar_id: str): if path.exists(f'{configGet("cache", "locations")}{sep}avatars{sep}{avatar_id}'): return FileResponse(f'{configGet("cache", "locations")}{sep}avatars{sep}{avatar_id}', media_type="image/jpg") + else: + raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="File not found") + +@app.head("/", response_class=Response, include_in_schema=False) +async def avatar_head(avatar_id: str): + if path.exists(f'{configGet("cache", "locations")}{sep}avatars{sep}{avatar_id}'): + return Response(headers={"Content-Length": path.getsize(f'{configGet("cache", "locations")}{sep}avatars{sep}{avatar_id}')}) else: raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="File not found") \ No newline at end of file