fix(file-manager): preserve project/prompt prefix in nested directories
修复文件管理 API 中路径计算问题。当点击第三级目录时,由于 PROJECTS_DIR 是相对路径而 item 是绝对路径,导致 relative_to() 失败返回绝对路径,从而使前端丢失前缀。 使用 PROJECTS_DIR.resolve() 作为基准确保正确计算相对路径。 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
e00f99c5c5
commit
c808517f02
@ -107,21 +107,27 @@ async def list_files(path: str = "", recursive: bool = False):
|
|||||||
|
|
||||||
def scan_directory(directory: Path, base_path: Path = PROJECTS_DIR) -> List[Dict[str, Any]]:
|
def scan_directory(directory: Path, base_path: Path = PROJECTS_DIR) -> List[Dict[str, Any]]:
|
||||||
items = []
|
items = []
|
||||||
|
# 使用解析后的绝对路径作为基准
|
||||||
|
base_resolved = PROJECTS_DIR.resolve()
|
||||||
try:
|
try:
|
||||||
for item in directory.iterdir():
|
for item in directory.iterdir():
|
||||||
# 跳过隐藏文件
|
# 跳过隐藏文件
|
||||||
if item.name.startswith('.'):
|
if item.name.startswith('.'):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# 计算相对于项目根目录的相对路径
|
# 计算相对于项目根目录的相对路径
|
||||||
try:
|
try:
|
||||||
relative_path = item.relative_to(PROJECTS_DIR)
|
relative_path = item.relative_to(base_resolved)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
# 如果无法计算相对路径,使用绝对路径
|
# 如果无法计算相对路径,尝试计算相对于当前目录的路径
|
||||||
relative_path = item
|
try:
|
||||||
|
relative_path = item.relative_to(directory.resolve().parent)
|
||||||
|
except ValueError:
|
||||||
|
# 最后尝试直接使用相对路径
|
||||||
|
relative_path = item
|
||||||
|
|
||||||
stat = item.stat()
|
stat = item.stat()
|
||||||
|
|
||||||
item_info = {
|
item_info = {
|
||||||
"name": item.name,
|
"name": item.name,
|
||||||
"path": str(relative_path),
|
"path": str(relative_path),
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user