dae/common/assets/assets.go

79 lines
2.4 KiB
Go
Raw Normal View History

2023-01-23 18:54:21 +07:00
/*
* SPDX-License-Identifier: AGPL-3.0-only
2023-01-28 12:56:06 +07:00
* Copyright (c) since 2023, v2rayA Organization <team@v2raya.org>
2023-01-23 18:54:21 +07:00
*/
package assets
import (
"errors"
2023-02-01 13:08:01 +07:00
"fmt"
2023-01-23 18:54:21 +07:00
"github.com/adrg/xdg"
2023-02-01 13:08:01 +07:00
"github.com/sirupsen/logrus"
2023-01-23 18:54:21 +07:00
"io/fs"
"os"
"path/filepath"
"runtime"
2023-02-01 13:08:01 +07:00
"strings"
2023-01-23 18:54:21 +07:00
)
2023-02-01 13:08:01 +07:00
func GetLocationAsset(log *logrus.Logger, filename string) (path string, err error) {
folder := "dae"
2023-01-23 18:54:21 +07:00
location := os.Getenv("DAE_LOCATION_ASSET")
// check if DAE_LOCATION_ASSET is set
if location != "" {
// add DAE_LOCATION_ASSET to search path
searchPaths := []string{
filepath.Join(location, filename),
}
// additional paths for non windows platforms
if runtime.GOOS != "windows" {
searchPaths = append(
searchPaths,
filepath.Join("/usr/local/share", folder, filename),
filepath.Join("/usr/share", folder, filename),
)
}
2023-02-01 13:08:01 +07:00
searchDirs := make([]string, len(searchPaths))
for i := range searchDirs {
searchDirs[i] = filepath.Dir(searchPaths[i])
}
log.Debugf(`Search "%v" in [%v]`, filename, strings.Join(searchDirs, ", "))
2023-01-23 18:54:21 +07:00
for _, searchPath := range searchPaths {
if _, err = os.Stat(searchPath); err != nil && errors.Is(err, fs.ErrNotExist) {
continue
}
2023-02-01 13:08:01 +07:00
log.Debugf(`Found "%v" at %v`, filename, searchPath)
2023-01-23 18:54:21 +07:00
// return the first path that exists
return searchPath, nil
}
2023-02-01 13:08:01 +07:00
return "", fmt.Errorf("%v: %w in [%v]", filename, os.ErrNotExist, strings.Join(searchDirs, ", "))
2023-01-23 18:54:21 +07:00
} else {
if runtime.GOOS != "windows" {
// search XDG data directories on non windows platform
2023-02-01 13:08:01 +07:00
searchDirs := append([]string{xdg.DataHome}, xdg.DataDirs...)
for i := range searchDirs {
searchDirs[i] = filepath.Join(searchDirs[i], folder)
}
log.Debugf(`Search "%v" in [%v]`, filename, strings.Join(searchDirs, ", "))
2023-01-23 18:54:21 +07:00
relpath := filepath.Join(folder, filename)
fullpath, err := xdg.SearchDataFile(relpath)
if err != nil {
2023-02-01 13:08:01 +07:00
return "", fmt.Errorf("%v: %w in [%v]", filename, os.ErrNotExist, strings.Join(searchDirs, ", "))
2023-01-23 18:54:21 +07:00
}
2023-02-01 13:08:01 +07:00
log.Debugf(`Found "%v" at %v`, filename, fullpath)
return fullpath, nil
} else {
// fallback to the old behavior of using only current dir on Windows
path := filepath.Join("./", filename)
if absPath, e := filepath.Abs(path); e == nil {
path = absPath
2023-01-23 18:54:21 +07:00
}
2023-02-01 13:08:01 +07:00
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
return "", fmt.Errorf("%v: %w in %v", filename, os.ErrNotExist, path)
2023-01-23 18:54:21 +07:00
}
2023-02-01 13:08:01 +07:00
return path, nil
2023-01-23 18:54:21 +07:00
}
}
}