41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
from pathlib import Path
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from fastapi.responses import FileResponse
|
|
from models.user import User
|
|
from services.project_service import project_service
|
|
|
|
from routers.auth import get_current_user
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/images/{project_id}/{filename}")
|
|
async def get_image(
|
|
project_id: str, filename: str, current_user: User = Depends(get_current_user)
|
|
):
|
|
"""Отдаёт изображение из папки проекта"""
|
|
try:
|
|
project_dir = project_service.get_project_dir(project_id, current_user.id)
|
|
image_path = project_dir / "images" / filename
|
|
|
|
if not image_path.exists():
|
|
raise HTTPException(status_code=404, detail="Image not found")
|
|
|
|
# Определяем MIME тип по расширению
|
|
suffix = Path(filename).suffix.lower()
|
|
mime_types = {
|
|
".png": "image/png",
|
|
".jpg": "image/jpeg",
|
|
".jpeg": "image/jpeg",
|
|
".webp": "image/webp",
|
|
".gif": "image/gif",
|
|
}
|
|
|
|
return FileResponse(
|
|
path=image_path,
|
|
media_type=mime_types.get(suffix, "application/octet-stream"),
|
|
)
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=404, detail=str(e))
|