Skip to content

fix(console): normalize duplicated preview URL prefixes#3355

Open
Prince-liu wants to merge 1 commit intoagentscope-ai:mainfrom
Prince-liu:fix/preview-path-normalization
Open

fix(console): normalize duplicated preview URL prefixes#3355
Prince-liu wants to merge 1 commit intoagentscope-ai:mainfrom
Prince-liu:fix/preview-path-normalization

Conversation

@Prince-liu
Copy link
Copy Markdown

Summary

  • Normalize filepath in file preview API to tolerate duplicated api/files/preview/ prefixes.
  • Decode URL-encoded paths before resolving local files.
  • Normalize Windows-style /C:/... paths to C:/... for robust file lookup.

Why

Some messages/clients may already contain a preview URL path (/api/files/preview/...). If another layer prepends the same prefix again, backend currently resolves an invalid filesystem path and returns 404.

Reproduction

  • Generate an image so local file exists.
  • Open preview with duplicated path:
    • /api/files/preview/api/files/preview/C%3A/Users/.../image.jpg
  • Current behavior: 404 Not Found.

Verification

  • Normal path still works:
    • /api/files/preview/C%3A/Users/.../image.jpg
  • Duplicated-prefix path now resolves correctly when file exists.

Scope

  • Backward-compatible server-side normalization only.
  • No change to successful normal-path behavior.

Copilot AI review requested due to automatic review settings April 14, 2026 04:20
@github-project-automation github-project-automation bot moved this to Todo in QwenPaw Apr 14, 2026
@github-actions github-actions bot added the first-time-contributor PR created by a first time contributor label Apr 14, 2026
@github-actions
Copy link
Copy Markdown

Welcome to QwenPaw! 🐾

Hi @Prince-liu, thank you for your first Pull Request! 🎉

📋 About PR Template

To help maintainers review your PR faster, please make sure to include:

  • Description - What this PR does and why
  • Type of Change - Bug fix / Feature / Breaking change / Documentation / Refactoring
  • Component(s) Affected - Core / Console / Channels / Skills / CLI / Documentation / Tests / CI/CD / Scripts
  • Checklist:
    • Run and pass pre-commit run --all-files
    • Run and pass relevant tests (pytest or as applicable)
    • Update documentation if needed
  • Testing - How to test these changes
  • Local Verification Evidence:
    pre-commit run --all-files
    # paste summary result
    
    pytest
    # paste summary result

Complete PR information helps speed up the review process. You can edit the PR description to add these details.

🙌 Join Developer Community

Thanks so much for your contribution! We'd love to invite you to join the official QwenPaw developer group! You can find the Discord and DingTalk group links under the "Developer Community" section on our docs page:
https://qwenpaw.agentscope.io/docs/community

We truly appreciate your enthusiasm—and look forward to your future contributions! 😊

We'll review your PR soon.

@Prince-liu
Copy link
Copy Markdown
Author

Repro on my local Windows setup (QwenPaw 1.1.1b1):

  1. Generate an image file:
    C:\Users\princeliu.copaw\workspaces\default\minimax-output\beta-test_001.jpg

  2. Open preview with duplicated prefix path:
    /api/files/preview/api/files/preview/C%3A/Users/princeliu/.copaw/workspaces/default/minimax-output/beta-test_001.jpg

Before patch:

  • Returns 404 Not Found

After patch:

  • Resolves to target local file and returns FileResponse successfully

Also verified normal path behavior remains unchanged.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the file preview endpoint to be more tolerant of malformed/duplicated preview URLs by normalizing the incoming filepath before resolving it to a local file on disk.

Changes:

  • URL-decodes the filepath parameter before filesystem resolution.
  • Strips duplicated api/files/preview/ prefixes from the path (to tolerate clients that re-prepend the preview route).
  • Normalizes Windows-style /C:/... into C:/... to improve cross-platform file lookup.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +19 to 44
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()
if not path.is_file():
Comment on lines +19 to 43
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

first-time-contributor PR created by a first time contributor Under Review

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

3 participants