dae/common/assets/assets.go

122 lines
2.9 KiB
Go
Raw Normal View History

2023-01-23 18:54:21 +07:00
/*
* SPDX-License-Identifier: AGPL-3.0-only
2023-03-14 14:01:55 +07:00
* Copyright (c) 2022-2023, 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"
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 := "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
searchDirs = []string{
location,
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 {
if runtime.GOOS != "windows" {
// Search XDG data directories on non windows platform
searchDirs = append([]string{xdg.DataHome}, xdg.DataDirs...)
2023-02-01 13:08:01 +07:00
for i := range searchDirs {
searchDirs[i] = filepath.Join(searchDirs[i], folder)
}
searchDirs = append(searchDirs, c.externDirs...)
2023-02-01 13:08:01 +07:00
} else {
searchDirs = append([]string{}, c.externDirs...)
2023-02-01 13:08:01 +07:00
// 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
}