Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

3.9 KB
Raw Blame
from pathlib import Path

from PIL import Image
from PIL.Image import Image as PILImageType

from invokeai.app.services.invoker import Invoker
from invokeai.app.services.model_images.model_images_base import ModelImageFileStorageBase
from invokeai.app.services.model_images.model_images_common import (
    ModelImageFileDeleteException,
    ModelImageFileNotFoundException,
    ModelImageFileSaveException,
)
from invokeai.app.util.misc import uuid_string
from invokeai.app.util.path_safety import is_contained_filename
from invokeai.app.util.thumbnails import make_thumbnail


class ModelImageFileStorageDisk(ModelImageFileStorageBase):
    """Stores images on disk"""

    def __init__(self, model_images_folder: Path):
        self._model_images_folder = model_images_folder
        self._validate_storage_folders()

    def start(self, invoker: Invoker) -> None:
        self._invoker = invoker

    def get(self, model_key: str) -> PILImageType:
        try:
            path = self.get_path(model_key)

            if not self._validate_path(path):
                raise ModelImageFileNotFoundException

            return Image.open(path)
        except FileNotFoundError as e:
            raise ModelImageFileNotFoundException from e

    def save(self, image: PILImageType, model_key: str) -> None:
        try:
            self._validate_storage_folders()
            image_path = self.get_path(model_key)
            thumbnail = make_thumbnail(image, 256)
            thumbnail.save(image_path, format="webp")

        except Exception as e:
            raise ModelImageFileSaveException from e

    def get_path(self, model_key: str) -> Path:
        self._validate_key(model_key)
        path = self._model_images_folder / (model_key + ".webp")

        return path

    def get_url(self, model_key: str) -> str | None:
        try:
            path = self.get_path(model_key)
        except ModelImageFileNotFoundException:
            # This is called while listing every model, so a bad key must not take the whole list down.
            return None
        try:
            if not self._validate_path(path):
                return
        except OSError:
            # e.g. ENAMETOOLONG for an over-long stored key: as above, one bad key must not fail the whole list.
            return None

        url = self._invoker.services.urls.get_model_image_url(model_key)

        # The image URL never changes, so we must add random query string to it to prevent caching
        url += f"?{uuid_string()}"

        return url

    def delete(self, model_key: str) -> None:
        try:
            path = self.get_path(model_key)

            if not self._validate_path(path):
                raise ModelImageFileNotFoundException

            path.unlink()

        except Exception as e:
            raise ModelImageFileDeleteException from e

    def _validate_path(self, path: Path) -> bool:
        """Validates the path given for an image."""
        return path.exists()

    @staticmethod
    def _validate_key(model_key: str) -> None:
        """Validates that a model key cannot escape the model images folder.

        Keys are server-generated - a uuid, or a slug for external API models - but they arrive from the client as
        a path parameter, so they are untrusted. This checks containment on the running platform rather than the
        portable rules new keys must meet: a key stored by an older version may be a legal posix filename that
        Windows would reject (`legacy:key`), and its existing cover must stay servable and manageable.

        :raises ModelImageFileNotFoundException: if the key would not name a file inside the folder
        """
        if not is_contained_filename(model_key):
            raise ModelImageFileNotFoundException(f"Invalid model key {model_key!r}")

    def _validate_storage_folders(self) -> None:
        """Checks if the required folders exist and create them if they don't"""
        self._model_images_folder.mkdir(parents=True, exist_ok=True)