Files
video-flow/modules/legacy_path_mapper.py
2026-01-09 14:09:16 +08:00

83 lines
2.6 KiB
Python

"""
Legacy path mapper for assets generated by the 8502 runtime (/root/video-flow).
Problem:
- Postgres `scene_assets.local_path` may contain paths like `/root/video-flow/temp/...`
which are not visible inside docker containers running 8503 stack.
Solution:
- Mount host directories into containers (e.g. /legacy/temp, /legacy/output)
- Map legacy host paths -> container paths, and produce static URLs accordingly.
"""
from __future__ import annotations
import os
from pathlib import Path
from typing import Optional, Tuple
LEGACY_HOST_TEMP_PREFIX = "/root/video-flow/temp/"
LEGACY_HOST_OUTPUT_PREFIX = "/root/video-flow/output/"
LEGACY_HOST_PREFIX = "/root/video-flow/"
# Container mount points (see docker-compose.yml)
LEGACY_CONTAINER_TEMP_DIR = "/legacy/temp"
LEGACY_CONTAINER_OUTPUT_DIR = "/legacy/output"
LEGACY_STATIC_TEMP_PREFIX = "/static/legacy-temp/"
LEGACY_STATIC_OUTPUT_PREFIX = "/static/legacy-output/"
def map_legacy_local_path(local_path: Optional[str]) -> Tuple[Optional[str], Optional[str]]:
"""
Returns: (container_visible_path, static_url)
- If local_path exists as-is, returns (local_path, None)
- If it's a legacy host path, rewrite to container mount and provide URL
- If unknown, returns (local_path, None)
"""
if not local_path:
return None, None
# If container can see it already, keep
if os.path.exists(local_path):
return local_path, None
# Legacy host path -> current container workspace path (same repo but different prefix)
# Example:
# /root/video-flow/temp/projects/... -> /app/temp/projects/...
# This covers cases where we don't mount /legacy/* but the files were copied into current stack.
if local_path.startswith(LEGACY_HOST_PREFIX):
rest = local_path[len(LEGACY_HOST_PREFIX):].lstrip("/")
candidate = str(Path("/app") / rest)
if os.path.exists(candidate):
return candidate, None
# Legacy host -> container mapping (preserve relative path)
if local_path.startswith(LEGACY_HOST_TEMP_PREFIX):
rel = local_path[len(LEGACY_HOST_TEMP_PREFIX):].lstrip("/")
container_path = str(Path(LEGACY_CONTAINER_TEMP_DIR) / rel)
# 静态路由通常只覆盖目录根(不包含子目录);这里交给 /api/assets/file 做 FileResponse 更稳
return container_path, None
if local_path.startswith(LEGACY_HOST_OUTPUT_PREFIX):
rel = local_path[len(LEGACY_HOST_OUTPUT_PREFIX):].lstrip("/")
container_path = str(Path(LEGACY_CONTAINER_OUTPUT_DIR) / rel)
return container_path, None
# Unknown path: keep as-is
return local_path, None