mirror of
https://gitee.com/zhangsken/aliyunpan.git
synced 2026-10-16 10:29:42 +08:00
adapt XDG folder #433
This commit is contained in:
@@ -15,6 +15,7 @@ package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/adrg/xdg"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/tickstep/aliyunpan-api/aliyunpan"
|
||||
"github.com/tickstep/aliyunpan/cmder/cmdutil"
|
||||
@@ -24,6 +25,7 @@ import (
|
||||
"github.com/tickstep/library-go/logger"
|
||||
"github.com/tickstep/library-go/requester"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
@@ -31,6 +33,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// EnvDownloadDir 配置下载环境变量
|
||||
EnvDownloadDir = "ALIYUNPAN_DOWNLOAD_DIR"
|
||||
// EnvVerbose 启用调试环境变量
|
||||
EnvVerbose = "ALIYUNPAN_VERBOSE"
|
||||
// EnvConfigDir 配置路径环境变量
|
||||
@@ -283,22 +287,7 @@ func (c *PanConfig) loadConfigFromFile() (err error) {
|
||||
}
|
||||
|
||||
func (c *PanConfig) initDefaultConfig() {
|
||||
// 设置默认的下载路径
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
c.SaveDir = cmdutil.ExecutablePathJoin("Downloads")
|
||||
case "android":
|
||||
// TODO: 获取完整的的下载路径
|
||||
c.SaveDir = "/sdcard/Download"
|
||||
default:
|
||||
dataPath, ok := os.LookupEnv("HOME")
|
||||
if !ok {
|
||||
CmdConfigVerbose.Warn("Environment HOME not set")
|
||||
c.SaveDir = cmdutil.ExecutablePathJoin("Downloads")
|
||||
} else {
|
||||
c.SaveDir = filepath.Join(dataPath, "Downloads")
|
||||
}
|
||||
}
|
||||
//c.SaveDir = GetDefaultDownloadDir()
|
||||
c.ConfigVer = ConfigVersion
|
||||
c.VideoFileExtensions = DefaultVideoFileExtensions
|
||||
c.DeviceId = RandomDeviceId() // 生成默认客户端ID
|
||||
@@ -311,7 +300,7 @@ func (c *PanConfig) initDefaultConfig() {
|
||||
// GetConfigDir 获取配置路径
|
||||
func GetConfigDir() string {
|
||||
// 按照以下顺序依次获取配置目录
|
||||
// 1.环境变量ALIYUNPAN_CONFIG_DIR => 2. /etc/aliyunpan/ => 3. ~/.aliyunpan/ => 4.当前程序目录
|
||||
// 1.环境变量ALIYUNPAN_CONFIG_DIR => 2. XDG_CONFIG_HOME/aliyunpan => 3. /etc/aliyunpan/ => 4. ~/.aliyunpan/ => 5.当前程序目录
|
||||
|
||||
// 1. 从环境变量读取
|
||||
configDir, ok := os.LookupEnv(EnvConfigDir)
|
||||
@@ -325,7 +314,14 @@ func GetConfigDir() string {
|
||||
logger.Verboseln("use config dir from ALIYUNPAN_CONFIG_DIR env: ", configDir)
|
||||
return configDir
|
||||
} else {
|
||||
// 2. /etc/aliyunpan/
|
||||
// 2. $XDG_CONFIG_HOME/aliyunpan
|
||||
xdgConfigHome := path.Join(xdg.ConfigHome, "aliyunpan")
|
||||
if IsFolderExist(xdgConfigHome) {
|
||||
logger.Verboseln("use XDG home config dir: ", xdgConfigHome)
|
||||
return xdgConfigHome
|
||||
}
|
||||
|
||||
// 3. /etc/aliyunpan/
|
||||
if runtime.GOOS == "linux" {
|
||||
cd := "/etc/aliyunpan"
|
||||
if IsFolderExist(cd) {
|
||||
@@ -334,7 +330,7 @@ func GetConfigDir() string {
|
||||
}
|
||||
}
|
||||
|
||||
// 3. ~/.aliyunpan/
|
||||
// 4. ~/.aliyunpan/
|
||||
if runtime.GOOS == "linux" || runtime.GOOS == "windows" {
|
||||
cd, er := homedir.Expand("~/.aliyunpan")
|
||||
if er == nil {
|
||||
@@ -346,12 +342,55 @@ func GetConfigDir() string {
|
||||
}
|
||||
}
|
||||
|
||||
// 4.当前程序所在目录
|
||||
// 5.当前程序所在目录
|
||||
configDir = cmdutil.ExecutablePathJoin("")
|
||||
logger.Verboseln("use config dir: ", configDir)
|
||||
return configDir
|
||||
}
|
||||
|
||||
// GetDefaultDownloadDir 获取默认的下载目录
|
||||
func GetDefaultDownloadDir() string {
|
||||
// 按照以下顺序依次获取下载目录
|
||||
// 1.环境变量ALIYUNPAN_DOWNLOAD_DIR => 2. XDG_DOWNLOAD_DIR目录 => 3. (当前程序目录)/Downloads
|
||||
|
||||
downloadDir := ""
|
||||
// 设置默认的下载路径
|
||||
switch runtime.GOOS {
|
||||
case "android":
|
||||
// TODO: 获取完整的的下载路径
|
||||
downloadDir = "/sdcard/Download"
|
||||
return downloadDir
|
||||
default:
|
||||
// 1. 环境变量ALIYUNPAN_DOWNLOAD_DIR
|
||||
d, ok := os.LookupEnv(EnvDownloadDir)
|
||||
if ok {
|
||||
logger.Verboseln("use download dir from ALIYUNPAN_DOWNLOAD_DIR env: ", d)
|
||||
downloadDir = d
|
||||
return downloadDir
|
||||
} else {
|
||||
// 2. $XDG_DOWNLOAD_DIR
|
||||
xdgDownloadDir := path.Join(xdg.UserDirs.Download)
|
||||
if IsFolderExist(xdgDownloadDir) {
|
||||
logger.Verboseln("use XDG download dir: ", xdgDownloadDir)
|
||||
return xdgDownloadDir
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. (当前程序目录)/Downloads
|
||||
downloadDir = cmdutil.ExecutablePathJoin("Downloads")
|
||||
logger.Verboseln("use default download dir: ", downloadDir)
|
||||
return downloadDir
|
||||
}
|
||||
|
||||
// GetDownloadDir 获取当前下载目录
|
||||
func GetDownloadDir() string {
|
||||
if Config.SaveDir == "" {
|
||||
return GetDefaultDownloadDir()
|
||||
}
|
||||
return Config.SaveDir
|
||||
}
|
||||
|
||||
// GetPluginDir 获取插件文件夹路径
|
||||
func GetPluginDir() string {
|
||||
return strings.TrimSuffix(GetConfigDir(), "/") + "/plugin"
|
||||
|
||||
@@ -113,7 +113,7 @@ func (c *PanConfig) PrintTable() {
|
||||
[]string{"max_upload_parallel", strconv.Itoa(c.MaxUploadParallel), "1 ~ 20", "最大上传并发量,即同时上传文件最大数量"},
|
||||
[]string{"max_download_rate", showMaxRate(c.MaxDownloadRate), "", "限制单个文件最大下载速度, 0代表不限制"},
|
||||
[]string{"max_upload_rate", showMaxRate(c.MaxUploadRate), "", "限制单个文件最大上传速度, 0代表不限制"},
|
||||
[]string{"savedir", c.SaveDir, "", "下载文件的储存目录"},
|
||||
[]string{"savedir", GetDownloadDir(), "", "下载文件的储存目录"},
|
||||
[]string{"proxy", c.Proxy, "", "设置代理, 支持 http/socks5 代理,例如: http://127.0.0.1:8888 或者 socks5://127.0.0.1:8889"},
|
||||
[]string{"local_addrs", c.LocalAddrs, "", "绑定本地网卡地址, 多个地址用逗号隔开,支持网口名称,例如: 127.0.0.1,192.168.100.126,en0,eth0"},
|
||||
[]string{"ip_type", c.PreferIPType, "ipv4-优先IPv4,ipv6-优先IPv6", "设置域名解析IP优先类型。修改后需要重启应用生效"},
|
||||
|
||||
@@ -332,7 +332,7 @@ func (pu *PanUser) FreshWorkdirInfo() {
|
||||
// GetSavePath 根据提供的网盘文件路径 panpath, 返回本地储存路径,
|
||||
// 返回绝对路径, 获取绝对路径出错时才返回相对路径...
|
||||
func (pu *PanUser) GetSavePath(filePanPath string) string {
|
||||
dirStr := filepath.Join(Config.SaveDir, fmt.Sprintf("%s", pu.UserId), filePanPath)
|
||||
dirStr := filepath.Join(GetDownloadDir(), fmt.Sprintf("%s", pu.UserId), filePanPath)
|
||||
dir, err := filepath.Abs(dirStr)
|
||||
if err != nil {
|
||||
dir = filepath.Clean(dirStr)
|
||||
|
||||
Reference in New Issue
Block a user