Spaces:
Running
Running
Update app/routers/screenshots.py
Browse files- app/routers/screenshots.py +267 -226
app/routers/screenshots.py
CHANGED
|
@@ -1,226 +1,267 @@
|
|
| 1 |
-
import asyncio
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
from app.core.
|
| 9 |
-
from app.
|
| 10 |
-
from app.
|
| 11 |
-
from app.services.
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
from app.services.
|
| 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 |
-
await
|
| 182 |
-
{
|
| 183 |
-
"
|
| 184 |
-
"
|
| 185 |
-
"
|
| 186 |
-
"
|
| 187 |
-
"
|
| 188 |
-
"reason": reason,
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import logging
|
| 3 |
+
import uuid
|
| 4 |
+
from datetime import datetime, timedelta, timezone
|
| 5 |
+
|
| 6 |
+
from fastapi import APIRouter, Depends, File, Form, UploadFile
|
| 7 |
+
|
| 8 |
+
from app.core.auth import AuthUser, get_current_web_user
|
| 9 |
+
from app.core.config import settings
|
| 10 |
+
from app.models.schemas import ScreenshotIngestResponse, ViolationScreenshot
|
| 11 |
+
from app.services.alert_hub import alert_hub
|
| 12 |
+
from app.services.classifier import (
|
| 13 |
+
classify_game_with_ocr_llm,
|
| 14 |
+
classify_sensitive_content,
|
| 15 |
+
)
|
| 16 |
+
from app.services.policy_window import is_restriction_active
|
| 17 |
+
from app.services.repos import (
|
| 18 |
+
audit_repo,
|
| 19 |
+
policy_repo,
|
| 20 |
+
review_repo,
|
| 21 |
+
url_verdict_repo,
|
| 22 |
+
violation_repo,
|
| 23 |
+
)
|
| 24 |
+
from app.services.storage import storage_service
|
| 25 |
+
|
| 26 |
+
router = APIRouter(prefix="/screenshots", tags=["screenshots"])
|
| 27 |
+
logger = logging.getLogger(__name__)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
async def _cleanup_expired_violations() -> None:
|
| 31 |
+
expired = await violation_repo.list_expired()
|
| 32 |
+
for row in expired:
|
| 33 |
+
absolute_path = row.get("absolute_path")
|
| 34 |
+
if isinstance(absolute_path, str):
|
| 35 |
+
storage_service.delete(absolute_path)
|
| 36 |
+
await violation_repo.delete_expired()
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
@router.post("/ingest", response_model=ScreenshotIngestResponse)
|
| 40 |
+
async def ingest_screenshot(
|
| 41 |
+
device_id: str = Form(...),
|
| 42 |
+
timestamp: str = Form(...),
|
| 43 |
+
suspected_game: bool = Form(False),
|
| 44 |
+
file: UploadFile = File(...),
|
| 45 |
+
) -> ScreenshotIngestResponse:
|
| 46 |
+
screenshot_id = str(uuid.uuid4())
|
| 47 |
+
_, absolute_path, stored_name = await storage_service.save_upload(file)
|
| 48 |
+
logger.info(
|
| 49 |
+
"ingest start screenshot_id=%s device_id=%s filename=%s suspected_game=%s",
|
| 50 |
+
screenshot_id,
|
| 51 |
+
device_id,
|
| 52 |
+
file.filename or "unknown.png",
|
| 53 |
+
suspected_game,
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
try:
|
| 57 |
+
moment = datetime.fromisoformat(timestamp.replace("Z", "+00:00"))
|
| 58 |
+
except ValueError:
|
| 59 |
+
moment = datetime.now(timezone.utc)
|
| 60 |
+
policy_doc = await policy_repo.get(device_id)
|
| 61 |
+
policy_payload = policy_doc.get("policy", {}) if policy_doc else {}
|
| 62 |
+
windows = policy_payload.get("restriction_windows", [])
|
| 63 |
+
if not is_restriction_active(windows, moment):
|
| 64 |
+
storage_service.delete(absolute_path)
|
| 65 |
+
logger.info(
|
| 66 |
+
"ingest drop screenshot_id=%s reason=outside_restriction_window",
|
| 67 |
+
screenshot_id,
|
| 68 |
+
)
|
| 69 |
+
await audit_repo.log(
|
| 70 |
+
{
|
| 71 |
+
"screenshot_id": screenshot_id,
|
| 72 |
+
"device_id": device_id,
|
| 73 |
+
"timestamp": timestamp,
|
| 74 |
+
"filename": file.filename or "unknown.png",
|
| 75 |
+
"action": "deleted_outside_window",
|
| 76 |
+
"reason": "outside_restriction_window",
|
| 77 |
+
"created_at": datetime.now(timezone.utc),
|
| 78 |
+
}
|
| 79 |
+
)
|
| 80 |
+
return ScreenshotIngestResponse(screenshot_id=screenshot_id)
|
| 81 |
+
|
| 82 |
+
game_task = asyncio.to_thread(
|
| 83 |
+
classify_game_with_ocr_llm,
|
| 84 |
+
absolute_path,
|
| 85 |
+
file.filename or "capture.png",
|
| 86 |
+
suspected_game,
|
| 87 |
+
)
|
| 88 |
+
sensitive_task = asyncio.to_thread(
|
| 89 |
+
classify_sensitive_content,
|
| 90 |
+
absolute_path,
|
| 91 |
+
file.filename or "capture.png",
|
| 92 |
+
)
|
| 93 |
+
game_raw, sensitive_raw = await asyncio.gather(game_task, sensitive_task, return_exceptions=True)
|
| 94 |
+
|
| 95 |
+
if isinstance(game_raw, Exception):
|
| 96 |
+
logger.warning("ingest game-check-error screenshot_id=%s err=%s", screenshot_id, repr(game_raw))
|
| 97 |
+
game_result = {
|
| 98 |
+
"verdict": "not_game",
|
| 99 |
+
"confidence": 0.1,
|
| 100 |
+
"reason": "game-check-error",
|
| 101 |
+
"urls": [],
|
| 102 |
+
"source": "error-fallback",
|
| 103 |
+
}
|
| 104 |
+
else:
|
| 105 |
+
game_result = game_raw
|
| 106 |
+
|
| 107 |
+
if isinstance(sensitive_raw, Exception):
|
| 108 |
+
logger.warning("ingest sensitive-check-error screenshot_id=%s err=%s", screenshot_id, repr(sensitive_raw))
|
| 109 |
+
sensitive_detected, sensitive_confidence, sensitive_reason = (False, 0.0, "sensitive-check-error")
|
| 110 |
+
else:
|
| 111 |
+
sensitive_detected, sensitive_confidence, sensitive_reason = sensitive_raw
|
| 112 |
+
|
| 113 |
+
ocr_urls = [str(x) for x in game_result.get("urls", [])]
|
| 114 |
+
|
| 115 |
+
cached_game_url = await url_verdict_repo.get_cached_game_url(ocr_urls)
|
| 116 |
+
if cached_game_url:
|
| 117 |
+
logger.info(
|
| 118 |
+
"ingest cache-hit screenshot_id=%s game_url=%s",
|
| 119 |
+
screenshot_id,
|
| 120 |
+
cached_game_url,
|
| 121 |
+
)
|
| 122 |
+
game_result = {
|
| 123 |
+
**game_result,
|
| 124 |
+
"verdict": "game",
|
| 125 |
+
"confidence": 0.99,
|
| 126 |
+
"reason": f"cached-game-url:{cached_game_url}",
|
| 127 |
+
"source": "url-cache",
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
verdict = str(game_result.get("verdict", "not_game"))
|
| 131 |
+
confidence = float(game_result.get("confidence", 0.1))
|
| 132 |
+
reason = str(game_result.get("reason", "no-game-evidence"))
|
| 133 |
+
|
| 134 |
+
if sensitive_detected:
|
| 135 |
+
logger.info(
|
| 136 |
+
"ingest verdict screenshot_id=%s verdict=sensitive confidence=%.2f reason=%s",
|
| 137 |
+
screenshot_id,
|
| 138 |
+
sensitive_confidence,
|
| 139 |
+
sensitive_reason,
|
| 140 |
+
)
|
| 141 |
+
image_url = f"{settings.media_prefix}/{stored_name}"
|
| 142 |
+
await violation_repo.insert(
|
| 143 |
+
{
|
| 144 |
+
"screenshot_id": screenshot_id,
|
| 145 |
+
"device_id": device_id,
|
| 146 |
+
"timestamp": timestamp,
|
| 147 |
+
"filename": file.filename or "unknown.png",
|
| 148 |
+
"created_at": datetime.now(timezone.utc).isoformat(),
|
| 149 |
+
"image_url": image_url,
|
| 150 |
+
"confidence": sensitive_confidence,
|
| 151 |
+
"reason": sensitive_reason,
|
| 152 |
+
"absolute_path": absolute_path,
|
| 153 |
+
"visibility_scope": "sensitive_admin",
|
| 154 |
+
"ocr_urls": ocr_urls,
|
| 155 |
+
"expires_at": datetime.now(timezone.utc) + timedelta(hours=24),
|
| 156 |
+
}
|
| 157 |
+
)
|
| 158 |
+
await alert_hub.broadcast_admin(
|
| 159 |
+
{
|
| 160 |
+
"type": "sensitive_detected",
|
| 161 |
+
"device_id": device_id,
|
| 162 |
+
"message": "Sensitive content detected. Routed to sensitive admin scope.",
|
| 163 |
+
"visibility_scope": "sensitive_admin",
|
| 164 |
+
}
|
| 165 |
+
)
|
| 166 |
+
await _cleanup_expired_violations()
|
| 167 |
+
return ScreenshotIngestResponse(screenshot_id=screenshot_id)
|
| 168 |
+
|
| 169 |
+
if verdict == "game":
|
| 170 |
+
await url_verdict_repo.upsert_game_urls(ocr_urls, source=str(game_result.get("source", "llm")))
|
| 171 |
+
|
| 172 |
+
if verdict == "not_game":
|
| 173 |
+
storage_service.delete(absolute_path)
|
| 174 |
+
logger.info(
|
| 175 |
+
"ingest verdict screenshot_id=%s verdict=not_game confidence=%.2f reason=%s urls=%d",
|
| 176 |
+
screenshot_id,
|
| 177 |
+
confidence,
|
| 178 |
+
reason,
|
| 179 |
+
len(ocr_urls),
|
| 180 |
+
)
|
| 181 |
+
await audit_repo.log(
|
| 182 |
+
{
|
| 183 |
+
"screenshot_id": screenshot_id,
|
| 184 |
+
"device_id": device_id,
|
| 185 |
+
"timestamp": timestamp,
|
| 186 |
+
"filename": file.filename or "unknown.png",
|
| 187 |
+
"action": "deleted_non_violation",
|
| 188 |
+
"reason": reason,
|
| 189 |
+
"created_at": datetime.now(timezone.utc),
|
| 190 |
+
}
|
| 191 |
+
)
|
| 192 |
+
return ScreenshotIngestResponse(screenshot_id=screenshot_id)
|
| 193 |
+
|
| 194 |
+
image_url = f"{settings.media_prefix}/{stored_name}"
|
| 195 |
+
logger.info(
|
| 196 |
+
"ingest verdict screenshot_id=%s verdict=%s confidence=%.2f reason=%s urls=%d",
|
| 197 |
+
screenshot_id,
|
| 198 |
+
verdict,
|
| 199 |
+
confidence,
|
| 200 |
+
reason,
|
| 201 |
+
len(ocr_urls),
|
| 202 |
+
)
|
| 203 |
+
await violation_repo.insert(
|
| 204 |
+
{
|
| 205 |
+
"screenshot_id": screenshot_id,
|
| 206 |
+
"device_id": device_id,
|
| 207 |
+
"timestamp": timestamp,
|
| 208 |
+
"filename": file.filename or "unknown.png",
|
| 209 |
+
"created_at": datetime.now(timezone.utc).isoformat(),
|
| 210 |
+
"image_url": image_url,
|
| 211 |
+
"confidence": confidence,
|
| 212 |
+
"reason": reason,
|
| 213 |
+
"absolute_path": absolute_path,
|
| 214 |
+
"visibility_scope": "admin",
|
| 215 |
+
"ocr_urls": ocr_urls,
|
| 216 |
+
"expires_at": datetime.now(timezone.utc) + timedelta(hours=24),
|
| 217 |
+
}
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
if verdict == "uncertain":
|
| 221 |
+
review_id = str(uuid.uuid4())
|
| 222 |
+
await review_repo.create_pending(
|
| 223 |
+
{
|
| 224 |
+
"review_id": review_id,
|
| 225 |
+
"screenshot_id": screenshot_id,
|
| 226 |
+
"device_id": device_id,
|
| 227 |
+
"timestamp": timestamp,
|
| 228 |
+
"status": "pending",
|
| 229 |
+
"reason": reason,
|
| 230 |
+
"ocr_urls": ocr_urls,
|
| 231 |
+
"created_at": datetime.now(timezone.utc),
|
| 232 |
+
}
|
| 233 |
+
)
|
| 234 |
+
await alert_hub.broadcast_admin(
|
| 235 |
+
{
|
| 236 |
+
"type": "game_lock_review_required",
|
| 237 |
+
"device_id": device_id,
|
| 238 |
+
"message": "Model uncertain. Admin confirmation is required before lock.",
|
| 239 |
+
"review_id": review_id,
|
| 240 |
+
"screenshot_id": screenshot_id,
|
| 241 |
+
"ocr_urls": ocr_urls,
|
| 242 |
+
}
|
| 243 |
+
)
|
| 244 |
+
|
| 245 |
+
await _cleanup_expired_violations()
|
| 246 |
+
return ScreenshotIngestResponse(screenshot_id=screenshot_id)
|
| 247 |
+
|
| 248 |
+
|
| 249 |
+
@router.get("/violations", response_model=list[ViolationScreenshot])
|
| 250 |
+
async def list_violations(user: AuthUser = Depends(get_current_web_user)) -> list[ViolationScreenshot]:
|
| 251 |
+
await _cleanup_expired_violations()
|
| 252 |
+
rows = await violation_repo.list_active(user.role)
|
| 253 |
+
return [
|
| 254 |
+
ViolationScreenshot(
|
| 255 |
+
screenshot_id=row.get("screenshot_id", ""),
|
| 256 |
+
device_id=row.get("device_id", ""),
|
| 257 |
+
timestamp=row.get("timestamp", ""),
|
| 258 |
+
filename=row.get("filename", ""),
|
| 259 |
+
created_at=row.get("created_at", ""),
|
| 260 |
+
image_url=row.get("image_url", ""),
|
| 261 |
+
confidence=float(row.get("confidence", 0.0)),
|
| 262 |
+
reason=row.get("reason", ""),
|
| 263 |
+
visibility_scope=row.get("visibility_scope", "admin"),
|
| 264 |
+
ocr_urls=[str(x) for x in row.get("ocr_urls", [])],
|
| 265 |
+
)
|
| 266 |
+
for row in rows
|
| 267 |
+
]
|