From 434a43a0faf628fb1f567b7fb65d8c44969172fe Mon Sep 17 00:00:00 2001 From: fgsfds <4870330+fgsfds@users.noreply.github.com> Date: Sat, 9 Aug 2025 16:39:50 +0500 Subject: [PATCH] moved db to user folder on windows --- config/config.go | 42 +++++++++++++++++++++++++++++++++++++++--- go.mod | 2 ++ go.sum | 4 ++++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/config/config.go b/config/config.go index 70be5ae6..14237cf4 100644 --- a/config/config.go +++ b/config/config.go @@ -4,7 +4,12 @@ import ( _ "embed" "fmt" "os" + "path/filepath" + "runtime" "strings" + "x-ui/logger" + + "github.com/otiai10/copy" ) //go:embed version @@ -56,10 +61,30 @@ func GetBinFolderPath() string { func GetDBFolderPath() string { dbFolderPath := os.Getenv("XUI_DB_FOLDER") - if dbFolderPath == "" { - dbFolderPath = "/etc/x-ui" + if dbFolderPath != "" { + return dbFolderPath + } + + defaultFolder := "/etc/x-ui" + + if runtime.GOOS == "windows" { + homeDir, err := os.UserHomeDir() + if err != nil { + logger.Error("Error while getting user folder: %w", err) + return defaultFolder + } + + userFolder := filepath.Join(homeDir, "x-ui") + err = moveExistingDb(defaultFolder, userFolder) + if err != nil { + logger.Error("Error while moving existing DB: %w", err) + return defaultFolder + } + + return userFolder + } else { + return defaultFolder } - return dbFolderPath } func GetDBPath() string { @@ -73,3 +98,14 @@ func GetLogFolder() string { } return logFolderPath } + +func moveExistingDb(from string, to string) error { + if _, err := os.Stat(to); os.IsNotExist(err) { + if _, err := os.Stat(from); !os.IsNotExist(err) { + if err := copy.Copy(from, to); err != nil { + return fmt.Errorf("failed to copy %s to %s: %w", from, to, err) + } + } + } + return nil +} diff --git a/go.mod b/go.mod index 771f1070..af046e6c 100644 --- a/go.mod +++ b/go.mod @@ -61,6 +61,8 @@ require ( github.com/miekg/dns v1.1.68 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/otiai10/mint v1.6.3 // indirect + github.com/otiai10/copy v1.14.1 github.com/pires/go-proxyproto v0.8.1 // indirect github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect github.com/quic-go/qpack v0.5.1 // indirect diff --git a/go.sum b/go.sum index 74eee2b8..d7b8da45 100644 --- a/go.sum +++ b/go.sum @@ -116,6 +116,10 @@ github.com/nicksnyder/go-i18n/v2 v2.6.0 h1:C/m2NNWNiTB6SK4Ao8df5EWm3JETSTIGNXBpM github.com/nicksnyder/go-i18n/v2 v2.6.0/go.mod h1:88sRqr0C6OPyJn0/KRNaEz1uWorjxIKP7rUUcvycecE= github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 h1:lDH9UUVJtmYCjyT0CI4q8xvlXPxeZ0gYCVvWbmPlp88= github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk= +github.com/otiai10/copy v1.14.1 h1:5/7E6qsUMBaH5AnQ0sSLzzTg1oTECmcCmT6lvF45Na8= +github.com/otiai10/copy v1.14.1/go.mod h1:oQwrEDDOci3IM8dJF0d8+jnbfPDllW6vUjNc3DoZm9I= +github.com/otiai10/mint v1.6.3 h1:87qsV/aw1F5as1eH1zS/yqHY85ANKVMgkDrf9rcxbQs= +github.com/otiai10/mint v1.6.3/go.mod h1:MJm72SBthJjz8qhefc4z1PYEieWmy8Bku7CjcAqyUSM= github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8= github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=