mirror of
https://github.com/Xtao-Labs/misskey2telegram_web.git
synced 2024-11-30 09:32:46 +00:00
feat: avatar jpg
This commit is contained in:
parent
a999f3e864
commit
340f25a471
BIN
default.jpg
Normal file
BIN
default.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 9.9 KiB |
32
main.py
32
main.py
@ -1,10 +1,16 @@
|
|||||||
|
import io
|
||||||
|
import traceback
|
||||||
import uuid
|
import uuid
|
||||||
|
import PIL.Image
|
||||||
|
from httpx import AsyncClient
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from starlette.responses import RedirectResponse
|
from starlette.responses import RedirectResponse, StreamingResponse
|
||||||
|
|
||||||
from utils import gen_url, get_auth
|
from utils import gen_url, get_auth
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
with open("default.jpg", "rb") as f:
|
||||||
|
default_jpg = io.BytesIO(f.read())
|
||||||
|
|
||||||
|
|
||||||
@app.get('/gen')
|
@app.get('/gen')
|
||||||
@ -18,6 +24,30 @@ async def gen(
|
|||||||
return RedirectResponse(gen_url(username, host, back_host, session))
|
return RedirectResponse(gen_url(username, host, back_host, session))
|
||||||
|
|
||||||
|
|
||||||
|
@app.get('/1.jpg')
|
||||||
|
async def jpg(
|
||||||
|
*,
|
||||||
|
url: str,
|
||||||
|
) -> StreamingResponse:
|
||||||
|
if "/proxy/avatar" not in url:
|
||||||
|
return StreamingResponse(default_jpg, media_type="image/jpg")
|
||||||
|
url = url.replace("/proxy/avatar.webp", "/proxy/avatar.png")
|
||||||
|
# jpg png webp gif to jpg
|
||||||
|
try:
|
||||||
|
async with AsyncClient() as client:
|
||||||
|
print(f"get jpg {url}")
|
||||||
|
r = await client.get(url)
|
||||||
|
remote = PIL.Image.open(io.BytesIO(r.content))
|
||||||
|
remote = remote.convert("RGB")
|
||||||
|
file_like = io.BytesIO()
|
||||||
|
remote.save(file_like, "jpeg")
|
||||||
|
file_like.seek(0)
|
||||||
|
return StreamingResponse(file_like, media_type="image/jpg")
|
||||||
|
except Exception:
|
||||||
|
traceback.print_exc()
|
||||||
|
return StreamingResponse(default_jpg, media_type="image/jpg")
|
||||||
|
|
||||||
|
|
||||||
@app.get("/{username}/{host}")
|
@app.get("/{username}/{host}")
|
||||||
async def back_to_telegram(
|
async def back_to_telegram(
|
||||||
*,
|
*,
|
||||||
|
@ -2,3 +2,4 @@ httpx==0.23.3
|
|||||||
fastapi~=0.85.2
|
fastapi~=0.85.2
|
||||||
starlette~=0.20.4
|
starlette~=0.20.4
|
||||||
uvicorn~=0.18.3
|
uvicorn~=0.18.3
|
||||||
|
pillow
|
||||||
|
Loading…
Reference in New Issue
Block a user