feat: audit media catalog integrity

This commit is contained in:
ik
2026-09-12 15:57:04 +07:00
parent 215af73388
commit 883ad2c73b
6 changed files with 62 additions and 3 deletions
+34
View File
@@ -120,6 +120,40 @@ def review_asset(
return item
def audit_media_catalog(root: Path) -> dict:
manifest = json.loads((root / "manifest.json").read_text(encoding="utf-8"))
statuses: dict[str, int] = {}
issues: list[str] = []
referenced: set[str] = set()
for item in manifest.get("assets", []):
status = str(item.get("status", "unknown"))
statuses[status] = statuses.get(status, 0) + 1
local_path = item.get("local_path")
if status in {"stored", "approved"}:
if not isinstance(local_path, str):
issues.append(f"{item['asset_url']}: {status} asset has no local_path")
continue
referenced.add(local_path)
target = root / local_path
if not target.is_file():
issues.append(f"{item['asset_url']}: local file is missing")
continue
body = target.read_bytes()
if hashlib.sha256(body).hexdigest() != item.get("sha256"):
issues.append(f"{item['asset_url']}: SHA-256 mismatch")
try:
width, height, mime = inspect_image(body)
if (width, height, mime) != (item.get("width"), item.get("height"), item.get("content_type")):
issues.append(f"{item['asset_url']}: image metadata mismatch")
except ValueError as exc:
issues.append(f"{item['asset_url']}: {exc}")
if status == "approved" and (not item.get("entity_key") or item.get("entity_type") not in {"fish", "waterbody", "tackle", "reference"}):
issues.append(f"{item['asset_url']}: approved asset has no valid canonical mapping")
files_root = root / "files"
orphaned = sorted(str(path.relative_to(root)) for path in files_root.rglob("*") if path.is_file() and str(path.relative_to(root)) not in referenced) if files_root.exists() else []
return {"total": sum(statuses.values()), "statuses": statuses, "issues": issues, "orphaned_files": orphaned}
def inspect_image(body: bytes) -> tuple[int, int, str]:
try:
with Image.open(io.BytesIO(body)) as image:
+6 -1
View File
@@ -8,7 +8,7 @@ import urllib.error
import urllib.request
from .community_cli import USER_AGENT, _StrictRedirectHandler, _validate_url_before_io, check_and_reserve, fetch_html, fetch_site_key
from .media_assets import extract_media_candidates, merge_manifest, reclassify_manifest, review_asset, store_asset
from .media_assets import audit_media_catalog, extract_media_candidates, merge_manifest, reclassify_manifest, review_asset, store_asset
DEFAULT_ROOT = Path("data/media")
@@ -57,10 +57,15 @@ def main(argv: list[str] | None = None) -> int:
parser.add_argument("--entity-type", choices=("fish", "waterbody", "tackle", "reference"))
parser.add_argument("--entity-key")
parser.add_argument("--note")
parser.add_argument("--audit", action="store_true", help="Verify manifest metadata, hashes and local files without network access")
parser.add_argument("--root", type=Path, default=DEFAULT_ROOT)
parser.add_argument("--state-file", type=Path, default=Path(".cache/community-fetch-state.json"))
args = parser.parse_args(argv)
try:
if args.audit:
report = audit_media_catalog(args.root)
print(json.dumps(report, ensure_ascii=False, indent=2))
return 1 if report["issues"] or report["orphaned_files"] else 0
if args.review_url:
if not args.decision:
parser.error("--decision is required with --review-url")