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

配置文件中增加了DataPath参数,如果该参数为空,使用可执行文件所在的目录,否则使用DataPath指定的目录。
pull/1039/head
不帅你报警 4 years ago
parent a3b4a22dbc
commit efedc35d6a

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

@ -16,7 +16,7 @@ var (
) )
func init() { func init() {
flag.StringVar(&confPath, "c", util.RelativePath("conf.ini"), "配置文件路径") flag.StringVar(&confPath, "c", util.RelativeExecutablePath("conf.ini"), "配置文件路径")
flag.BoolVar(&isEject, "eject", false, "导出内置静态资源") flag.BoolVar(&isEject, "eject", false, "导出内置静态资源")
flag.StringVar(&scriptName, "database-script", "", "运行内置数据库助手脚本") flag.StringVar(&scriptName, "database-script", "", "运行内置数据库助手脚本")
flag.Parse() flag.Parse()

@ -2,6 +2,7 @@ package conf
import ( import (
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/cloudreve/Cloudreve/v3/global"
"github.com/go-ini/ini" "github.com/go-ini/ini"
"gopkg.in/go-playground/validator.v9" "gopkg.in/go-playground/validator.v9"
) )
@ -26,6 +27,7 @@ type system struct {
Debug bool Debug bool
SessionSecret string SessionSecret string
HashIDSalt string HashIDSalt string
DataPath string
} }
type ssl struct { type ssl struct {
@ -146,6 +148,7 @@ func Init(path string) {
util.GloablLogger = nil util.GloablLogger = nil
util.Log() util.Log()
} }
global.DataPath = SystemConfig.DataPath
} }

@ -5,6 +5,7 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/cloudreve/Cloudreve/v3/global"
) )
// DotPathToStandardPath 将","分割的路径转换为标准路径 // DotPathToStandardPath 将","分割的路径转换为标准路径
@ -48,11 +49,23 @@ func FormSlash(old string) string {
return path.Clean(strings.ReplaceAll(old, "\\", "/")) return path.Clean(strings.ReplaceAll(old, "\\", "/"))
} }
// RelativePath 获取相对可执行文件的路径 // RelativeExecutablePath 获取相对可执行文件的路径
func RelativePath(name string) string { func RelativeExecutablePath(name string) string {
if filepath.IsAbs(name) { if filepath.IsAbs(name) {
return name return name
} }
e, _ := os.Executable() e, _ := os.Executable()
return filepath.Join(filepath.Dir(e), name) 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)
}
}

Loading…
Cancel
Save