Files
xiaohongshu-mcp/cookies/cookies.go
zy 84f6a85105 优化 cookies 路径管理策略 (#127)
* feat: 优化 cookies 路径管理策略

1. 实现向后兼容的路径迁移逻辑:
   - 优先使用旧路径 /tmp/cookies.json(如果存在)
   - 否则使用当前目录 ./cookies.json
2. 移除不必要的目录创建逻辑
   - 删除 NewLoadCookie 中的 MkdirAll 调用
   - 避免相对路径可能导致的权限问题

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* feat: 添加 cookies.json 到 .gitignore

- 避免将包含敏感登录信息的 cookies 文件提交到版本控制
- 保护用户隐私和安全

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-09-21 23:25:11 +08:00

62 lines
1.3 KiB
Go

package cookies
import (
"os"
"path/filepath"
"github.com/pkg/errors"
)
type Cookier interface {
LoadCookies() ([]byte, error)
SaveCookies(data []byte) error
}
type localCookie struct {
path string
}
func NewLoadCookie(path string) Cookier {
if path == "" {
panic("path is required")
}
return &localCookie{
path: path,
}
}
// LoadCookies 从文件中加载 cookies。
func (c *localCookie) LoadCookies() ([]byte, error) {
data, err := os.ReadFile(c.path)
if err != nil {
return nil, errors.Wrap(err, "failed to read cookies from tmp file")
}
return data, nil
}
// SaveCookies 保存 cookies 到文件中。
func (c *localCookie) SaveCookies(data []byte) error {
return os.WriteFile(c.path, data, 0644)
}
// GetCookiesFilePath 获取 cookies 文件路径。
// 为了向后兼容,如果旧路径 /tmp/cookies.json 存在,则继续使用;
// 否则使用当前目录下的 cookies.json
func GetCookiesFilePath() string {
// 旧路径:/tmp/cookies.json
tmpDir := os.TempDir()
oldPath := filepath.Join(tmpDir, "cookies.json")
// 检查旧路径文件是否存在
if _, err := os.Stat(oldPath); err == nil {
// 文件存在,使用旧路径(向后兼容)
return oldPath
}
// 文件不存在,使用新路径(当前目录)
return "cookies.json"
}