summaryrefslogtreecommitdiffstats
path: root/packages/meshbay-node/src/meshbay_node/indexer/enrich_photo.py
blob: 5cacef9d59a8a750b6ce466c82b688e6fa641676 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""
Index-time enrichment for the Photos group app: a resized thumbnail and a
minimal, best-effort info set (`taken_at`, `camera`) read from the image's
own EXIF block, for a newly-added image IndexEntry.

Deliberately small — docs/MESHBAY_DESIGN.md §9.9 is explicit that this app does not
build a full EXIF-viewer panel. Two fields only, both best-effort (missing
EXIF is the ordinary case for a screenshot or a re-saved/edited image, not
an error). GPS is never read here, on purpose: it is a location disclosure
the instant it is surfaced to every group member, and nothing in this
module extracts, caches, or hands it to a caller.

Runs through its own small bounded worker pool, separate from the video
(ffmpeg) and audio (mutagen) enrichment pools — mirrors enrich.py exactly,
per docs/MESHBAY_DESIGN.md §6.5's "its own small bounded pool, never the
streaming pool" rule, even though
Pillow's own work is comparatively cheap: a burst of hundreds of newly
shared photos should not peg every CPU core at once.
"""

import asyncio
import datetime
import io
import logging
from collections.abc import Awaitable, Callable
from pathlib import Path

import blake3
from meshbay_common.protocol import IndexEntry
from PIL import ExifTags, Image, ImageOps

from meshbay_node.media_cache import MediaCache

log = logging.getLogger(__name__)

DEFAULT_MAX_CONCURRENT = 2
ENRICH_TIMEOUT_SECS = 20
THUMB_LONG_EDGE = 480
THUMB_JPEG_QUALITY = 85

# Reverse-lookup: EXIF tag id -> name, built once (Image.getexif() returns a
# dict keyed by numeric tag id, not by name).
_EXIF_TAG_NAMES = {v: k for k, v in ExifTags.TAGS.items()}
# Make/Model are 0th-IFD (TIFF) tags, present directly on Image.getexif().
_TAG_MAKE = _EXIF_TAG_NAMES.get("Make")
_TAG_MODEL = _EXIF_TAG_NAMES.get("Model")
# DateTimeOriginal is an Exif-SubIFD tag, not the 0th IFD — a real camera's
# JPEG (verified against piexif-built EXIF, matching what real hardware
# produces) never has it directly on getexif(); it is only reachable via
# getexif().get_ifd(ExifTags.IFD.Exif). Reading it off the plain top-level
# dict, as an earlier version of this module did, silently returned None
# for every real photo while Make/Model kept working — found before this
# ever ran against a real file, by testing with a properly structured EXIF
# block instead of a flat one Pillow itself is lenient enough to round-trip.
_TAG_DATETIME_ORIGINAL = _EXIF_TAG_NAMES.get("DateTimeOriginal")


def _parse_exif_datetime(value: str) -> int | None:
    """EXIF's own format: "YYYY:MM:DD HH:MM:SS", local time, no timezone."""
    try:
        dt = datetime.datetime.strptime(value.strip(), "%Y:%m:%d %H:%M:%S")
        return int(dt.timestamp())
    except (ValueError, TypeError):
        return None


def _read_image(file_path: Path) -> tuple[bytes, int, int, int | None, str | None]:
    """
    Runs in a worker thread (Pillow is synchronous, and decoding a large
    photo is real CPU work — same reason enrich.py's own ancestor/sibling
    scans go through `asyncio.to_thread`).

    Returns (thumbnail_jpeg_bytes, width, height, taken_at, camera) for the
    *original* image's own dimensions — the thumbnail is a separate, resized
    copy, never what width/height describe.
    """
    with Image.open(file_path) as img:
        taken_at = None
        camera = None
        try:
            exif = img.getexif()
            if exif:
                if _TAG_DATETIME_ORIGINAL is not None:
                    sub_ifd = exif.get_ifd(ExifTags.IFD.Exif)
                    raw = sub_ifd.get(_TAG_DATETIME_ORIGINAL)
                    if raw:
                        taken_at = _parse_exif_datetime(str(raw))
                make = exif.get(_TAG_MAKE) if _TAG_MAKE is not None else None
                model = exif.get(_TAG_MODEL) if _TAG_MODEL is not None else None
                make = (make or "").strip() if isinstance(make, str) else None
                model = (model or "").strip() if isinstance(model, str) else None
                if make or model:
                    camera = " ".join(p for p in (make, model) if p)
        except Exception as e:
            # A malformed EXIF block is a real, observed case (a corrupted
            # tag, a non-standard camera) — best-effort, never fatal to the
            # thumbnail itself.
            log.debug("EXIF read failed for %s: %s", file_path, e)

        # Applies (and then clears) the EXIF Orientation tag before reading
        # width/height and resizing — otherwise a phone photo stored
        # "sideways" reports its raw, pre-rotation dimensions (swapped from
        # what it actually displays as) and produces a sideways thumbnail
        # (docs/MESHBAY_DESIGN.md §9.9). Never reads Orientation itself as a
        # client-visible field; this is display correction only, and
        # width/height must describe the *displayed* image, matching what
        # the lightbox and the info panel show.
        oriented = ImageOps.exif_transpose(img)
        width, height = oriented.size
        oriented.thumbnail((THUMB_LONG_EDGE, THUMB_LONG_EDGE), Image.LANCZOS)
        if oriented.mode not in ("RGB", "L"):
            oriented = oriented.convert("RGB")
        buf = io.BytesIO()
        oriented.save(buf, format="JPEG", quality=THUMB_JPEG_QUALITY)
        return buf.getvalue(), width, height, taken_at, camera


class PhotoEnricher:
    """Owns the node's bounded Photos index-time enrichment pool."""

    def __init__(self, media_cache: MediaCache, max_concurrent: int = DEFAULT_MAX_CONCURRENT):
        self._media_cache = media_cache
        self._sem = asyncio.Semaphore(max_concurrent)
        self._tasks: set[asyncio.Task] = set()

    def spawn(
        self, entry: IndexEntry, file_path: Path,
        on_done: Callable[[str, dict], Awaitable[None]],
    ) -> asyncio.Task:
        """Fire-and-forget, same contract as enrich.py's Enricher.spawn."""
        task = asyncio.ensure_future(self._run(entry, file_path, on_done))
        self._tasks.add(task)

        def _cleanup(t: asyncio.Task) -> None:
            self._tasks.discard(t)
            if not t.cancelled() and t.exception():
                log.error("Photo enrichment failed for %s: %s", entry.id[:12], t.exception(),
                          exc_info=t.exception())
        task.add_done_callback(_cleanup)
        return task

    async def _run(
        self, entry: IndexEntry, file_path: Path,
        on_done: Callable[[str, dict], Awaitable[None]],
    ) -> None:
        async with self._sem:
            # entry.id is the file's own content hash — the same bytes
            # produce the same thumbnail, so a hit here means this exact
            # content was already decoded, resized and EXIF-read at some
            # point (this run, an earlier one, even a previous daemon
            # process — media_cache.db is the durable half of this).
            # Without this check, every restart re-ran Pillow over every
            # image in every configured photo_root from scratch — the
            # in-memory GroupIndex enrichment fields don't survive a
            # restart, but this cache does, and nothing was reading it
            # before spawning the expensive work.
            cached_hash = await self._media_cache.get_thumb_hash_by_file_id(entry.id)
            cached_meta = await self._media_cache.get_photo_meta(entry.id) if cached_hash else None
            if cached_hash and cached_meta:
                await on_done(entry.id, {**cached_meta, "thumb_hash": cached_hash})
                return

            fields: dict = {}
            try:
                thumb, width, height, taken_at, camera = await asyncio.wait_for(
                    asyncio.to_thread(_read_image, file_path), timeout=ENRICH_TIMEOUT_SECS)
                fields["width"] = width
                fields["height"] = height
                fields["taken_at"] = taken_at
                fields["camera"] = camera
                thumb_hash = blake3.blake3(thumb).hexdigest()
                await self._media_cache.put_thumb(thumb_hash, entry.id, thumb)
                await self._media_cache.put_photo_meta(entry.id, width, height, taken_at, camera)
                fields["thumb_hash"] = thumb_hash
            except Exception as e:
                log.warning("Photo enrichment failed for %s: %s", file_path, e)

            await on_done(entry.id, fields)