Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/qwenpaw/app/routers/files.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
from pathlib import Path
from urllib.parse import unquote
from fastapi import APIRouter, HTTPException
from starlette.responses import FileResponse

Expand All @@ -15,9 +16,30 @@ async def preview_file(
filepath: str,
):
"""Preview file."""
path = Path(filepath)
normalized = unquote(filepath)

# Tolerate duplicated preview prefix from some clients, e.g.
# /api/files/preview/api/files/preview/C%3A/Users/...
while True:
trimmed = normalized.lstrip("/")
prefix = "api/files/preview/"
if trimmed.startswith(prefix):
normalized = trimmed[len(prefix) :]
continue
break

# Normalize /C:/... to C:/... on Windows.
if (
len(normalized) >= 4
and normalized[0] == "/"
and normalized[2] == ":"
and normalized[1].isalpha()
):
normalized = normalized[1:]

path = Path(normalized)
if not path.is_absolute():
path = Path("/" + filepath)
path = Path("/" + normalized)
path = path.resolve()
Comment on lines +19 to 43
if not path.is_file():
Comment on lines +19 to 44
raise HTTPException(status_code=404, detail="Not found")
Expand Down
Loading