2023-05-21 12:15:44 +00:00
|
|
|
# Copyright (c) 2022 Kyle Schouviller (https://github.com/kyle0654) and the InvokeAI Team
|
|
|
|
from fastapi import HTTPException, Path
|
|
|
|
from fastapi.responses import FileResponse
|
2023-05-21 10:05:33 +00:00
|
|
|
from fastapi.routing import APIRouter
|
|
|
|
from invokeai.app.models.image import ImageType
|
|
|
|
|
|
|
|
from ..dependencies import ApiDependencies
|
|
|
|
|
2023-05-21 12:15:44 +00:00
|
|
|
image_files_router = APIRouter(prefix="/v1/files/images", tags=["images", "files"])
|
2023-05-21 10:05:33 +00:00
|
|
|
|
|
|
|
|
2023-05-21 12:15:44 +00:00
|
|
|
@image_files_router.get("/{image_type}/{image_name}", operation_id="get_image")
|
2023-05-21 10:05:33 +00:00
|
|
|
async def get_image(
|
|
|
|
image_type: ImageType = Path(description="The type of the image to get"),
|
|
|
|
image_name: str = Path(description="The id of the image to get"),
|
|
|
|
) -> FileResponse:
|
|
|
|
"""Gets an image"""
|
|
|
|
|
2023-05-21 12:15:44 +00:00
|
|
|
try:
|
|
|
|
path = ApiDependencies.invoker.services.images_new.get_path(
|
|
|
|
image_type=image_type, image_name=image_name
|
|
|
|
)
|
2023-05-21 10:05:33 +00:00
|
|
|
|
|
|
|
return FileResponse(path)
|
2023-05-21 12:15:44 +00:00
|
|
|
except Exception as e:
|
2023-05-21 10:05:33 +00:00
|
|
|
raise HTTPException(status_code=404)
|
|
|
|
|
|
|
|
|
2023-05-21 12:15:44 +00:00
|
|
|
@image_files_router.get(
|
|
|
|
"/{image_type}/{image_name}/thumbnail", operation_id="get_thumbnail"
|
2023-05-21 10:05:33 +00:00
|
|
|
)
|
|
|
|
async def get_thumbnail(
|
2023-05-21 12:15:44 +00:00
|
|
|
image_type: ImageType = Path(
|
|
|
|
description="The type of the image whose thumbnail to get"
|
|
|
|
),
|
|
|
|
image_name: str = Path(description="The id of the image whose thumbnail to get"),
|
|
|
|
) -> FileResponse:
|
2023-05-21 10:05:33 +00:00
|
|
|
"""Gets a thumbnail"""
|
|
|
|
|
2023-05-21 12:15:44 +00:00
|
|
|
try:
|
|
|
|
path = ApiDependencies.invoker.services.images_new.get_path(
|
|
|
|
image_type=image_type, image_name=image_name, thumbnail=True
|
|
|
|
)
|
2023-05-21 10:05:33 +00:00
|
|
|
|
|
|
|
return FileResponse(path)
|
2023-05-21 12:15:44 +00:00
|
|
|
except Exception as e:
|
2023-05-21 10:05:33 +00:00
|
|
|
raise HTTPException(status_code=404)
|