Files
mcphub/core/media_audit.py
airano-ir f203ca88de
Some checks failed
Release / Test before release (push) Has been cancelled
Release / Publish to PyPI (push) Has been cancelled
Release / Publish to Docker Hub (push) Has been cancelled
Release / Create GitHub Release (push) Has been cancelled
feat(v3.12.0): media pipeline, AI image generation, capability probe, companion v2.9.0
Three-month batch sync from internal repo (~80 commits) covering Tracks F.5a, F.7e, F.8, F.17, F.18, F.X.

WordPress media pipeline
- Pillow-based optimization, AI image generation (OpenAI / Stability / Replicate / Google Nano Banana / OpenRouter), chunked + resumable uploads, bulk delete/reassign, idempotent retries.

Capability discovery (F.7e)
- Per-site credential probe + adapters for WordPress / WooCommerce / Gitea, tier-fit unions granted ∪ roles, capability badge UI with HTMX partial re-check, install hint in every companion-unreachable error.

Companion plugin overhaul
- Renamed wordpress-plugin/airano-mcp-seo-bridge → wordpress-plugin/airano-mcp-bridge.
- Eight new endpoints: /capabilities, /bulk-meta, /export, /cache-purge, /transient-flush, /site-health, /audit-hook, /upload-and-attach.
- wp.org Plugin Check pass: i18n, WP_Filesystem, scheme allowlist on audit-hook URL.

Other
- Gitea ergonomics (F.17): batch files, tree, search, compare, releases, fork.
- Opportunistic bcrypt upgrade for legacy SHA-256 admin keys (F.8).
- n8n refactor: structured errors, capability probe, missing tools backfilled.
- Idempotency-Key dedup for AI media upload retries; WP client fast-fails on unreachable sites.

Docs
- README + CLAUDE.md drop the fixed "633 tools" claim. The total grows with each release; per-plugin approximations + dashboard-surfaced counts replace it.
- Tools/Tests badges removed in favour of "Plugins: 10".

Deployment
- PyPI mirror chain, optional BUILD_HTTP_PROXY, Alpine→Yandex apk mirror, Debian-slim Plan-B Dockerfile, mirror.gcr.io variant.

CI
- Black + Ruff clean on Python 3.12; pytest tests/ green.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-25 16:25:58 +02:00

68 lines
2.1 KiB
Python

"""F.5a.6.4 — Audit-log emission for media uploads.
One ``media.upload`` entry is written per **successful** upload regardless
of source (base64 / url / chunked / ai:<provider>). Failures are intentionally
NOT logged here — they surface to the caller as typed ``UploadError`` JSON
and to the dashboard via the existing tool-call audit emitted by the
ToolRouter wrapper. Logging failures twice would double-count error rates.
GDPR: never log raw bytes, base64, prompts, or URLs that may carry tokens.
"""
from __future__ import annotations
import logging
from typing import Any
logger = logging.getLogger(__name__)
def log_media_upload(
*,
site: str | None,
user_id: str | None,
mime: str | None,
size_bytes: int,
source: str,
media_id: int | None,
cost_usd: float | None = None,
) -> None:
"""Emit a single ``media.upload`` audit entry. Best-effort — never raises.
Args:
site: Site URL or alias (whichever the handler has on hand).
user_id: Calling user id (None for admin / env-fallback).
mime: Sniffed MIME type after security validation.
size_bytes: Final uploaded byte count (post-optimization).
source: One of ``"base64"``, ``"url"``, ``"chunked"``, ``"ai:<provider>"``.
media_id: WordPress media library id of the resulting attachment.
cost_usd: Provider cost in USD for AI-generated uploads only.
"""
try:
from core.audit_log import get_audit_logger
audit = get_audit_logger()
except Exception: # noqa: BLE001
return
params: dict[str, Any] = {
"site": site,
"mime": mime,
"size_bytes": int(size_bytes),
"source": source,
"media_id": media_id,
}
if cost_usd is not None:
params["cost_usd"] = round(float(cost_usd), 6)
try:
audit.log_tool_call(
tool_name="media.upload",
site=site,
params=params,
result_summary=(f"{source} {size_bytes}B {mime or '?'} -> media_id={media_id}"),
user_id=user_id,
)
except Exception: # noqa: BLE001
logger.debug("media.upload audit emit failed", exc_info=True)