dae/common/assets/assets.go

124 lines
3.1 KiB
Go
Raw Permalink Normal View History

2023-01-23 18:54:21 +07:00
/*
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (c) 2022-2024, daeuniverse Organization <dae@v2raya.org>
2023-01-23 18:54:21 +07:00
*/
package assets
import (
"errors"
2023-02-01 13:08:01 +07:00
"fmt"
"github.com/daeuniverse/dae/common/consts"
2023-01-23 18:54:21 +07:00
"io/fs"
"os"
"path/filepath"
"runtime"
2023-02-01 13:08:01 +07:00
"strings"
2023-02-08 21:05:44 +07:00
"sync"
"time"
"github.com/adrg/xdg"
"github.com/sirupsen/logrus"
2023-01-23 18:54:21 +07:00
)
2023-02-08 21:05:44 +07:00
const CacheTimeout = 5 * time.Second
type CacheItem struct {
Filename string
Path string
CacheDeadline time.Time
}
type LocationFinder struct {
mu sync.Mutex
m map[string]CacheItem
externDirs []string
2023-02-08 21:05:44 +07:00
}
func NewLocationFinder(externDirPath []string) *LocationFinder {
2023-02-08 21:05:44 +07:00
return &LocationFinder{
mu: sync.Mutex{},
m: map[string]CacheItem{},
externDirs: externDirPath,
2023-02-08 21:05:44 +07:00
}
}
func (c *LocationFinder) GetLocationAsset(log *logrus.Logger, filename string) (path string, err error) {
c.mu.Lock()
defer c.mu.Unlock()
// Search cache.
2023-02-08 21:05:44 +07:00
if item, ok := c.m[filename]; ok && time.Now().Before(item.CacheDeadline) {
return item.Path, nil
}
defer func() {
if err == nil {
c.m[filename] = CacheItem{
Filename: filename,
Path: path,
CacheDeadline: time.Now().Add(CacheTimeout),
}
time.AfterFunc(CacheTimeout, func() {
c.mu.Lock()
defer c.mu.Unlock()
if item, ok := c.m[filename]; ok && time.Now().After(item.CacheDeadline) {
delete(c.m, filename)
}
})
}
}()
// Search dirs.
var searchDirs []string
folder := consts.AppName
2023-01-23 18:54:21 +07:00
// check if DAE_LOCATION_ASSET is set
location := os.Getenv("DAE_LOCATION_ASSET")
2023-01-23 18:54:21 +07:00
if location != "" {
// add DAE_LOCATION_ASSET to search path
searchDirs = append(searchDirs, location)
// add /etc/dae to search path
searchDirs = append(searchDirs, c.externDirs...)
2023-01-23 18:54:21 +07:00
// additional paths for non windows platforms
if runtime.GOOS != "windows" {
searchDirs = append(
searchDirs,
filepath.Join("/usr/local/share", folder),
filepath.Join("/usr/share", folder),
2023-01-23 18:54:21 +07:00
)
}
searchDirs = append(searchDirs, c.externDirs...)
2023-01-23 18:54:21 +07:00
} else {
// add /etc/dae to search path
searchDirs = append(searchDirs, c.externDirs...)
2023-01-23 18:54:21 +07:00
if runtime.GOOS != "windows" {
// Search XDG data directories on non windows platform
xdgDirs := append([]string{xdg.DataHome}, xdg.DataDirs...)
for i := range xdgDirs {
xdgDirs[i] = filepath.Join(xdgDirs[i], folder)
2023-02-01 13:08:01 +07:00
}
searchDirs = append(searchDirs, xdgDirs...)
2023-02-01 13:08:01 +07:00
} else {
// fallback to the old behavior of using only current dir on Windows
pwd := "./"
if absPath, e := filepath.Abs(pwd); e == nil {
pwd = absPath
2023-01-23 18:54:21 +07:00
}
searchDirs = append(searchDirs, pwd)
}
}
log.Debugf(`Search "%v" in [%v]`, filename, strings.Join(searchDirs, ", "))
for _, searchDir := range searchDirs {
searchPath := filepath.Join(searchDir, filename)
if _, err = os.Stat(searchPath); err != nil {
if errors.Is(err, fs.ErrNotExist) {
continue
2023-01-23 18:54:21 +07:00
}
return "", err
2023-01-23 18:54:21 +07:00
}
log.Debugf(`Found "%v" at %v`, filename, searchPath)
// return the first path that exists
return searchPath, nil
2023-01-23 18:54:21 +07:00
}
return "", fmt.Errorf("%v: %w in [%v]", filename, os.ErrNotExist, strings.Join(searchDirs, ", "))
2023-01-23 18:54:21 +07:00
}