增加数据目录可配置的能力

配置文件中增加了DataPath参数,如果该参数为空,使用可执行文件所在的目录,否则使用DataPath指定的目录。
This commit is contained in:
不帅你报警 2021-10-30 23:21:20 +08:00
parent a3b4a22dbc
commit efedc35d6a
4 changed files with 22 additions and 3 deletions

3
global/conf.go Normal file
View file

@ -0,0 +1,3 @@
package global
var DataPath string

View file

@ -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()

View file

@ -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
}

View file

@ -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)
}
}