From 22ee68d0057e9577baf7fe6908ed495e60f96bf3 Mon Sep 17 00:00:00 2001 From: puke <1129090915@qq.com> Date: Sat, 6 Dec 2025 12:59:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=A7=86=E9=A2=91=E7=94=9F?= =?UTF-8?q?=E6=88=90=20API=20=E8=BF=94=E5=9B=9E=20URL=20=E7=9A=84=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E5=A4=84=E7=90=86=EF=BC=8C=E6=94=AF=E6=8C=81=E7=BB=9D?= =?UTF-8?q?=E5=AF=B9=E8=B7=AF=E5=BE=84=E8=BD=AC=E6=8D=A2=E5=92=8C=E8=B7=A8?= =?UTF-8?q?=E5=B9=B3=E5=8F=B0=E5=85=BC=E5=AE=B9=EF=BC=8C=E7=A1=AE=E4=BF=9D?= =?UTF-8?q?=20URL=20host=20=E5=8A=A8=E6=80=81=E5=8C=B9=E9=85=8D=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E5=9F=9F=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TEST_BATCH_VIDEO.md | 0 api/routers/video.py | 54 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 TEST_BATCH_VIDEO.md diff --git a/TEST_BATCH_VIDEO.md b/TEST_BATCH_VIDEO.md new file mode 100644 index 0000000..e69de29 diff --git a/api/routers/video.py b/api/routers/video.py index 9f09ccf..952d1b5 100644 --- a/api/routers/video.py +++ b/api/routers/video.py @@ -32,11 +32,55 @@ router = APIRouter(prefix="/video", tags=["Video Generation"]) def path_to_url(request: Request, file_path: str) -> str: - """Convert file path to accessible URL""" - # file_path is like "output/abc123.mp4" - # Remove "output/" prefix for cleaner URL - if file_path.startswith("output/"): - file_path = file_path[7:] # Remove "output/" + """ + Convert file path to accessible URL + + Handles both absolute and relative paths, extracting the path relative + to the output directory for URL construction. + + Args: + request: FastAPI Request object (provides base_url from actual request) + file_path: Absolute or relative file path + + Returns: + Full URL to access the file + + Examples: + Windows: G:\\...\\output\\20251205_233630_c939\\final.mp4 + -> http://localhost:8000/api/files/20251205_233630_c939/final.mp4 + + Linux: /home/user/.../output/20251205_233630_c939/final.mp4 + -> http://localhost:8000/api/files/20251205_233630_c939/final.mp4 + + Domain: With domain request -> https://your-domain.com/api/files/... + """ + from pathlib import Path + import os + + # Normalize path separators to forward slashes first (for cross-platform compatibility) + file_path = file_path.replace("\\", "/") + + # Check if it's an absolute path (works for both Windows and Linux) + is_absolute = os.path.isabs(file_path) or Path(file_path).is_absolute() + + if is_absolute: + # Find "output" in the path and get everything after it + # Split by / to work with normalized paths + parts = file_path.split("/") + try: + output_idx = parts.index("output") + # Get all parts after "output" and join them + relative_parts = parts[output_idx + 1:] + file_path = "/".join(relative_parts) + except ValueError: + # If "output" not in path, use the filename only + file_path = Path(file_path).name + else: + # If relative path starting with "output/", remove it + if file_path.startswith("output/"): + file_path = file_path[7:] # Remove "output/" + + # Build URL using request's base_url (automatically matches the request host) base_url = str(request.base_url).rstrip('/') return f"{base_url}/api/files/{file_path}"