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.5 KB
Raw Blame
"""Media references named by saved documents.

Project documents, library workflows, persisted client state (the legacy editor keeps its canvas
and reference images only there) and quarantined project documents are opaque JSON to the server,
but the assets they name are not opaque to cleanup: an intermediate a saved document still points
at (a canvas staging candidate, a cropped reference image) must not be collected as garbage. The
`media_references` table is that index. It is written in the same transaction as the document it describes, so a
save and its references are never observed apart, and the extractor here is the one definition of
"names a media asset" for both writers and the migration that backfills existing documents.

Extraction is deliberately over-inclusive: every string under an image- or video-name key counts,
history included. A false reference retains a file; a missed one deletes something in use.

Applied migrations call the extractors: keep their behaviour frozen, since changing it changes what a fresh
install's migrations do. The migrations' table DDL and writes are frozen in `sqlite_migrator/migrations/
_media_references_v1.py`; the application writes through `database.queries.media_references`.
"""

import json
from dataclasses import dataclass, field
from typing import Literal

MediaReferenceOwnerKind = Literal["project", "project_workspace", "workflow", "client_state", "quarantined_project"]
MediaKind = Literal["image", "video"]

IMAGE_NAME_KEYS = frozenset({"imageName", "image_name"})
VIDEO_NAME_KEYS = frozenset({"videoName", "video_name", "source_video_name"})

# Bounds a pathological document: names are generated UUID filenames, so anything longer is not
# one and is not worth a row.
_MAX_NAME_LENGTH = 255

_NAME_KEY_MARKERS = tuple(f'"{key}"' for key in (*IMAGE_NAME_KEYS, *VIDEO_NAME_KEYS))


@dataclass
class MediaReferences:
    images: set[str] = field(default_factory=set)
    videos: set[str] = field(default_factory=set)

    def is_empty(self) -> bool:
        return not self.images and not self.videos


def extract_media_references(document: object) -> MediaReferences:
    """Collects every image and video name a JSON-like document names, at any depth."""
    references = MediaReferences()
    # Iterative: project documents nest deeply enough that recursion depth is not a bound worth
    # trusting.
    pending: list[object] = [document]
    while pending:
        node = pending.pop()
        if isinstance(node, list):
            pending.extend(node)
            continue
        if not isinstance(node, dict):
            continue
        for key, value in node.items():
            if isinstance(value, str):
                if not value or len(value) > _MAX_NAME_LENGTH:
                    continue
                if key in IMAGE_NAME_KEYS:
                    references.images.add(value)
                elif key in VIDEO_NAME_KEYS:
                    references.videos.add(value)
                continue
            if isinstance(value, (dict, list)):
                pending.append(value)
    return references


def extract_media_references_from_json(document_json: str) -> MediaReferences:
    """Like `extract_media_references` for serialized JSON; malformed JSON names nothing."""
    # Most client-state values name no media; skip parsing them.
    if not any(marker in document_json for marker in _NAME_KEY_MARKERS):
        return MediaReferences()
    try:
        return extract_media_references(json.loads(document_json))
    except ValueError:
        return MediaReferences()