File size: 13,706 Bytes
2274b3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
"""Explicit-consent public snapshots. Never expose private inspection identifiers."""

import base64
import hmac
from io import BytesIO
import json
import math
from pathlib import Path
import re
import sqlite3
import threading
import uuid
from urllib.parse import urlparse

import httpx
from fastapi import HTTPException, Query
from PIL import Image
from pydantic import BaseModel, ConfigDict, StrictBool

from .config import CATEGORIES
from .storage import now

FIELDS = ("category", "verdict", "score", "threshold", "uncertainty_margin", "model_id", "localization", "scope")


def public_measurement(record):
    result = {key: record[key] for key in FIELDS if key in record}
    if result.get("category") not in CATEGORIES or result.get("verdict") not in ("Good", "Anomalous", "Uncertain"):
        raise HTTPException(422, "A completed supported inspection is required.")
    for key in ("score", "threshold", "uncertainty_margin"):
        value = result.get(key)
        if key == "uncertainty_margin" and value is None:
            continue
        if isinstance(value, bool) or not isinstance(value, (float, int)) or not math.isfinite(value):
            raise HTTPException(422, "Inspection measurements are invalid.")
    return result


def thumbnail(content):
    with Image.open(BytesIO(content)) as source:
        image = source.convert("RGB")
        image.thumbnail((512, 512))
        out = BytesIO()
        # A new JPEG drops EXIF, original filename and embedded image metadata.
        image.save(out, format="JPEG", quality=70, optimize=True)
    if len(out.getvalue()) > 120000:
        raise HTTPException(422, "Public preview is too large.")
    return base64.b64encode(out.getvalue()).decode("ascii")


class LocalGallery:
    """Durable on local disk only; deliberately NOT the hosted storage fallback."""

    def __init__(self, path, key):
        self.path = Path(path)
        self.path.parent.mkdir(parents=True, exist_ok=True)
        self.key = key.encode()
        self.lock = threading.RLock()
        with self.connect() as db:
            db.execute(
                "CREATE TABLE IF NOT EXISTS entries(id TEXT PRIMARY KEY,created_at TEXT,owner TEXT,source TEXT,payload TEXT,image TEXT,heatmap TEXT,visible INTEGER,UNIQUE(owner,source))"
            )

    def connect(self):
        db = sqlite3.connect(self.path, timeout=20)
        db.row_factory = sqlite3.Row
        return db

    def identity(self, value):
        return hmac.new(self.key, value.encode(), "sha256").hexdigest()

    def read(self, identifier=None, offset=0):
        with self.connect() as db:
            if identifier:
                rows = db.execute("SELECT * FROM entries WHERE id=? AND visible=1", (identifier,)).fetchall()
            else:
                rows = db.execute(
                    "SELECT * FROM entries WHERE visible=1 ORDER BY created_at DESC,id DESC LIMIT 12 OFFSET ?",
                    (offset,),
                ).fetchall()
        return [
            dict(
                id=r["id"],
                created_at=r["created_at"],
                measurement=json.loads(r["payload"]),
                **({"image": r["image"], "heatmap": r["heatmap"]} if identifier else {}),
            )
            for r in rows
        ]

    def write(self, action, owner, source="", payload=None, image="", heatmap="", identifier=None, moderator=False):
        with self.lock, self.connect() as db:
            if action == "status":
                row = db.execute(
                    "SELECT id,visible FROM entries WHERE owner=? AND source=?", (owner, source)
                ).fetchone()
                return dict(row) if row else None
            if action == "remove":
                cursor = db.execute(
                    "UPDATE entries SET visible=0 WHERE id=? AND (owner=? OR ?=1)", (identifier, owner, int(moderator))
                )
                if not cursor.rowcount:
                    raise HTTPException(404, "Published item is unavailable or belongs to another browser/account.")
                return {"removed": True}
            existing = db.execute(
                "SELECT id,visible FROM entries WHERE owner=? AND source=?", (owner, source)
            ).fetchone()
            if existing:
                return dict(existing)
            if (
                db.execute("SELECT count(*) FROM entries").fetchone()[0] >= 100
                or db.execute("SELECT count(*) FROM entries WHERE owner=?", (owner,)).fetchone()[0] >= 5
            ):
                raise HTTPException(429, "Demo gallery capacity reached (100 total, 5 per publisher).")
            identifier = uuid.uuid4().hex
            db.execute(
                "INSERT INTO entries VALUES(?,?,?,?,?,?,?,1)",
                (identifier, now(), owner, source, json.dumps(payload), image, heatmap),
            )
            return {"id": identifier, "visible": True}


class SupabaseGallery:
    def __init__(self, config, key):
        if not re.fullmatch("[a-f0-9]{64}", key):
            raise ValueError("DEFECTRAG_GALLERY_KEY must be a dedicated 64-character lowercase hex secret.")
        self.config = config
        self.key = key.encode()

    identity = LocalGallery.identity

    def rpc(self, name, payload):
        try:
            with httpx.Client(timeout=25, trust_env=False) as client:
                response = client.post(
                    self.config["url"] + "/rest/v1/rpc/" + name, headers={"apikey": self.config["key"]}, json=payload
                )
        except httpx.HTTPError:
            raise HTTPException(503, "Gallery storage is unavailable. Private inspection remains saved.") from None
        if not response.is_success:
            # Do not forward request bodies, secret, SQL details or database exceptions.
            code = (
                response.json().get("code")
                if response.headers.get("content-type", "").startswith("application/json")
                else None
            )
            if code == "P0001":
                raise HTTPException(
                    409, "Gallery operation declined: item withdrawn, publisher limit or gallery capacity reached."
                )
            raise HTTPException(503, "Gallery setup or storage is unavailable. Private inspection remains saved.")
        return response.json()

    def read(self, identifier=None, offset=0):
        return self.rpc("defectrag_gallery_read", {"p_id": identifier, "p_offset": offset})

    def write(self, action, owner, source="", payload=None, image="", heatmap="", identifier=None, moderator=False):
        return self.rpc(
            "defectrag_gallery_write",
            {
                "p_key": self.key.decode(),
                "p_action": action,
                "p_owner": owner,
                "p_source": source,
                "p_payload": payload or {},
                "p_image": image,
                "p_heatmap": heatmap,
                "p_id": identifier,
                "p_moderator": moderator,
            },
        )


class PublishConsent(BaseModel):
    model_config = ConfigDict(extra="forbid")
    consent: StrictBool


def mount_gallery(app, repo, artifacts, gallery=None, owner=lambda: "local-operator", cloud_origin=""):
    def ready():
        if gallery is None:
            raise HTTPException(
                503, "Community gallery is awaiting its separate Supabase setup. Private inspections are unaffected."
            )
        return gallery

    def principal():
        current = repo.current() if hasattr(repo, "current") else repo
        staff = getattr(current, "staff", None)
        user = getattr(current, "user", None)
        return ("staff:" + user["id"] if user else "browser:" + owner()), bool(staff)

    def keys(identifier):
        actor, staff = principal()
        return ready().identity(actor), ready().identity("inspection:" + identifier), staff

    def saved(identifier):
        if not re.fullmatch("[a-f0-9]{32}", identifier):
            raise HTTPException(404, "Inspection not found")
        record = repo.get(identifier)
        if not record:
            raise HTTPException(404, "Inspection not found")
        return record

    @app.get("/api/gallery")
    def listing(offset: int = Query(0, ge=0, le=100)):
        if gallery is None:
            return {"data": {"enabled": False, "items": []}}
        return {"data": {"enabled": True, "items": gallery.read(offset=offset)}}

    @app.get("/api/gallery/{identifier}")
    def detail(identifier: str):
        try:
            uuid.UUID(identifier)
        except ValueError:
            raise HTTPException(404, "Gallery item not found") from None
        rows = ready().read(identifier)
        if not rows:
            raise HTTPException(404, "Gallery item not found or withdrawn")
        return {"data": rows[0]}

    @app.get("/api/inspections/{identifier}/publication")
    def publication(identifier: str):
        if gallery is None:
            return {
                "data": {
                    "enabled": False,
                    "publication": None,
                    "runtime": "local"
                    if getattr(type(repo), "local_sync_enabled", False) or not hasattr(repo, "current")
                    else "cloud",
                }
            }
        saved(identifier)
        actor, source, _ = keys(identifier)
        return {"data": {"enabled": True, "publication": gallery.write("status", actor, source)}}

    @app.post("/api/inspections/{identifier}/publication")
    def publish(identifier: str, body: PublishConsent):
        ready()
        if body.consent is not True:
            raise HTTPException(422, "Explicit public publication consent is required.")
        record = saved(identifier)
        actor, source, _ = keys(identifier)
        if record.get("storage", "").startswith("supabase"):
            current = repo.current()
            if record.get("owner_id") != current.user["id"]:
                raise HTTPException(403, "Only the original staff uploader can publish this inspection.")
        measurement = public_measurement(record)
        previews = []
        for kind, suffix in (("image", ""), ("heatmap", "_heatmap")):
            path = Path(artifacts() if callable(artifacts) else artifacts) / f"{identifier}{suffix}.jpg"
            if not record.get("storage", "").startswith("supabase"):
                if not path.is_file():
                    raise HTTPException(409, "Saved image is unavailable. Nothing was published.")
                content = path.read_bytes()
            else:
                if getattr(current, "config", {}).get("auth_workflow_enabled"):
                    # The MFA-enabled repository deliberately returns scoped API
                    # URLs, not signed Storage links. Read under the user's JWT
                    # so owner/RLS/MFA checks still apply to public publication.
                    content = current.private_artifact(identifier, kind)
                    if len(content) > 5 * 1024 * 1024:
                        raise HTTPException(413, "Saved image exceeds gallery limit.")
                    previews.append(thumbnail(content))
                    continue
                url = record.get(kind + "_url", "")
                if (
                    not cloud_origin
                    or urlparse(url).netloc != urlparse(cloud_origin).netloc
                    or not url.startswith("https://")
                ):
                    raise HTTPException(403, "Saved image origin is not authorized.")
                try:
                    with httpx.Client(timeout=20, trust_env=False, follow_redirects=False) as client:
                        with client.stream("GET", url) as response:
                            response.raise_for_status()
                            chunks = []
                            size = 0
                            for chunk in response.iter_bytes():
                                size += len(chunk)
                                if size > 5 * 1024 * 1024:
                                    raise HTTPException(413, "Saved image exceeds gallery limit.")
                                chunks.append(chunk)
                            content = b"".join(chunks)
                except httpx.HTTPError:
                    raise HTTPException(503, "Saved image is temporarily unavailable.") from None
            previews.append(thumbnail(content))
        return {"data": gallery.write("publish", actor, source, measurement, *previews)}

    @app.post("/api/gallery/{identifier}/withdraw")
    def withdraw(identifier: str):
        try:
            uuid.UUID(identifier)
        except ValueError:
            raise HTTPException(404, "Gallery item not found") from None
        actor, _, _ = keys("withdraw")
        return {"data": gallery.write("remove", actor, identifier=identifier)}

    @app.post("/api/gallery/{identifier}/moderate")
    def moderate(identifier: str):
        try:
            uuid.UUID(identifier)
        except ValueError:
            raise HTTPException(404, "Gallery item not found") from None
        actor, _, staff = keys("moderate")
        if not staff:
            raise HTTPException(403, "Approved staff access is required for gallery moderation.")
        return {"data": gallery.write("remove", actor, identifier=identifier, moderator=True)}