File size: 22,142 Bytes
c6abe34 035d434 c6abe34 035d434 c6abe34 035d434 c6abe34 035d434 c6abe34 035d434 c6abe34 | 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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 | """
Video management API endpoints.
"""
import os
import shutil
from uuid import uuid4
from datetime import datetime
from typing import Optional
from urllib.parse import quote
from fastapi import (
APIRouter,
Depends,
HTTPException,
Request,
UploadFile,
File,
Form,
Query,
status,
)
from fastapi.responses import FileResponse
from slowapi import Limiter
from slowapi.util import get_remote_address
from app.config import get_settings
from app.dependencies import get_current_user, get_supabase
from app.models.user import AccountType
from app.models.video import (
VideoUpload,
Video,
VideoStatus,
AnalysisMode,
VideoStatusResponse,
VideoListResponse,
)
from app.services.supabase_client import SupabaseService
router = APIRouter()
def _annotated_video_path(video_id: str) -> str:
# Produced by analysis pipelines (e.g. analysis/team_analysis.py)
return os.path.join("output_videos", "annotated", f"{video_id}.mp4")
def _get_limiter(request: Request) -> Limiter:
return request.app.state.limiter
def get_video_info(file_path: str) -> dict:
"""Extract video metadata using OpenCV."""
try:
import cv2
cap = cv2.VideoCapture(file_path)
except ImportError:
print("⚠️ OpenCV not installed, skipping video metadata extraction")
return {
"fps": 30.0,
"frame_count": 0,
"width": 1920,
"height": 1080,
"duration_seconds": 0,
}
try:
if not cap.isOpened():
return {}
fps = cap.get(cv2.CAP_PROP_FPS)
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
duration = frame_count / fps if fps > 0 else 0
cap.release()
return {
"fps": fps,
"frame_count": frame_count,
"width": width,
"height": height,
"duration_seconds": duration,
}
except Exception:
return {}
finally:
try:
cap.release()
except Exception:
pass
@router.post("/upload", response_model=Video, status_code=status.HTTP_201_CREATED)
async def upload_video(
request: Request,
file: UploadFile = File(...),
title: Optional[str] = Form(None, max_length=200),
description: Optional[str] = Form(None, max_length=1000),
analysis_mode: AnalysisMode = Form(...),
organization_id: Optional[str] = Form(None),
current_user: dict = Depends(get_current_user),
supabase: SupabaseService = Depends(get_supabase),
):
"""
Upload a video for analysis.
- **file**: Video file (mp4, avi, mov, mkv)
- **analysis_mode**: 'team' or 'personal'
- **organization_id**: Required for TEAM analysis
"""
settings = get_settings()
# Apply a modest rate limit to uploads to avoid resource exhaustion.
limiter = _get_limiter(request)
limiter.limit("10/hour")(lambda request: None)(request)
# Validate file extension
if not file.filename:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Missing filename"
)
_, ext = os.path.splitext(file.filename)
ext = ext.lstrip(".").lower()
if ext not in settings.allowed_extensions_list:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Invalid file type. Allowed: {settings.allowed_video_extensions}"
)
# Basic content-type validation (defence-in-depth – still rely on OpenCV check later)
allowed_mime_types = {
"video/mp4",
"video/x-msvideo",
"video/quicktime",
"video/x-matroska",
}
if file.content_type and file.content_type.lower() not in allowed_mime_types:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Invalid content type for video upload",
)
# Validate file size
file.file.seek(0, 2) # Seek to end
file_size = file.file.tell()
file.file.seek(0) # Reset
if file_size > settings.max_upload_size_bytes:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"File too large. Maximum: {settings.max_upload_size_mb}MB"
)
# Validate team analysis requirements
if analysis_mode == AnalysisMode.TEAM:
allowed_types = [AccountType.TEAM.value, AccountType.COACH.value]
if current_user.get("account_type") not in allowed_types:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Team analysis requires a TEAM or COACH account"
)
# Determine the user's organization context
user_org_id = current_user.get("organization_id")
if user_org_id in ("null", "undefined", ""):
user_org_id = None
# If the user is a team manager but organization_id isn't in token (unlikely but safe check)
if not user_org_id and current_user.get("account_type") == AccountType.TEAM.value:
orgs = await supabase.select("organizations", filters={"owner_id": current_user["id"]})
if orgs:
user_org_id = str(orgs[0]["id"])
if organization_id in ("null", "undefined", ""):
organization_id = None
if not organization_id:
organization_id = user_org_id
# Determine if we should allow null organization_id
# We allow it for individual coaches who aren't linked yet.
is_coach = current_user.get("account_type") == AccountType.COACH.value
if not organization_id and not is_coach:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="organization_id is required and you are not linked to any organization"
)
# Verify org access if an organization_id is provided or found
if organization_id and str(organization_id) != str(user_org_id):
# For robustness, if it's a team account, double check org ownership if the ID doesn't match token
if current_user.get("account_type") == AccountType.TEAM.value:
org = await supabase.select_one("organizations", str(organization_id))
if not org or org.get("owner_id") != current_user["id"]:
raise HTTPException(status_code= status.HTTP_403_FORBIDDEN, detail="Access denied to this organization")
else:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="You can only upload to your linked organization")
# Generate unique filename and save. We never trust the original name for paths.
video_id = str(uuid4())
filename = f"{video_id}.{ext}"
# Ensure uploader id does not introduce path traversal
safe_uploader_id = str(current_user["id"]).replace("/", "_").replace("\\", "_")
storage_path = os.path.join(settings.upload_dir, safe_uploader_id, filename)
os.makedirs(os.path.dirname(storage_path), exist_ok=True)
with open(storage_path, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
# Get video metadata
video_info = get_video_info(storage_path)
if not video_info or not video_info.get("frame_count"):
# Reject unreadable / non-video uploads
try:
os.remove(storage_path)
except Exception:
pass
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Uploaded file is not a readable video"
)
# Create database record
video_record = {
"id": video_id,
"uploader_id": current_user["id"],
"title": title or file.filename,
"description": description,
"analysis_mode": analysis_mode.value,
"status": VideoStatus.PENDING.value,
"storage_path": storage_path,
"file_size_bytes": file_size,
"organization_id": organization_id,
**video_info,
}
await supabase.insert("videos", video_record)
return Video(
**video_record,
created_at=datetime.utcnow(),
download_url=f"/api/videos/{video_id}/download",
annotated_download_url=f"/api/videos/{video_id}/annotated",
has_annotated=os.path.exists(_annotated_video_path(video_id)),
)
@router.get("", response_model=VideoListResponse)
async def list_videos(
page: int = Query(1, ge=1),
page_size: int = Query(20, ge=1, le=100),
status_filter: Optional[VideoStatus] = Query(None),
current_user: dict = Depends(get_current_user),
supabase: SupabaseService = Depends(get_supabase),
):
"""
List videos uploaded by the current user.
"""
# Determine filtering strategy based on role and organization link
filters = {}
user_org_id = current_user.get("organization_id")
account_type = current_user.get("account_type")
if user_org_id:
# Linked users see team videos + their own personal uploads
# In Supabase, we can use an OR filter or just fetch all team videos
# For simplicity in this logic, we'll fetch team videos as the primary feed
filters["organization_id"] = user_org_id
elif account_type == AccountType.TEAM.value:
orgs = await supabase.select("organizations", filters={"owner_id": current_user["id"]})
if orgs:
filters["organization_id"] = orgs[0]["id"]
else:
filters["uploader_id"] = current_user["id"]
else:
filters["uploader_id"] = current_user["id"]
if status_filter:
filters["status"] = status_filter.value
videos = await supabase.select(
"videos",
filters=filters,
order_by="created_at",
ascending=False,
)
# Paginate
total = len(videos)
start = (page - 1) * page_size
end = start + page_size
paginated = videos[start:end]
return VideoListResponse(
videos=[
Video(
**{
**v,
"download_url": f"/api/videos/{v.get('id')}/download",
"annotated_download_url": f"/api/videos/{v.get('id')}/annotated",
"has_annotated": bool(v.get("annotated_url")) or os.path.exists(_annotated_video_path(str(v.get('id')))),
}
)
for v in paginated
],
total=total,
page=page,
page_size=page_size,
)
@router.get("/{video_id}", response_model=Video)
async def get_video(
video_id: str,
current_user: dict = Depends(get_current_user),
supabase: SupabaseService = Depends(get_supabase),
):
"""
Get video details by ID.
"""
video = await supabase.select_one("videos", video_id)
if not video:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Video not found"
)
# Check ownership or organization access
is_owner = str(video["uploader_id"]) == str(current_user["id"])
is_org_member = False
if video.get("organization_id") and current_user.get("organization_id"):
if str(video["organization_id"]) == str(current_user["organization_id"]):
is_org_member = True
if not is_owner and not is_org_member:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You don't have access to this video"
)
return Video(
**{
**video,
"download_url": f"/api/videos/{video_id}/download",
"annotated_download_url": f"/api/videos/{video_id}/annotated",
"has_annotated": bool(video.get("annotated_url")) or os.path.exists(_annotated_video_path(video_id)),
}
)
@router.get("/{video_id}/download")
async def download_video(
video_id: str,
current_user: dict = Depends(get_current_user),
supabase: SupabaseService = Depends(get_supabase),
):
"""
Download a previously uploaded video.
Authenticated and ownership-checked.
"""
video = await supabase.select_one("videos", video_id)
if not video:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Video not found")
# Check ownership or organization access
is_owner = str(video.get("uploader_id")) == str(current_user["id"])
is_org_member = False
if video.get("organization_id") and current_user.get("organization_id"):
if str(video["organization_id"]) == str(current_user["organization_id"]):
is_org_member = True
if not is_owner and not is_org_member:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="You don't have access to this video")
storage_path = video.get("storage_path")
if not storage_path or not os.path.exists(storage_path):
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Video file not found on server")
# Prefer the original filename for download if available
original_title = (video.get("title") or f"{video_id}").strip()
safe_name = quote(original_title.replace("/", "_").replace("\\", "_"))
_, ext = os.path.splitext(storage_path)
ext = ext if ext else ".mp4"
# Guess media type based on extension
media_types = {
".mp4": "video/mp4",
".webm": "video/webm",
".avi": "video/x-msvideo",
".mov": "video/quicktime",
".mkv": "video/x-matroska",
}
media_type = media_types.get(ext.lower(), "video/mp4")
return FileResponse(
path=storage_path,
filename=f"{safe_name}{ext}",
media_type=media_type,
content_disposition_type="inline"
)
@router.get("/{video_id}/annotated")
async def download_annotated_video(
video_id: str,
current_user: dict = Depends(get_current_user),
supabase: SupabaseService = Depends(get_supabase),
):
"""
Download the annotated output video (if available).
Redirects to Supabase signed URL when stored in cloud, otherwise streams from disk.
Ownership-checked via the uploaded video record.
"""
from fastapi.responses import RedirectResponse
video = await supabase.select_one("videos", video_id)
if not video:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Video not found")
# Check ownership or organization access
is_owner = str(video.get("uploader_id")) == str(current_user["id"])
is_org_member = False
if video.get("organization_id") and current_user.get("organization_id"):
if str(video["organization_id"]) == str(current_user["organization_id"]):
is_org_member = True
if not is_owner and not is_org_member:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="You don't have access to this video")
# Prefer Supabase signed URL (cloud storage) — redirect the client directly
annotated_url = video.get("annotated_url")
if annotated_url and annotated_url.startswith("https"):
return RedirectResponse(url=annotated_url, status_code=302)
# Fallback: serve from local disk
annotated_path = _annotated_video_path(video_id)
if not os.path.exists(annotated_path):
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Annotated video not available yet")
original_title = (video.get("title") or f"{video_id}").strip()
safe_name = quote(original_title.replace("/", "_").replace("\\", "_"))
return FileResponse(
path=annotated_path,
filename=f"{safe_name}-annotated.mp4",
media_type="video/mp4",
content_disposition_type="inline"
)
@router.get("/{video_id}/status", response_model=VideoStatusResponse)
async def get_video_status(
video_id: str,
current_user: dict = Depends(get_current_user),
supabase: SupabaseService = Depends(get_supabase),
):
"""
Get video processing status with robust error handling for network issues.
"""
try:
video = await supabase.select_one("videos", video_id)
if not video:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Video not found"
)
# Security: Only owner or team members can see status
is_owner = str(video["uploader_id"]) == str(current_user["id"])
# Check org access if uploader is different
is_org_member = False
if not is_owner and video.get("organization_id") and current_user.get("organization_id"):
if str(video["organization_id"]) == str(current_user["organization_id"]):
is_org_member = True
if not is_owner and not is_org_member:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You don't have access to this video status"
)
# HEAL: If status is failed but an analysis result was actually written,
# then the process likely finished but the final status update was interrupted.
if video["status"] == VideoStatus.FAILED.value:
try:
results = await supabase.select("analysis_results", filters={"video_id": video_id}, limit=1)
if results:
video["status"] = VideoStatus.COMPLETED.value
video["progress_percent"] = 100
video["current_step"] = "Complete (Auto-Recovered)"
# Apply heal to DB so it doesn't repeat
await supabase.update("videos", video_id, {
"status": VideoStatus.COMPLETED.value,
"progress_percent": 100,
"current_step": "Complete (Auto-Recovered)"
})
except Exception as e:
print(f"⚠️ Auto-heal check failed for video {video_id}: {e}")
return VideoStatusResponse(
id=video["id"],
status=VideoStatus(video["status"]),
progress_percent=video.get("progress_percent") or 0,
current_step=video.get("current_step") or "Initializing",
error_message=video.get("error_message"),
)
except HTTPException:
raise
except Exception as e:
print(f"❌ Error fetching video status for {video_id}: {e}")
# Return 503 instead of 500 for network/database connection errors
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Temporary database connection error. Please try again in a few moments."
)
@router.delete("/{video_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_video(
video_id: str,
current_user: dict = Depends(get_current_user),
supabase: SupabaseService = Depends(get_supabase),
):
"""
Delete a video and associated data.
Allows uploader, team owner, or coach (if in same org) to delete.
"""
video = await supabase.select_one("videos", video_id)
if not video:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Video not found"
)
# Permission Check: Uploader can always delete
is_owner = str(video["uploader_id"]) == str(current_user["id"])
# Organization Check: Team Owner or Coach can delete team videos
is_org_authorized = False
if video.get("organization_id") and current_user.get("organization_id"):
if str(video["organization_id"]) == str(current_user["organization_id"]):
if current_user.get("account_type") in [AccountType.TEAM.value, AccountType.COACH.value]:
is_org_authorized = True
if not is_owner and not is_org_authorized:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You don't have permission to delete this video"
)
# 1. Delete original file
if video.get("storage_path") and os.path.exists(video["storage_path"]):
try:
os.remove(video["storage_path"])
except Exception as e:
print(f"Error removing original video: {e}")
# 2. Delete annotated video (if any)
annotated_path = _annotated_video_path(video_id)
if os.path.exists(annotated_path):
try:
os.remove(annotated_path)
except Exception as e:
print(f"Error removing annotated video: {e}")
# 3. Clean up personal analysis outputs (if any)
# Personal analysis uses a different naming convention: {video_id}_output.mp4 etc.
from app.api.personal_analysis import PERSONAL_OUTPUT_DIR
for suffix in ["_output.mp4", "_output.avi", "_report.txt", "_input.mp4", "_input.avi"]:
personal_path = os.path.join(PERSONAL_OUTPUT_DIR, f"{video_id}{suffix}")
if os.path.exists(personal_path):
try:
os.remove(personal_path)
except Exception:
pass
# 4. Best-effort cascade delete related rows
for table in ["analysis_results", "detections", "analytics", "personal_analyses", "clips"]:
try:
await supabase.delete_where(table, {"video_id": video_id})
except Exception:
# Fallback for tables that might use different columns or not exist
try:
if table == "personal_analyses":
await supabase.delete_where(table, {"job_id": video_id})
elif table == "clips":
# Advanced analytics clips usually linked via video_id
await supabase.delete_where(table, {"video_id": video_id})
except Exception:
pass
# 5. Delete database record
await supabase.delete("videos", video_id)
return None
|