From efedc35d6a0c9833171336f474f71a7d41d5788a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=B8=85=E4=BD=A0=E6=8A=A5=E8=AD=A6?= Date: Sat, 30 Oct 2021 23:21:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=95=B0=E6=8D=AE=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E5=8F=AF=E9=85=8D=E7=BD=AE=E7=9A=84=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 配置文件中增加了DataPath参数,如果该参数为空,使用可执行文件所在的目录,否则使用DataPath指定的目录。 --- global/conf.go | 3 +++ main.go | 2 +- pkg/conf/conf.go | 3 +++ pkg/util/path.go | 17 +++++++++++++++-- 4 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 global/conf.go diff --git a/global/conf.go b/global/conf.go new file mode 100644 index 0000000..6b507fb --- /dev/null +++ b/global/conf.go @@ -0,0 +1,3 @@ +package global + +var DataPath string diff --git a/main.go b/main.go index d71e587..f609018 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 ceeb597..54a9bbd 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 ff51d57..07be040 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) + } +}