feat: seprate os and docker default download dir

This commit is contained in:
Simon Ding
2025-08-31 23:25:33 +08:00
parent 94b4db3310
commit 05d114be66
2 changed files with 43 additions and 1 deletions

View File

@@ -62,7 +62,17 @@ func (c *client) init() {
downloadDir := c.GetSetting(SettingDownloadDir)
if downloadDir == "" {
log.Infof("set default download dir")
c.SetSetting(SettingDownloadDir, "/downloads")
if utils.IsRunningInDocker() {
c.SetSetting(SettingDownloadDir, "/downloads")
} else {
downloadDir, err := utils.UserDownloadDir()
if err != nil {
log.Errorf("get user download dir error: %v", err)
downloadDir = "/downloads"
}
c.SetSetting(SettingDownloadDir, downloadDir)
}
}
logLevel := c.GetSetting(SettingLogLevel)
if logLevel == "" {

View File

@@ -314,3 +314,35 @@ func DirSize(path string) (int64, error) {
})
return size, err
}
func IsRunningInDocker() bool {
if _, err := os.Stat("/.dockerenv"); err == nil {
return true
}
return false
}
func UserDownloadDir() (string, error) {
var downloadDirNames []string = []string{"Downloads", "downloads", "download", "下载"}
if IsRunningInDocker() {
return "/downloads", nil
}
homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}
for _, ddn := range downloadDirNames {
var dir = filepath.Join(homeDir, ddn)
if _, err := os.Stat(dir); os.IsNotExist(err) {
continue
} else {
return dir, nil
}
}
return "", errors.New("no download dir found")
}