27 lines
1 KiB
Python
27 lines
1 KiB
Python
from fastapi import APIRouter, UploadFile, HTTPException
|
|
import httpx
|
|
|
|
from app.config import settings
|
|
|
|
router = APIRouter(
|
|
prefix="/images",
|
|
tags=["Загрузка картинок"]
|
|
)
|
|
|
|
|
|
@router.post('')
|
|
async def upload_image(file: UploadFile):
|
|
remote_server_url = settings.IMAGE_UPLOAD_SERVER + "images"
|
|
async with httpx.AsyncClient() as client:
|
|
try:
|
|
file_content = await file.read()
|
|
response = await client.post(remote_server_url, files={"file": file_content})
|
|
if response.status_code != 200:
|
|
raise HTTPException(status_code=response.status_code,
|
|
detail="Ошибка при загрузке файла на удаленный сервер")
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
image_name = response.json()['image_url']
|
|
image_url = '/'.join(image_name.split('/')[1:])
|
|
image_url = settings.IMAGE_UPLOAD_SERVER + image_url
|
|
return image_url
|