This commit is contained in:
Drew Weymouth
2025-02-06 08:53:38 -03:00
parent 59931e3679
commit 6d5af7e841
4 changed files with 29 additions and 20 deletions
+7 -3
View File
@@ -48,6 +48,7 @@ type App struct {
MPRISHandler *MPRISHandler
WinSMTC *SMTC
ipcServer ipc.IPCServer
LrcLibFetcher *LrcLibFetcher
// UI callbacks to be set in main
OnReactivate func()
@@ -57,7 +58,7 @@ type App struct {
displayAppName string
appVersionTag string
configDir string
CacheDir string
cacheDir string
portableMode bool
isFirstLaunch bool // set by config file reader
@@ -103,7 +104,7 @@ func StartupApp(appName, displayAppName, appVersion, appVersionTag, latestReleas
displayAppName: displayAppName,
appVersionTag: appVersionTag,
configDir: confDir,
CacheDir: cacheDir,
cacheDir: cacheDir,
portableMode: portableMode,
}
a.bgrndCtx, a.cancel = context.WithCancel(context.Background())
@@ -144,6 +145,9 @@ func StartupApp(appName, displayAppName, appVersion, appVersionTag, latestReleas
a.ServerManager.SetPrefetchAlbumCoverCallback(func(coverID string) {
_, _ = a.ImageManager.GetCoverThumbnail(coverID)
})
if a.Config.Application.EnableLrcLib {
a.LrcLibFetcher = NewLrcLibFetcher(a.cacheDir)
}
a.PlaybackManager.OnPlaying(func() {
SetSystemSleepDisabled(true)
@@ -411,7 +415,7 @@ func (a *App) LoginToDefaultServer(string) error {
}
func (a *App) DeleteServerCacheDir(serverID uuid.UUID) error {
path := path.Join(a.CacheDir, serverID.String())
path := path.Join(a.cacheDir, serverID.String())
log.Printf("Deleting server cache dir: %s", path)
return os.RemoveAll(path)
}
+15 -8
View File
@@ -20,13 +20,21 @@ import (
"github.com/dweymouth/supersonic/backend/mediaprovider"
)
const CACHE_LYRICS_FOLDER = "lyrics"
const lrclibCacheFolder = "lrclib"
func FetchLrcLibLyricsCached(name, artist, album string, durationSecs int, cacheDir string) (*mediaprovider.Lyrics, error) {
hash := makeTrackIdHash(name, artist, album, durationSecs)
cachePath := filepath.Join(cacheDir, CACHE_LYRICS_FOLDER)
type LrcLibFetcher struct {
cachePath string
}
func NewLrcLibFetcher(baseCacheDir string) *LrcLibFetcher {
cachePath := filepath.Join(baseCacheDir, lrclibCacheFolder)
configdir.MakePath(cachePath)
cacheFilePath := filepath.Join(cachePath, fmt.Sprintf("%s_lyrics.txt", hash))
return &LrcLibFetcher{cachePath: cachePath}
}
func (l *LrcLibFetcher) FetchLrcLibLyrics(name, artist, album string, durationSecs int) (*mediaprovider.Lyrics, error) {
hash := makeTrackIdHash(name, artist, album, durationSecs)
cacheFilePath := filepath.Join(l.cachePath, fmt.Sprintf("%s.txt", hash))
// File is cached. Try to use it
if _, err := os.Stat(cacheFilePath); err == nil {
@@ -42,7 +50,7 @@ func FetchLrcLibLyricsCached(name, artist, album string, durationSecs int, cache
}
// Fetch the lyrics
lyrics, err := FetchLrcLibLyrics(name, artist, album, durationSecs)
lyrics, err := l.fetchFromServer(name, artist, album, durationSecs)
if err != nil {
return nil, err
}
@@ -56,8 +64,7 @@ func FetchLrcLibLyricsCached(name, artist, album string, durationSecs int, cache
return lyrics, nil
}
// FetchLrcLibLyrics is a static function to search and fetch lyrics from lrclib.net
func FetchLrcLibLyrics(name, artist, album string, durationSecs int) (*mediaprovider.Lyrics, error) {
func (l *LrcLibFetcher) fetchFromServer(name, artist, album string, durationSecs int) (*mediaprovider.Lyrics, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()