loopable / api /catalog_render.py
fsanyoto's picture
Deploy AIOS web (React glide grid + FastAPI slice)
22987e4 verified
Raw History Blame Contribute Delete
17.7 kB
"""Render stored C2 catalog pages through scoped source and image readers."""
from __future__ import annotations
import http.client
import io
import ipaddress
import math
from decimal import Decimal, InvalidOperation
from pathlib import Path
import re
import socket
import ssl
import time
from urllib.parse import urlsplit
import app_rules
import catalog_store
import dash_norm
from deps import err
MAX_IMAGE_BYTES = 5 * 1024 * 1024
MAX_IMAGE_PIXELS = 16_000_000
IMAGE_TIMEOUT = 5
_PAPERS = {"letter": (215.9, 279.4), "a4": (210, 297), "tabloid": (279.4, 431.8)}
def admit_catalog(session, catalog_id, *, ai=False):
"""Recheck tool, catalog, App, source, field and credential walls on every export."""
if not isinstance(catalog_id, str) or not catalog_id.strip() or len(catalog_id) > 256:
raise err(400, "invalid_catalog", "Choose a stored catalog id from the Catalog App.")
if ai:
from core import perm_scope
reason = perm_scope.ai_apps_refusal(session.user, session.runtime)
if reason:
raise err(403, "ai_apps_off", reason)
try:
record = catalog_store.read_catalog(session, catalog_id)
except (AttributeError, KeyError, TypeError):
raise err(400, "invalid_catalog", "This catalog contains malformed pages or bindings. Repair it before exporting.") from None
# A library can retain orphan records. A PDF needs a current, readable source App.
app, _fields = catalog_store.require_app(session, record["appId"])
app_rules.validate_catalog_display({"catalogs": [record]})
if not record.get("pages"):
raise err(400, "catalog_empty", "Add a page to this catalog before exporting it.")
return record, app
def _public_target(value):
if (not isinstance(value, str) or len(value) > 4096 or "\\" in value
or any(char.isspace() or ord(char) < 32 or ord(char) == 127 for char in value)):
raise ValueError("Invalid image URL.")
url = urlsplit(value)
host = (url.hostname or "").lower().rstrip(".")
if (url.scheme not in ("http", "https") or not host or url.username is not None
or url.password is not None or url.fragment or "%" in host
or url.port not in (None, 80 if url.scheme == "http" else 443)
or host == "localhost" or "." not in host and ":" not in host
or host.endswith((".localhost", ".local", ".internal", ".home", ".lan"))):
raise ValueError("Invalid image URL.")
port = url.port or (443 if url.scheme == "https" else 80)
addresses = socket.getaddrinfo(host, port, type=socket.SOCK_STREAM)
if not addresses:
raise ValueError("Image host is unavailable.")
for _family, _type, _proto, _name, address in addresses:
ip = ipaddress.ip_address(address[0])
if (not ip.is_global or ip.is_multicast or ip.is_reserved
or getattr(ip, "ipv4_mapped", None) is not None
or getattr(ip, "sixtofour", None) is not None
or getattr(ip, "teredo", None) is not None):
raise ValueError("Image host is unavailable.")
return url, host, port, addresses[0]
def _fetch_public(value):
url, host, port, address = _public_target(value)
family, socktype, protocol, _name, sockaddr = address
connection = http.client.HTTPConnection(host, port, timeout=IMAGE_TIMEOUT)
sock = socket.socket(family, socktype, protocol)
deadline = time.monotonic() + IMAGE_TIMEOUT
try:
sock.settimeout(IMAGE_TIMEOUT)
# Connect to the checked numeric address without a second DNS lookup or an env proxy.
sock.connect(sockaddr)
if url.scheme == "https":
sock = ssl.create_default_context().wrap_socket(sock, server_hostname=host)
connection.sock = sock
target = url.path or "/"
if url.query:
target += "?" + url.query
connection.request("GET", target, headers={"Accept": "image/png,image/jpeg,image/webp",
"Accept-Encoding": "identity"})
with connection.getresponse() as response:
# No redirect is followed, including a public URL redirecting to an internal host.
if response.status != 200 or response.getheader("Content-Encoding", "identity") != "identity":
return None
if response.getheader("Content-Type", "").split(";", 1)[0].lower() not in (
"image/png", "image/jpeg", "image/webp", "image/gif"):
return None
length = response.getheader("Content-Length")
if length is not None and (not length.isdecimal() or int(length) > MAX_IMAGE_BYTES):
return None
chunks, size = [], 0
while True:
remaining = deadline - time.monotonic()
if remaining <= 0:
return None
sock.settimeout(remaining)
chunk = response.read1(min(64 * 1024, MAX_IMAGE_BYTES + 1 - size))
if not chunk:
return b"".join(chunks)
size += len(chunk)
if size > MAX_IMAGE_BYTES:
return None
chunks.append(chunk)
finally:
connection.close()
sock.close()
def _internal_image(session, reference):
import routes_assets
# Canonical routes take the tenant from this session, never from a URL or reference.
match = re.fullmatch(r"/api/v1/assets/(records|amazon|products|editorial)/([^/?#]+)(?:\?q=(?:web|print|orig))?", reference)
if match:
namespace, key = match.groups()
elif reference.startswith("rec:"):
namespace, key = "records", reference[4:]
elif reference.startswith("amz:"):
namespace, key = "amazon", reference[4:]
elif reference.startswith("ed:"):
namespace, key = "editorial", reference[3:]
else:
namespace, key = "products", reference
if namespace == "amazon":
# The bytes come from this session's tenant store, never from an Amazon host.
raw = routes_assets.amazon_picture_bytes(session, key)
return raw[:MAX_IMAGE_BYTES + 1] if raw else None
if namespace == "records":
response = routes_assets.record_asset(key, q="web", session=session)
else:
# Calling a route function bypasses Depends, so repeat its module grant explicitly.
from routes_external import _assert_database_scope
_assert_database_scope(session, routes_assets.MODULE)
session.require(routes_assets.MODULE)
reader = routes_assets.product_asset if namespace == "products" else routes_assets.editorial_asset
response = reader(key, q="web", session=session)
with Path(response.path).open("rb") as handle:
return handle.read(MAX_IMAGE_BYTES + 1)
def read_image(session, reference):
"""Return bounded raster pixels or a quiet gap without forwarding credentials to a URL."""
from fastapi import HTTPException
from PIL import Image, UnidentifiedImageError
if not isinstance(reference, str) or not reference or len(reference) > 4096:
return None
try:
if reference.startswith(("http://", "https://")):
raw = _fetch_public(reference)
elif reference.startswith(("rec:", "amz:", "ed:", "/api/v1/assets/")) or re.fullmatch(r"[A-Za-z0-9_.-]{1,128}", reference):
raw = _internal_image(session, reference)
else:
return None
if not raw or len(raw) > MAX_IMAGE_BYTES:
return None
with Image.open(io.BytesIO(raw)) as image:
if image.format not in ("PNG", "JPEG", "WEBP", "GIF") or image.width * image.height > MAX_IMAGE_PIXELS:
return None
image.load()
image.thumbnail((1600, 1600))
return image.convert("RGBA")
except (OSError, ValueError, HTTPException, http.client.HTTPException,
UnidentifiedImageError, Image.DecompressionBombError):
return None
def page_model(session, record, app):
"""Bind page source keys through the App roles and the permission-filtered query door."""
from loopy_file_tools import read_all
field_map = app["fieldMap"]
pages = catalog_store.normalize_pages(record["pages"])
keys = list(dict.fromkeys(key for page in pages for key in page["bindings"].get("products", [])))
products = {}
if keys:
# Query filters allow at most 50 sibling conditions per key batch.
batch_size = 50
for start in range(0, len(keys), batch_size):
result = read_all(session, app["sourceDatabase"], fields=list(dict.fromkeys(field_map.values())),
filters={"conj": "or", "nodes": [
{"colId": field_map["key"], "op": "eq", "value": key}
for key in keys[start:start + batch_size]]})
for row in result["records"]:
key = str(row.get(field_map["key"], "")).strip()
if key not in keys:
continue
if key in products:
raise err(409, "catalog_key_ambiguous", "Catalog source keys must be unique. Rebind the App to a unique key.")
products[key] = {role: row.get(field) for role, field in field_map.items()}
cache = {}
output = []
for page in pages:
bound = page["bindings"]
selected = []
for key in bound.get("products", []):
# Hidden or removed rows contribute no source values, including image URLs.
product = products.get(key)
if product is None:
selected.append({"missing": True})
continue
product = dict(product)
reference = product.get("image")
if isinstance(reference, str) and reference not in cache:
cache[reference] = read_image(session, reference)
product["pixels"] = cache.get(reference) if isinstance(reference, str) else None
selected.append(product)
output.append({**page, "design": app_rules.catalog_page_design(page), "items": selected})
return output
def _rgb(value):
digits = value.lstrip("#")
if len(digits) == 3:
digits = "".join(char * 2 for char in digits)
return tuple(int(digits[index:index + 2], 16) for index in (0, 2, 4))
def _text(value):
from loopy_pdf import _latin1
return _latin1(dash_norm.plain("" if value is None else str(value)))[0]
def _label(pdf, value, x, y, width, height, *, size=11, bold=False, align="L"):
pdf.set_xy(x, y)
pdf.set_font(pdf.catalog_font, "B" if bold else "", size)
pdf.c_margin = 0
text = _text(value).replace("\n", " ").replace("\r", " ")
measured = pdf.get_string_width(text)
if measured > width:
pdf.set_font_size(max(1, size * width / measured))
pdf.cell(width, height, text, align=align)
def _paragraph(pdf, value, x, y, width, height, *, size=11, align="L"):
pdf.set_font(pdf.catalog_font, "", size)
pdf.c_margin = 0
text = _text(value).replace("\r", "")
# Fit authored text on its stored page instead of silently adding PDF pages.
for _attempt in range(12):
lines = pdf.multi_cell(width, size * 0.5, text, dry_run=True, output="LINES")
if len(lines) * size * 0.5 <= height:
break
size *= 0.85
pdf.set_font_size(size)
pdf.set_xy(x, y)
pdf.multi_cell(width, size * 0.5, text, align=align)
def _picture(pdf, pixels, box, design):
from PIL import ImageOps
x, y, width, height = box
if pixels is None:
pdf.set_draw_color(*_rgb(design["accent"]))
pdf.set_line_width(0.2)
pdf.rect(x + width * 0.35, y + height * 0.35, width * 0.3, height * 0.3)
return
if design["imageFit"] == "cover":
pixels = ImageOps.fit(pixels, (max(1, round(width * 5)), max(1, round(height * 5))))
pdf.image(pixels, x=x, y=y, w=width, h=height, keep_aspect_ratio=True)
def _product(pdf, item, box, design):
x, y, width, height = box
scale = min(1, height / 48, width / 40)
image_height = height - 25 * scale
_picture(pdf, item.get("pixels"), (x, y, width, image_height), design)
if item.get("missing"):
return
align = {"left": "L", "center": "C", "right": "R"}[design["align"]]
_label(pdf, item.get("title"), x, y + image_height + 2 * scale, width, 6 * scale,
size=11 * scale, bold=True, align=align)
# SKU labels and source identity are separate roles. Price is never multiplied by pack.
sku = item.get("sku")
if sku is None or sku == "":
sku = item.get("key")
_label(pdf, sku, x, y + image_height + 8 * scale, width, 5 * scale,
size=9 * scale, align=align)
price = item.get("price")
try:
price = Decimal(str(price)) if not isinstance(price, bool) else None
except InvalidOperation:
price = None
if price is not None and price.is_finite():
value = f"{price:,.2f}" if abs(price.adjusted()) < 24 else str(price)
_label(pdf, value, x, y + image_height + 14 * scale, width, 6 * scale, size=12 * scale, align=align)
if item.get("description"):
_label(pdf, item["description"], x, y + image_height + 20 * scale, width, 5 * scale, size=8 * scale, align=align)
def render_pages(record, pages):
"""Draw exactly one PDF page per stored page using the shared template design model."""
from fpdf import FPDF
paper = record.get("paper", "letter")
orientation = record.get("orientation", "portrait")
if not isinstance(paper, str) or paper not in _PAPERS or orientation not in ("portrait", "landscape"):
raise err(400, "invalid_catalog_paper", "Choose a supported catalog paper and orientation.")
pdf = FPDF(orientation="L" if orientation == "landscape" else "P", format=_PAPERS[paper])
pdf.set_auto_page_break(False)
pdf.set_title(_text(record["name"]))
for index, page in enumerate(pages):
pdf.add_page()
design, bindings = page["design"], page["bindings"]
kind = app_rules.CATALOG_KIND_ALIASES.get(page["kind"], page["kind"])
pdf.catalog_font = "Times" if design["fontFamily"] == "serif" else "Helvetica"
pdf.set_fill_color(*_rgb(design["background"]))
pdf.rect(0, 0, pdf.w, pdf.h, style="F")
pdf.set_text_color(*_rgb(design["color"]))
margin, gap = 18, 8
width, bottom = pdf.w - margin * 2, pdf.h - 23
align = {"left": "L", "center": "C", "right": "R"}[design["align"]]
centered = design["layout"] == "centered"
title_y = 36 if centered and kind in ("cover", "closing", "section") else 18
_label(pdf, bindings.get("title", ""), margin, title_y, width, 18, size=28, bold=True, align=align)
pdf.set_draw_color(*_rgb(design["accent"]))
pdf.set_line_width(0.6)
pdf.line(margin, title_y + 21, pdf.w - margin, title_y + 21)
body = bindings.get("body", "")
content_y = title_y + 28
items = page["items"]
if kind == "toc":
entries = [(other["bindings"].get("title", ""), number + 1)
for number, other in enumerate(pages) if other["id"] != page["id"]]
if body:
_paragraph(pdf, body, margin, content_y, width, 45, align=align)
content_y += 52
columns = design["columns"]
rows = max(1, math.ceil(len(entries) / columns))
line_height = min(13, (bottom - content_y) / rows)
column_width = (width - (columns - 1) * gap) / columns
for entry, (title, folio) in enumerate(entries):
x = margin + (entry // rows) * (column_width + gap)
y = content_y + (entry % rows) * line_height
_label(pdf, title, x, y, column_width - 12, line_height, align=align)
_label(pdf, folio, x + column_width - 10, y, 10, line_height, align="R")
elif design["layout"] == "split" and items:
left = width * 0.4
_paragraph(pdf, body, margin, content_y, left, bottom - content_y, align=align)
_draw_items(pdf, items, (margin + left + gap, content_y, width - left - gap, bottom - content_y), design, kind)
else:
if body:
body_height = min(65, (bottom - content_y) * 0.3) if items else bottom - content_y
_paragraph(pdf, body, margin, content_y, width, body_height, align=align)
content_y += body_height + gap
if items:
_draw_items(pdf, items, (margin, content_y, width, bottom - content_y), design, kind)
_label(pdf, str(index + 1), margin, pdf.h - 16, width, 5, size=9, align="C")
return bytes(pdf.output())
def _draw_items(pdf, items, box, design, kind):
x, y, width, height = box
columns = design["columns"]
rows = math.ceil(len(items) / columns)
gap = min(6, width / (columns * 4), height / (rows * 4))
cell_w = (width - gap * (columns - 1)) / columns
cell_h = (height - gap * (rows - 1)) / rows
for index, item in enumerate(items):
cell = (x + index % columns * (cell_w + gap), y + index // columns * (cell_h + gap), cell_w, cell_h)
if kind == "cover":
_picture(pdf, item.get("pixels"), cell, design)
else:
_product(pdf, item, cell, design)
def render_catalog(session, catalog_id, *, ai=False):
record, app = admit_catalog(session, catalog_id, ai=ai)
pages = page_model(session, record, app)
return render_pages(record, pages), record, app