2023-05-17 09:13:53 +00:00
|
|
|
import os
|
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
|
2023-05-27 11:39:20 +00:00
|
|
|
from invokeai.app.models.image import ResourceOrigin
|
2023-05-17 09:13:53 +00:00
|
|
|
from invokeai.app.util.thumbnails import get_thumbnail_name
|
|
|
|
|
|
|
|
|
|
|
|
class UrlServiceBase(ABC):
|
2023-05-21 12:44:16 +00:00
|
|
|
"""Responsible for building URLs for resources."""
|
2023-05-17 09:13:53 +00:00
|
|
|
|
|
|
|
@abstractmethod
|
2023-05-21 12:44:16 +00:00
|
|
|
def get_image_url(
|
2023-05-27 11:39:20 +00:00
|
|
|
self, image_origin: ResourceOrigin, image_name: str, thumbnail: bool = False
|
2023-05-21 12:44:16 +00:00
|
|
|
) -> str:
|
|
|
|
"""Gets the URL for an image or thumbnail."""
|
2023-05-17 09:13:53 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class LocalUrlService(UrlServiceBase):
|
|
|
|
def __init__(self, base_url: str = "api/v1"):
|
|
|
|
self._base_url = base_url
|
|
|
|
|
2023-05-21 12:44:16 +00:00
|
|
|
def get_image_url(
|
2023-05-27 11:39:20 +00:00
|
|
|
self, image_origin: ResourceOrigin, image_name: str, thumbnail: bool = False
|
2023-05-21 12:44:16 +00:00
|
|
|
) -> str:
|
2023-05-17 09:13:53 +00:00
|
|
|
image_basename = os.path.basename(image_name)
|
2023-05-23 12:57:29 +00:00
|
|
|
|
|
|
|
# These paths are determined by the routes in invokeai/app/api/routers/images.py
|
2023-05-21 12:44:16 +00:00
|
|
|
if thumbnail:
|
2023-05-23 12:57:29 +00:00
|
|
|
return (
|
2023-05-27 11:39:20 +00:00
|
|
|
f"{self._base_url}/images/{image_origin.value}/{image_basename}/thumbnail"
|
2023-05-23 12:57:29 +00:00
|
|
|
)
|
2023-05-17 09:13:53 +00:00
|
|
|
|
2023-05-27 11:39:20 +00:00
|
|
|
return f"{self._base_url}/images/{image_origin.value}/{image_basename}"
|