This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
Telegram/api_avatars.py

28 lines
1.4 KiB
Python
Raw Normal View History

from os import makedirs, path, sep
2022-10-26 16:20:23 +03:00
from fastapi import FastAPI, HTTPException
2022-12-17 23:31:30 +02:00
from fastapi.responses import FileResponse, JSONResponse, Response
2022-10-26 16:20:23 +03:00
from starlette.status import HTTP_404_NOT_FOUND
from modules.utils import configGet
makedirs(f'{configGet("cache", "locations")}{sep}avatars', exist_ok=True)
2022-10-26 16:20:23 +03:00
app = FastAPI(title="HoloUA Avatars API", docs_url=None, redoc_url=None, version="1.0")
2022-11-04 14:05:14 +02:00
@app.get("/check", response_class=JSONResponse, include_in_schema=False)
2022-12-17 23:31:30 +02:00
@app.head("/check", response_class=JSONResponse, include_in_schema=False)
2022-11-04 14:05:14 +02:00
async def check():
return JSONResponse({"detail": "I'm alright, thank you"})
2022-10-26 16:20:23 +03:00
@app.get("/", response_class=FileResponse, include_in_schema=False)
2022-12-17 23:31:30 +02:00
async def avatar_get(avatar_id: str):
2022-10-26 16:20:23 +03:00
if path.exists(f'{configGet("cache", "locations")}{sep}avatars{sep}{avatar_id}'):
2022-10-27 12:40:15 +03:00
return FileResponse(f'{configGet("cache", "locations")}{sep}avatars{sep}{avatar_id}', media_type="image/jpg")
2022-12-17 23:31:30 +02:00
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}')})
2022-10-26 16:20:23 +03:00
else:
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="File not found")