MCPcopy Create free account
hub / github.com/53AI/53AIHub / GetFileWithParentsByID

Function GetFileWithParentsByID

api/model/file.go:1734–1772  ·  view source on GitHub ↗

GetFileWithParentsByID 根据文件ID查询文件及其所有父级文件 返回当前文件对象和按深度倒序排列的文件列表(包含当前文件) 示例:对于路径 /1/2/3/file.md,返回顺序为 [/1/2/3/file.md, /1/2/3, /1/2, /1]

(eid int64, fileID int64)

Source from the content-addressed store, hash-verified

1732// 返回当前文件对象和按深度倒序排列的文件列表(包含当前文件)
1733// 示例:对于路径 /1/2/3/file.md,返回顺序为 [/1/2/3/file.md, /1/2/3, /1/2, /1]
1734func GetFileWithParentsByID(eid int64, fileID int64) (*File, []File, error) {
1735 // 1. 查询当前文件
1736 currentFile, err := GetFileByID(eid, fileID)
1737 if err != nil {
1738 return nil, nil, err
1739 }
1740
1741 // 2. 拆解路径获取所有父级路径
1742 parentPaths := splitPathLevels(currentFile.Path)
1743
1744 // 3. 批量查询所有父级文件,避免逐层 N+1 查询
1745 parentFiles, err := GetFilesByPathsAndLibrary(eid, currentFile.LibraryID, parentPaths)
1746 if err != nil {
1747 return nil, nil, err
1748 }
1749
1750 parentFileMap := make(map[string]File, len(parentFiles))
1751 for _, parentFile := range parentFiles {
1752 parentFileMap[parentFile.Path] = parentFile
1753 }
1754
1755 // 4. 构建结果列表(深度倒序:当前文件 -> 最深父级 -> ... -> 根级)
1756 var result []File
1757 result = append(result, *currentFile) // 先添加当前文件
1758
1759 orderedParents := make([]File, 0, len(parentPaths))
1760 for _, parentPath := range parentPaths {
1761 if parentFile, ok := parentFileMap[parentPath]; ok {
1762 orderedParents = append(orderedParents, parentFile)
1763 }
1764 }
1765
1766 // 倒序添加父级文件(从深到浅)
1767 for i := len(orderedParents) - 1; i >= 0; i-- {
1768 result = append(result, orderedParents[i])
1769 }
1770
1771 return currentFile, result, nil
1772}
1773
1774// splitPathLevels 拆解文件路径为所有父级目录路径
1775// 示例:/1/2/3/file.md -> ["/1", "/1/2", "/1/2/3"]

Callers

nothing calls this directly

Calls 3

GetFileByIDFunction · 0.85
splitPathLevelsFunction · 0.85

Tested by

no test coverage detected