fix(drivers/onedrive_sharelink): respect root_folder_path for subdirectory access - #2840
fix(drivers/onedrive_sharelink): respect root_folder_path for subdirectory access#2840xireiki wants to merge 4 commits into
Conversation
…cess - Add relativePath() to strip RootFolderPath prefix from virtual paths - Add effectiveDriveRootPath() to compute drive-relative path from RootFolderPath - Override rootFolder in getFiles() when RootFolderPath is configured - Apply relativePath() in List, MakeDir, Put, GetDirectUploadInfo - Store listURL for path computation against document library root Co-authored-by: GitHub Copilot <copilot@github.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes path handling in the onedrive_sharelink driver so that when root_folder_path is configured, listing/creating/uploading targets the configured subdirectory correctly (and avoids double path-prefixing that could lead to 404s).
Changes:
- Add
relativePath()to translate OpenList virtual paths into paths relative to the share root, and use it acrossList,MakeDir,Put,GetDirectUploadInfo. - Adjust Graph API path building via
effectiveDriveRootPath()whenRootFolderPathis set. - Update
getFiles()to treatRootFolderPathas the initial root folder for the GraphQL query.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| drivers/onedrive_sharelink/driver.go | Adds root-relative path conversion and adjusts drive API base-path computation for root_folder_path. |
| drivers/onedrive_sharelink/util.go | Updates GraphQL listing logic to anchor queries at root_folder_path. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
PIKACHUIM
left a comment
There was a problem hiding this comment.
🙏 感谢贡献
感谢 @xireiki 提交此PR!
🤖 AI评审声明:本报告由AI自动生成(Claude Opus 5),仅供参考,可能存在误判。最终合并决策由项目维护者综合判定。
📖 PR概要
标题:fix(drivers/onedrive_sharelink): respect root_folder_path for subdirectory access | 关联:无关联Issue | 目标:修复 onedrive_sharelink 驱动在配置 root_folder_path 后无法正确访问子目录的问题
核心改动:新增路径转换方法 relativePath() 和 effectiveDriveRootPath(),在 List、MakeDir、Put、GetDirectUploadInfo 等操作中统一使用相对路径转换,并在 getFiles() 中覆盖 rootFolder 以确保 GraphQL 查询指向正确的子目录。
📋 评审发现
总体评分:功能性 ⭐⭐⭐⭐ | 安全性 ⭐⭐⭐⭐ | 代码质量 ⭐⭐⭐ | 实现方案 ⭐⭐⭐
关键问题:
⚠️ 路径处理逻辑:relativePath()的路径前缀匹配可能在边界情况下产生误判(如/foo和/foobar)⚠️ 代码重复:多处检查RootFolderPath == "" || RootFolderPath == "/",存在重复逻辑⚠️ 格式不一致:Init()中新增的验证逻辑使用了空格缩进,与文件其他部分(使用制表符)不一致- 💡 测试覆盖:仅提供了手动测试,缺少单元测试覆盖路径转换的边界情况
📂 主要文件分析
drivers/onedrive_sharelink/driver.go
改动:新增字段 listURL;添加 relativePath() 和 effectiveDriveRootPath() 方法;在 Init() 中验证 RootFolderPath 格式;在 List、MakeDir、Put、GetDirectUploadInfo 中应用路径转换;在 drivePathAPIURL() 中使用 effectiveDriveRootPath()。
问题与建议:
-
⚠️ driver.go:103-115(relativePath方法) - 路径前缀匹配逻辑可更严谨- 问题:当前
strings.HasPrefix(vpath+"/", root+"/")依赖FixAndCleanPath后的格式保证,逻辑上正确但可读性一般,且当vpath == root时已经在前面return,这导致下面的HasPrefix分支显得多余 - 建议:合并
vpath == root判断,让代码更紧凑:
func (d *OnedriveSharelink) relativePath(virtualPath string) string { if d.RootFolderPath == "" || d.RootFolderPath == "/" { return virtualPath } root := utils.FixAndCleanPath(d.RootFolderPath) vpath := utils.FixAndCleanPath(virtualPath) if vpath == root { return "/" } // 确保 root 之后是路径分隔符,避免 /foo 匹配 /foobar if len(vpath) > len(root) && strings.HasPrefix(vpath, root) && vpath[len(root)] == '/' { return utils.FixAndCleanPath(vpath[len(root):]) } log.Warnf("onedrive_sharelink: path %q is outside configured root %q", virtualPath, d.RootFolderPath) return virtualPath }
- 问题:当前
-
⚠️ driver.go:85-90- 缩进格式不一致- 问题:此处使用 4 空格缩进,而文件其他部分使用 tab 缩进。
gofmt会直接修改此处 - 建议:运行
gofmt -w drivers/onedrive_sharelink/driver.go统一格式化。PR 已在 Checklist 中勾选gofmt,但实际并未格式化
- 问题:此处使用 4 空格缩进,而文件其他部分使用 tab 缩进。
-
⚠️ driver.go:472-475-drivePathAPIURL中存在重复计算- 问题:先
drivePath := stdpath.Join(d.driveRootPath, path),紧接在条件分支中又重新计算drivePath = stdpath.Join(d.effectiveDriveRootPath(), path),第一次计算完全被丢弃 - 建议:先选 base,再计算一次,避免重复:
func (d *OnedriveSharelink) drivePathAPIURL(path string) string { base := d.driveRootPath if d.RootFolderPath != "" && d.RootFolderPath != "/" { base = d.effectiveDriveRootPath() } drivePath := utils.FixAndCleanPath(stdpath.Join(base, path)) if drivePath == "/" { return d.DriveURL + "/root" } return fmt.Sprintf("%s/root:%s:", d.DriveURL, utils.EncodePath(drivePath, true)) }
- 问题:先
-
⚠️ driver.go:486-501(effectiveDriveRootPath) - 与relativePath存在逻辑重复- 问题:两个方法都做了
FixAndCleanPath、HasPrefix+ 路径分隔符检查、Warnf后回退。差异仅在返回的是 root 还是 list 的剩余部分 - 建议:考虑抽取共用 helper
stripPrefix(fullPath, prefix string) (string, bool),由两个方法复用
- 问题:两个方法都做了
-
✅ 做得好的地方:
Init()中路径验证逻辑 fail-fast 设计良好,能在驱动初始化阶段即时发现配置错误relativePath()和effectiveDriveRootPath()职责分离清晰,分别处理虚拟路径和 drive API 路径- 详细的注释说明了
_relativePath/listURL/RootFolderPath之间的关系,对后续维护者非常友好 - 三个 commit 分工明确:第一处修复主功能,第二处合并上游,第三处对路径匹配的额外加固
drivers/onedrive_sharelink/util.go
改动:在 getFiles() 中,若用户配置了 RootFolderPath 则覆盖初始计算的 rootFolder。
问题与建议:
-
⚠️ util.go:261-265- 路径覆盖缺乏前置校验- 问题:先从 redirect 中提取
rootFolder,然后无脑用d.RootFolderPath覆盖。如果RootFolderPath格式(如是否包含Documents段)不符合下方strings.Split(rootFolder, "Documents")的假设,下游会出错 - 建议:要么先调用
utils.FixAndCleanPath(d.RootFolderPath)再覆盖,要么在Init()校验时就把Documents段存在性当作硬约束
- 问题:先从 redirect 中提取
-
💡
util.go:262-266- 硬编码 "Documents" 分割对本地化场景脆弱- 问题:
strings.Split(rootFolder, "Documents")[0] + "Documents"假设路径中必含Documents,但 OneDrive 中文/日文账户的文档库根段名可能是文档/ドキュメント等 - 建议:在文档中明确该驱动仅支持英文版 OneDrive,或用户配置 RootFolderPath 时需要自行带上
Documents段
- 问题:
-
✅ 做得好的地方:
- 修改范围小、影响面窄,仅 5 行新增
log.Debugln("rootFolder:", rootFolder)仍保留,便于排查
🎯 结论
建议操作:🔄 Request Changes
理由:PR 解决了一个真实问题,核心思路正确,但存在(1)gofmt 漏跑导致的格式不一致,(2)drivePathAPIURL 重复计算,(3)路径匹配逻辑可进一步加固。建议作者修复以上三点并补充 relativePath / effectiveDriveRootPath 的单元测试后再合并。需要说明的是:这些都不是阻断性问题,逻辑主流程是工作的,作者只需要稍微打磨一下即可。
…ndling - Merge vpath==root into a shared stripPrefix helper that matches on the path separator, so /foo no longer matches /foobar - Reuse stripPrefix in effectiveDriveRootPath, removing duplicated FixAndCleanPath/prefix/warning logic - Select the base drive root once in drivePathAPIURL instead of a discarded first Join - Enforce absolute and \"/Documents\"-segment requirements for root_folder_path in Init, and normalize the override in getFiles - Add unit tests for relativePath and effectiveDriveRootPath boundary cases
Summary / 摘要
修复 onedrive_sharelink 驱动在配置
root_folder_path后无法正确访问子目录的问题。用户可感知的变化:
实现变化:
driver.go:新增relativePath()、effectiveDriveRootPath()方法;List、MakeDir、Put、GetDirectUploadInfo统一使用relativePath()转换路径;drivePathAPIURL()在配置RootFolderPath时改用effectiveDriveRootPath()作为基准util.go:getFiles()在计算出初始 rootFolder 后,若用户配置了RootFolderPath则覆盖之This PR has breaking changes.
/ 此 PR 包含破坏性变更。
This PR changes public API, config, storage format, or migration behavior.
/ 此 PR 修改了公开 API、配置、存储格式或迁移行为。
This PR requires corresponding changes in related repositories.
/ 此 PR 需要关联仓库同步修改。
Related repository PRs / 关联仓库 PR:
Related Issues / 关联 Issue
Testing / 测试
go vet ./drivers/onedrive_sharelink/...go build ./drivers/onedrive_sharelink/...Checklist / 检查清单
/ 我已阅读 CONTRIBUTING。
/ 我确认此贡献符合仓库许可证、贡献规范和行为准则。
gofmt,go fmt, orprettierwhere applicable./ 我已按适用情况使用
gofmt、go fmt或prettier格式化变更代码。/ 我已在适用情况下请求相关维护者或代码所有者审查。
AI Disclosure / AI 使用声明
/ 此 PR 包含 AI 辅助内容。
Tools used / 使用工具:
Usage scope / 使用范围:
Code generation / 代码生成
Refactoring / 重构
Documentation / 文档
Tests / 测试
Translation / 翻译
Review assistance / 审查辅助
I have reviewed and validated all AI-assisted content included in this PR.
/ 我已审核并验证此 PR 中的所有 AI 辅助内容。
I have ensured that all AI-assisted commits include
Co-Authored-Byattribution./ 我已确保所有 AI 辅助提交都包含
Co-Authored-By归属信息。I can reproduce all AI-assisted content included in this PR without any AI tools.
/ 我可以在没有任何 AI 工具的情况下重现此 PR 中包含的所有 AI 辅助内容。