feat(nodes): add get_uri method to image storage

- gets the external URI of an image
This commit is contained in:
psychedelicious 2023-04-25 00:24:03 +10:00
parent 74013b6611
commit 711d09a107
2 changed files with 35 additions and 11 deletions

View File

@ -104,17 +104,19 @@ async def upload_image(
invokeai_metadata = ApiDependencies.invoker.services.metadata.get_metadata(img) invokeai_metadata = ApiDependencies.invoker.services.metadata.get_metadata(img)
image_url = ApiDependencies.invoker.services.images.get_uri(
ImageType.UPLOAD, saved_image.image_name
)
thumbnail_url = ApiDependencies.invoker.services.images.get_uri(
ImageType.UPLOAD, saved_image.image_name, True
)
res = ImageResponse( res = ImageResponse(
image_type=ImageType.UPLOAD, image_type=ImageType.UPLOAD,
image_name=saved_image.image_name, image_name=saved_image.image_name,
image_url=request.url_for( image_url=image_url,
"get_image", image_type=ImageType.UPLOAD.value, image_name=saved_image.image_name thumbnail_url=thumbnail_url,
),
thumbnail_url=request.url_for(
"get_thumbnail",
thumbnail_type=ImageType.UPLOAD.value,
thumbnail_name=saved_image.thumbnail_name,
),
metadata=ImageResponseMetadata( metadata=ImageResponseMetadata(
created=saved_image.created, created=saved_image.created,
width=img.width, width=img.width,

View File

@ -45,7 +45,15 @@ class ImageStorageBase(ABC):
def get_path( def get_path(
self, image_type: ImageType, image_name: str, is_thumbnail: bool = False self, image_type: ImageType, image_name: str, is_thumbnail: bool = False
) -> str: ) -> str:
"""Gets the path to an image or its thumbnail.""" """Gets the internal path to an image or its thumbnail."""
pass
# TODO: make this a bit more flexible for e.g. cloud storage
@abstractmethod
def get_uri(
self, image_type: ImageType, image_name: str, is_thumbnail: bool = False
) -> str:
"""Gets the external URI to an image or its thumbnail."""
pass pass
# TODO: make this a bit more flexible for e.g. cloud storage # TODO: make this a bit more flexible for e.g. cloud storage
@ -130,8 +138,8 @@ class DiskImageStorage(ImageStorageBase):
image_type=image_type.value, image_type=image_type.value,
image_name=filename, image_name=filename,
# TODO: DiskImageStorage should not be building URLs...? # TODO: DiskImageStorage should not be building URLs...?
image_url=f"api/v1/images/{image_type.value}/{filename}", image_url=self.get_uri(image_type, filename),
thumbnail_url=f"api/v1/images/{image_type.value}/thumbnails/{os.path.splitext(filename)[0]}.webp", thumbnail_url=self.get_uri(image_type, filename, True),
# TODO: Creation of this object should happen elsewhere (?), just making it fit here so it works # TODO: Creation of this object should happen elsewhere (?), just making it fit here so it works
metadata=ImageResponseMetadata( metadata=ImageResponseMetadata(
created=int(os.path.getctime(path)), created=int(os.path.getctime(path)),
@ -180,6 +188,20 @@ class DiskImageStorage(ImageStorageBase):
return path return path
def get_uri(
self, image_type: ImageType, image_name: str, is_thumbnail: bool = False
) -> str:
# strip out any relative path shenanigans
basename = os.path.basename(image_name)
if is_thumbnail:
thumbnail_basename = get_thumbnail_name(basename)
uri = f"api/v1/images/{image_type.value}/thumbnails/{thumbnail_basename}"
else:
uri = f"api/v1/images/{image_type.value}/{basename}"
return uri
def validate_path(self, path: str) -> bool: def validate_path(self, path: str) -> bool:
try: try:
os.stat(path) os.stat(path)