From bc008113b6d1654e34ed372a385cc9dd0a0e2936 Mon Sep 17 00:00:00 2001 From: vcptr <51714622+vcptr@users.noreply.github.com> Date: Mon, 17 Aug 2020 18:56:01 +0800 Subject: [PATCH] add asset location search --- common/platform/others.go | 22 ++++++++++++++++++++++ common/platform/platform.go | 6 ------ common/platform/windows.go | 7 +++++++ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/common/platform/others.go b/common/platform/others.go index e93f5af6b..e526f498d 100644 --- a/common/platform/others.go +++ b/common/platform/others.go @@ -20,3 +20,25 @@ func GetToolLocation(file string) string { toolPath := EnvFlag{Name: name, AltName: NormalizeEnvName(name)}.GetValue(getExecutableDir) return filepath.Join(toolPath, file) } + +// GetAssetLocation search for `file` in certain locations +func GetAssetLocation(file string) string { + const name = "v2ray.location.asset" + assetPath := NewEnvFlag(name).GetValue(getExecutableDir) + defPath := filepath.Join(assetPath, file) + for _, p := range []string{ + defPath, + filepath.Join("/usr/local/share/v2ray/", file), + filepath.Join("/usr/share/v2ray/", file), + } { + if _, err := os.Stat(p); os.IsNotExist(err) { + continue + } + + // asset found + return p + } + + // asset not found, let the caller throw out the error + return defPath +} diff --git a/common/platform/platform.go b/common/platform/platform.go index b57a98fd7..c0bf8c20b 100644 --- a/common/platform/platform.go +++ b/common/platform/platform.go @@ -66,12 +66,6 @@ func getExecutableSubDir(dir string) func() string { } } -func GetAssetLocation(file string) string { - const name = "v2ray.location.asset" - assetPath := NewEnvFlag(name).GetValue(getExecutableDir) - return filepath.Join(assetPath, file) -} - func GetPluginDirectory() string { const name = "v2ray.location.plugin" pluginDir := NewEnvFlag(name).GetValue(getExecutableSubDir("plugins")) diff --git a/common/platform/windows.go b/common/platform/windows.go index c311ef5c0..454a24063 100644 --- a/common/platform/windows.go +++ b/common/platform/windows.go @@ -18,3 +18,10 @@ func GetToolLocation(file string) string { toolPath := EnvFlag{Name: name, AltName: NormalizeEnvName(name)}.GetValue(getExecutableDir) return filepath.Join(toolPath, file+".exe") } + +// GetAssetLocation search for `file` in the excutable dir +func GetAssetLocation(file string) string { + const name = "v2ray.location.asset" + assetPath := NewEnvFlag(name).GetValue(getExecutableDir) + return filepath.Join(assetPath, file) +}