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.

5.3 KB
Raw Blame
from abc import ABC, abstractmethod
from collections.abc import Collection, Iterator, Sequence
from pathlib import Path
from typing import Optional

from PIL.Image import Image as PILImageType


class ImageFileStorageBase(ABC):
    """Low-level service responsible for storing and retrieving image files."""

    @abstractmethod
    def get(self, image_name: str, image_subfolder: str = "") -> PILImageType:
        """Retrieves an image as PIL Image."""
        pass

    @abstractmethod
    def get_path(self, image_name: str, thumbnail: bool = False, image_subfolder: str = "") -> Path:
        """Gets the internal path to an image or thumbnail."""
        pass

    @abstractmethod
    def iter_image_paths(self) -> Iterator[Path]:
        """Yields supported image paths in stable order, excluding reserved trees and symlinks."""
        pass

    @abstractmethod
    def iter_thumbnail_paths(self) -> Iterator[Path]:
        """Yields thumbnail paths in stable order, excluding reserved trees and symlinks."""
        pass

    @property
    @abstractmethod
    def image_root(self) -> Path:
        """Gets the root directory for full-size images."""
        pass

    @property
    @abstractmethod
    def thumbnail_root(self) -> Path:
        """Gets the root directory for thumbnails."""
        pass

    @abstractmethod
    def evict_cache_paths(self, paths: list[Path]) -> None:
        """Evicts any cached image objects for the provided paths."""
        pass

    # TODO: We need to validate paths before starlette makes the FileResponse, else we get a
    # 500 internal server error. I don't like having this method on the service.
    @abstractmethod
    def validate_path(self, path: str) -> bool:
        """Validates the path given for an image or thumbnail."""
        pass

    @abstractmethod
    def save(
        self,
        image: PILImageType,
        image_name: str,
        metadata: Optional[str] = None,
        workflow: Optional[str] = None,
        graph: Optional[str] = None,
        thumbnail_size: int = 256,
        image_subfolder: str = "",
        resolved_inputs: Optional[str] = None,
    ) -> None:
        """Saves an image and a 256x256 WEBP thumbnail. Returns a tuple of the image name, thumbnail name, and created timestamp."""
        pass

    @abstractmethod
    def generate_thumbnail_if_missing(
        self, image_name: str, image_subfolder: str = "", thumbnail_size: int = 256
    ) -> bool:
        """Atomically creates a thumbnail only when absent, without rewriting the source image."""
        pass

    @abstractmethod
    def copy(
        self,
        source_image_name: str,
        image_name: str,
        source_subfolder: str = "",
        image_subfolder: str = "",
        thumbnail_size: int = 256,
    ) -> None:
        """Duplicates an existing image's file and thumbnail under a new name.

        A byte-level copy, not a re-encode: every PNG chunk travels, including ones this
        application does not parse, and no pixels are decoded.
        """
        pass

    @abstractmethod
    def get_file_size_bytes(self, image_name: str, image_subfolder: str = "") -> Optional[int]:
        """Bytes the image and its thumbnail occupy; None when the full-size file is missing."""
        pass

    @abstractmethod
    def delete(self, image_name: str, image_subfolder: str = "") -> None:
        """Deletes an image and its thumbnail (if one exists)."""
        pass

    @abstractmethod
    def stage_delete(self, image_name: str, image_subfolder: str = "") -> object:
        """Moves an image's files out of service and returns a rollback token."""
        pass

    @abstractmethod
    def begin_delete(self, images: Sequence[tuple[str, str]]) -> object:
        """Durably records the intent to purge the (image_name, image_subfolder) pairs' files.

        Call this before deleting the records, then ``commit_delete()`` after. If the process dies
        in between, startup recovery uses the journal to purge the files of every listed image
        whose record is gone, and leaves the files of every image whose record survives.
        """
        pass

    @abstractmethod
    def commit_delete(self, token: object, image_names: Optional[Collection[str]] = None) -> None:
        """Permanently removes the files represented by a delete token.

        ``image_names`` narrows a pending-delete token to the records that were actually deleted;
        it is ignored for a staged-delete token.
        """
        pass

    @abstractmethod
    def abandon_delete(self, token: object) -> None:
        """Drops a pending-delete journal without purging anything, leaving the files in place."""
        pass

    @abstractmethod
    def rollback_delete(self, token: object) -> None:
        """Restores files represented by a staged-delete token."""
        pass

    @abstractmethod
    def get_workflow(self, image_name: str, image_subfolder: str = "") -> Optional[str]:
        """Gets the workflow of an image."""
        pass

    @abstractmethod
    def get_graph(self, image_name: str, image_subfolder: str = "") -> Optional[str]:
        """Gets the graph of an image."""
        pass

    @abstractmethod
    def get_resolved_inputs(self, image_name: str, image_subfolder: str = "") -> Optional[str]:
        """Gets the per-image resolved-input Graph JSON, when present."""
        pass