feat(nodes): streamline urlservice

This commit is contained in:
psychedelicious 2023-05-21 22:44:16 +10:00 committed by Kent Keirsey
parent adde8450bc
commit cc1160a43a
2 changed files with 28 additions and 24 deletions

View File

@ -59,13 +59,8 @@ class ImageServiceABC(ABC):
pass
@abstractmethod
def get_image_url(self, image_type: ImageType, image_name: str) -> str:
"""Gets an image's URL"""
pass
@abstractmethod
def get_thumbnail_url(self, image_type: ImageType, image_name: str) -> str:
"""Gets an image's URL"""
def get_url(self, image_type: ImageType, image_name: str, thumbnail: bool = False) -> str:
"""Gets an image's or thumbnail's URL"""
pass
@abstractmethod
@ -197,8 +192,8 @@ class ImageService(ImageServiceABC):
)
image_url = self._services.urls.get_image_url(image_type, image_name)
thumbnail_url = self._services.urls.get_thumbnail_url(
image_type, image_name
thumbnail_url = self._services.urls.get_image_url(
image_type, image_name, True
)
return ImageDTO(
@ -251,6 +246,15 @@ class ImageService(ImageServiceABC):
self._services.logger.error("Problem getting image path")
raise e
def get_url(
self, image_type: ImageType, image_name: str, thumbnail: bool = False
) -> str:
try:
return self._services.urls.get_image_url(image_type, image_name, thumbnail)
except Exception as e:
self._services.logger.error("Problem getting image path")
raise e
def get_dto(self, image_type: ImageType, image_name: str) -> ImageDTO:
try:
image_record = self._services.records.get(image_type, image_name)
@ -258,7 +262,7 @@ class ImageService(ImageServiceABC):
image_dto = image_record_to_dto(
image_record,
self._services.urls.get_image_url(image_type, image_name),
self._services.urls.get_thumbnail_url(image_type, image_name),
self._services.urls.get_image_url(image_type, image_name, True),
)
return image_dto
@ -289,7 +293,9 @@ class ImageService(ImageServiceABC):
lambda r: image_record_to_dto(
r,
self._services.urls.get_image_url(image_type, r.image_name),
self._services.urls.get_thumbnail_url(image_type, r.image_name),
self._services.urls.get_image_url(
image_type, r.image_name, True
),
),
results.items,
)

View File

@ -6,16 +6,13 @@ from invokeai.app.util.thumbnails import get_thumbnail_name
class UrlServiceBase(ABC):
"""Responsible for building URLs for resources (eg images or tensors)"""
"""Responsible for building URLs for resources."""
@abstractmethod
def get_image_url(self, image_type: ImageType, image_name: str) -> str:
"""Gets the URL for an image"""
pass
@abstractmethod
def get_thumbnail_url(self, image_type: ImageType, image_name: str) -> str:
"""Gets the URL for an image's thumbnail"""
def get_image_url(
self, image_type: ImageType, image_name: str, thumbnail: bool = False
) -> str:
"""Gets the URL for an image or thumbnail."""
pass
@ -23,10 +20,11 @@ class LocalUrlService(UrlServiceBase):
def __init__(self, base_url: str = "api/v1"):
self._base_url = base_url
def get_image_url(self, image_type: ImageType, image_name: str) -> str:
def get_image_url(
self, image_type: ImageType, image_name: str, thumbnail: bool = False
) -> str:
image_basename = os.path.basename(image_name)
return f"{self._base_url}/files/images/{image_type.value}/{image_basename}"
if thumbnail:
return f"{self._base_url}/files/images/{image_type.value}/{image_basename}/thumbnail"
def get_thumbnail_url(self, image_type: ImageType, image_name: str) -> str:
image_basename = os.path.basename(image_name)
return f"{self._base_url}/files/images/{image_type.value}/{image_basename}/thumbnail"
return f"{self._base_url}/files/images/{image_type.value}/{image_basename}"