moved db to user folder on windows

This commit is contained in:
fgsfds 2025-08-09 16:39:50 +05:00
parent 4f25eb230e
commit 434a43a0fa
No known key found for this signature in database
GPG key ID: 264C1B9113012917
3 changed files with 45 additions and 3 deletions

View file

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

2
go.mod
View file

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

4
go.sum
View file

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