Skip to content

windows环境下,认证文件显示重复的BUG #822

@74596877

Description

@74596877

Windows 平台认证文件重复显示修复总结

1. 问题描述

在 Windows 平台上,文件系统对路径大小写不敏感(例如 Auths\file.jsonauths\File.json 指向同一文件),但程序内部使用字符串路径作为唯一标识符(ID)且大小写敏感。

这导致:

  1. Watcher 自动扫描时使用系统返回的路径格式生成 ID。
  2. 管理 API 或其他手动加载流程可能使用不同大小写的路径生成 ID。
  3. 结果导致同一个物理文件在内存中被注册为两个不同的认证条目,从而在管理页面和配额页面重复显示。

2. 修复方案

在生成认证 ID 的关键路径上,针对 Windows 平台强制进行路径规范化(统一转换为小写)。

3. 修改文件列表

3.1. internal/watcher/synthesizer/file.go

作用:负责扫描磁盘上的认证文件并生成内存对象。

修改内容
Synthesize 方法中,计算出相对路径 ID 后,增加 Windows 平台检测并转为小写。

// 引入 runtime 包
import "runtime"

// ... 在 Synthesize 方法中 ...

// Use relative path under authDir as ID to stay consistent with the file-based token store
id := full
if rel, errRel := filepath.Rel(ctx.AuthDir, full); errRel == nil && rel != "" {
    id = rel
}

// [新增] Windows 平台下强制转为小写,确保 ID 一致性
if runtime.GOOS == "windows" {
    id = strings.ToLower(id)
}

3.2. internal/api/handlers/management/auth_files.go

作用:处理管理 API 请求,包括手动上传、注册认证文件。

修改内容
authIDForPath 辅助方法中,增加 Windows 平台检测并转为小写。

// 引入 runtime 包
import "runtime"

// ...

func (h *Handler) authIDForPath(path string) string {
    // ... (前置逻辑)
    
    if rel, err := filepath.Rel(authDir, path); err == nil && rel != "" {
        // [新增]
        if runtime.GOOS == "windows" {
            return strings.ToLower(rel)
        }
        return rel
    }
    
    // [新增]
    if runtime.GOOS == "windows" {
        return strings.ToLower(path)
    }
    return path
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions