diff --git a/global/conf.go b/global/conf.go new file mode 100644 index 00000000..6b507fb1 --- /dev/null +++ b/global/conf.go @@ -0,0 +1,3 @@ +package global + +var DataPath string diff --git a/main.go b/main.go index d71e5873..f6090181 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,7 @@ var ( ) func init() { - flag.StringVar(&confPath, "c", util.RelativePath("conf.ini"), "配置文件路径") + flag.StringVar(&confPath, "c", util.RelativeExecutablePath("conf.ini"), "配置文件路径") flag.BoolVar(&isEject, "eject", false, "导出内置静态资源") flag.StringVar(&scriptName, "database-script", "", "运行内置数据库助手脚本") flag.Parse() diff --git a/pkg/conf/conf.go b/pkg/conf/conf.go index ceeb5971..54a9bbd5 100644 --- a/pkg/conf/conf.go +++ b/pkg/conf/conf.go @@ -2,6 +2,7 @@ package conf import ( "github.com/cloudreve/Cloudreve/v3/pkg/util" + "github.com/cloudreve/Cloudreve/v3/global" "github.com/go-ini/ini" "gopkg.in/go-playground/validator.v9" ) @@ -26,6 +27,7 @@ type system struct { Debug bool SessionSecret string HashIDSalt string + DataPath string } type ssl struct { @@ -146,6 +148,7 @@ func Init(path string) { util.GloablLogger = nil util.Log() } + global.DataPath = SystemConfig.DataPath } diff --git a/pkg/util/path.go b/pkg/util/path.go index ff51d579..07be0406 100644 --- a/pkg/util/path.go +++ b/pkg/util/path.go @@ -5,6 +5,7 @@ import ( "path" "path/filepath" "strings" + "github.com/cloudreve/Cloudreve/v3/global" ) // DotPathToStandardPath 将","分割的路径转换为标准路径 @@ -48,11 +49,23 @@ func FormSlash(old string) string { return path.Clean(strings.ReplaceAll(old, "\\", "/")) } -// RelativePath 获取相对可执行文件的路径 -func RelativePath(name string) string { +// RelativeExecutablePath 获取相对可执行文件的路径 +func RelativeExecutablePath(name string) string { if filepath.IsAbs(name) { return name } e, _ := os.Executable() return filepath.Join(filepath.Dir(e), name) } + +//RelativePath 获取相对路径 +func RelativePath(name string) string { + if filepath.IsAbs(name) { + return name + } + if global.DataPath != "" { + return filepath.Join(global.DataPath, name) + } else { + return RelativeExecutablePath(name) + } +}