Skip to content

fix(drivers/onedrive_sharelink): respect root_folder_path for subdirectory access - #2840

Open
xireiki wants to merge 4 commits into
OpenListTeam:mainfrom
xireiki:main-fixosl
Open

fix(drivers/onedrive_sharelink): respect root_folder_path for subdirectory access#2840
xireiki wants to merge 4 commits into
OpenListTeam:mainfrom
xireiki:main-fixosl

Conversation

@xireiki

@xireiki xireiki commented Jul 26, 2026

Copy link
Copy Markdown

Summary / 摘要

修复 onedrive_sharelink 驱动在配置 root_folder_path 后无法正确访问子目录的问题。

用户可感知的变化:

  • 设置「根文件夹路径」后,挂载点将正确指向该子目录,而非分享链接的根目录
  • 文件夹大小统计不再因路径双重拼接返回 404

实现变化:

  • driver.go:新增 relativePath()effectiveDriveRootPath() 方法;ListMakeDirPutGetDirectUploadInfo 统一使用 relativePath() 转换路径;drivePathAPIURL() 在配置 RootFolderPath 时改用 effectiveDriveRootPath() 作为基准

  • util.gogetFiles() 在计算出初始 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:

  • OpenList-Frontend:
  • OpenList-Docs:

Related Issues / 关联 Issue

Testing / 测试

  • go vet ./drivers/onedrive_sharelink/...
  • go build ./drivers/onedrive_sharelink/...
  • Manual test / 手动测试: 编译 amd64 二进制通过,实际运行查看目录正常

Checklist / 检查清单

  • I have read CONTRIBUTING.
    / 我已阅读 CONTRIBUTING
  • I confirm this contribution follows the repository license, contribution policy, and code of conduct.
    / 我确认此贡献符合仓库许可证、贡献规范和行为准则。
  • I have formatted the changed code with gofmt, go fmt, or prettier where applicable.
    / 我已按适用情况使用 gofmtgo fmtprettier 格式化变更代码。
  • I have requested review from relevant maintainers or code owners where applicable.
    / 我已在适用情况下请求相关维护者或代码所有者审查。

AI Disclosure / AI 使用声明

  • This PR includes AI-assisted content.
    / 此 PR 包含 AI 辅助内容。

Tools used / 使用工具:

  • ChatGPT
  • Codex
  • GitHub Copilot
  • Claude
  • Gemini
  • Other (please specify) / 其他(请注明):

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-By attribution.
    / 我已确保所有 AI 辅助提交都包含 Co-Authored-By 归属信息。

  • I can reproduce all AI-assisted content included in this PR without any AI tools.
    / 我可以在没有任何 AI 工具的情况下重现此 PR 中包含的所有 AI 辅助内容。

…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>
@xrgzs xrgzs changed the title fix(onedrive_sharelink): respect root_folder_path for subdirectory access fix(drivers/onedrive_sharelink): respect root_folder_path for subdirectory access Jul 26, 2026
@xrgzs xrgzs added the Module: Driver Driver-Related Issue/PR label Jul 26, 2026
@PIKACHUIM
PIKACHUIM requested a review from Copilot July 30, 2026 03:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 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 across List, MakeDir, Put, GetDirectUploadInfo.
  • Adjust Graph API path building via effectiveDriveRootPath() when RootFolderPath is set.
  • Update getFiles() to treat RootFolderPath as 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.

Comment thread drivers/onedrive_sharelink/util.go
Comment thread drivers/onedrive_sharelink/driver.go
PIKACHUIM

This comment was marked as outdated.

@xireiki
xireiki requested a review from PIKACHUIM August 19, 2026 10:11

@PIKACHUIM PIKACHUIM left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🙏 感谢贡献

感谢 @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(),在 ListMakeDirPutGetDirectUploadInfo 等操作中统一使用相对路径转换,并在 getFiles() 中覆盖 rootFolder 以确保 GraphQL 查询指向正确的子目录。


📋 评审发现

总体评分:功能性 ⭐⭐⭐⭐ | 安全性 ⭐⭐⭐⭐ | 代码质量 ⭐⭐⭐ | 实现方案 ⭐⭐⭐

关键问题

  • ⚠️ 路径处理逻辑relativePath() 的路径前缀匹配可能在边界情况下产生误判(如 /foo/foobar
  • ⚠️ 代码重复:多处检查 RootFolderPath == "" || RootFolderPath == "/",存在重复逻辑
  • ⚠️ 格式不一致Init() 中新增的验证逻辑使用了空格缩进,与文件其他部分(使用制表符)不一致
  • 💡 测试覆盖:仅提供了手动测试,缺少单元测试覆盖路径转换的边界情况

📂 主要文件分析

drivers/onedrive_sharelink/driver.go

改动:新增字段 listURL;添加 relativePath()effectiveDriveRootPath() 方法;在 Init() 中验证 RootFolderPath 格式;在 ListMakeDirPutGetDirectUploadInfo 中应用路径转换;在 drivePathAPIURL() 中使用 effectiveDriveRootPath()

问题与建议

  1. ⚠️ 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
    }
  2. ⚠️ driver.go:85-90 - 缩进格式不一致

    • 问题:此处使用 4 空格缩进,而文件其他部分使用 tab 缩进。gofmt 会直接修改此处
    • 建议:运行 gofmt -w drivers/onedrive_sharelink/driver.go 统一格式化。PR 已在 Checklist 中勾选 gofmt,但实际并未格式化
  3. ⚠️ 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))
    }
  4. ⚠️ driver.go:486-501 (effectiveDriveRootPath) - 与 relativePath 存在逻辑重复

    • 问题:两个方法都做了 FixAndCleanPathHasPrefix + 路径分隔符检查、Warnf 后回退。差异仅在返回的是 root 还是 list 的剩余部分
    • 建议:考虑抽取共用 helper stripPrefix(fullPath, prefix string) (string, bool),由两个方法复用
  5. 做得好的地方

    • Init() 中路径验证逻辑 fail-fast 设计良好,能在驱动初始化阶段即时发现配置错误
    • relativePath()effectiveDriveRootPath() 职责分离清晰,分别处理虚拟路径和 drive API 路径
    • 详细的注释说明了 _relativePath / listURL / RootFolderPath 之间的关系,对后续维护者非常友好
    • 三个 commit 分工明确:第一处修复主功能,第二处合并上游,第三处对路径匹配的额外加固

drivers/onedrive_sharelink/util.go

改动:在 getFiles() 中,若用户配置了 RootFolderPath 则覆盖初始计算的 rootFolder

问题与建议

  1. ⚠️ util.go:261-265 - 路径覆盖缺乏前置校验

    • 问题:先从 redirect 中提取 rootFolder,然后无脑用 d.RootFolderPath 覆盖。如果 RootFolderPath 格式(如是否包含 Documents 段)不符合下方 strings.Split(rootFolder, "Documents") 的假设,下游会出错
    • 建议:要么先调用 utils.FixAndCleanPath(d.RootFolderPath) 再覆盖,要么在 Init() 校验时就把 Documents 段存在性当作硬约束
  2. 💡 util.go:262-266 - 硬编码 "Documents" 分割对本地化场景脆弱

    • 问题strings.Split(rootFolder, "Documents")[0] + "Documents" 假设路径中必含 Documents,但 OneDrive 中文/日文账户的文档库根段名可能是 文档 / ドキュメント
    • 建议:在文档中明确该驱动仅支持英文版 OneDrive,或用户配置 RootFolderPath 时需要自行带上 Documents
  3. 做得好的地方

    • 修改范围小、影响面窄,仅 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Module: Driver Driver-Related Issue/PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants