2026-02-09 12:55:12 +08:00
|
|
|
"""Shared path resolution for thread virtual paths (e.g. mnt/user-data/outputs/...)."""
|
|
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
from fastapi import HTTPException
|
|
|
|
|
|
2026-02-25 21:30:33 +08:00
|
|
|
from src.config.paths import get_paths
|
2026-02-09 12:55:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def resolve_thread_virtual_path(thread_id: str, virtual_path: str) -> Path:
|
|
|
|
|
"""Resolve a virtual path to the actual filesystem path under thread user-data.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
thread_id: The thread ID.
|
2026-02-25 21:30:33 +08:00
|
|
|
virtual_path: The virtual path as seen inside the sandbox
|
|
|
|
|
(e.g., /mnt/user-data/outputs/file.txt).
|
2026-02-09 12:55:12 +08:00
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
The resolved filesystem path.
|
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
HTTPException: If the path is invalid or outside allowed directories.
|
|
|
|
|
"""
|
|
|
|
|
try:
|
2026-02-25 21:30:33 +08:00
|
|
|
return get_paths().resolve_virtual_path(thread_id, virtual_path)
|
|
|
|
|
except ValueError as e:
|
|
|
|
|
status = 403 if "traversal" in str(e) else 400
|
|
|
|
|
raise HTTPException(status_code=status, detail=str(e))
|