wails-player/session.go

156 lines
3.7 KiB
Go
Raw Normal View History

2024-05-27 22:13:13 +00:00
package main
import (
2024-05-27 23:22:42 +00:00
"errors"
2024-05-27 22:13:13 +00:00
"fmt"
"log"
2024-05-29 21:25:25 +00:00
"os"
2024-05-29 15:48:50 +00:00
"sync"
2024-05-27 22:13:13 +00:00
2024-05-29 21:25:25 +00:00
"github.com/adrg/xdg"
2024-05-27 22:13:13 +00:00
"github.com/go-resty/resty/v2"
)
type AuthError struct {
Error string `json:"error"`
}
2024-05-29 21:25:25 +00:00
const appname = "wMusic"
2024-05-29 15:48:50 +00:00
var LoggedUser AuthSuccess
var randomAlbumWaitGroup sync.WaitGroup
var randomAlbums []Album
2024-05-29 21:25:25 +00:00
var serverUrl = ""
var client = resty.New()
2024-05-27 23:22:42 +00:00
// (Tries to) login to a remote navidrome server
func (a *App) Login(server, username, password string) (bool, error) {
2024-05-29 15:48:50 +00:00
log.Print("begin Login to server")
2024-05-29 21:25:25 +00:00
// client := resty.New()
2024-05-27 22:13:13 +00:00
2024-05-29 15:48:50 +00:00
// TODO: check server for leading https and trailing /, normalize
2024-05-27 22:13:13 +00:00
successData := AuthSuccess{}
errorData := AuthError{}
response, err := client.R().
SetHeader("Content-Type", "").
SetBody(fmt.Sprintf(`{"username":"%s","password":"%s"}`, username, password)).
SetResult(&successData).
SetError(&errorData).
Post(fmt.Sprintf("%s/auth/login", server))
if err != nil {
log.Print("Login error", err)
2024-05-27 23:22:42 +00:00
return false, err
2024-05-27 22:13:13 +00:00
}
if response.IsSuccess() {
log.Printf("%+v", successData)
2024-05-29 15:48:50 +00:00
2024-05-29 21:25:25 +00:00
// Set the auth header globally
client.SetHeader("x-nd-authorization", successData.Token)
serverUrl = server
2024-05-29 15:48:50 +00:00
// Set global session
LoggedUser = successData
// Begin to load the list of albums on the background
randomAlbumWaitGroup.Add(1)
go loadAlbums(server)
2024-05-27 23:22:42 +00:00
return true, nil
2024-05-27 22:13:13 +00:00
} else if response.IsError() {
log.Printf("%+v", errorData)
2024-05-27 23:22:42 +00:00
return false, errors.New(errorData.Error + ".")
} else {
log.Printf("ehhh???")
return false, errors.New("invalid state")
2024-05-27 22:13:13 +00:00
}
}
2024-05-29 15:48:50 +00:00
// Waits for the random albums to be loaded, and returns them.
// This function assumes that the random albums are being loaded in the background.
func (a *App) GetRandomAlbums() ([]Album, error) {
log.Printf("Waiting for loading group...")
randomAlbumWaitGroup.Wait()
log.Printf("Waiting group finished")
return randomAlbums, nil
}
// Loads a list of random albums from the server.
func loadAlbums(serverUrl string) {
defer randomAlbumWaitGroup.Done()
log.Print("begin loadAlbums")
client := resty.New()
var errorData AuthError
response, err := client.R().
SetHeader("x-nd-authorization", fmt.Sprintf("Bearer %s", LoggedUser.Token)).
SetResult(&randomAlbums).
SetError(&errorData).
Get(fmt.Sprintf("%s/api/album?_end=20&_order=DESC&_sort=random&_start=0", serverUrl))
if err != nil {
log.Print("Get albums error")
}
if response.IsSuccess() {
log.Print("Get albums success")
// TODO: Begin to load album artwork in the background
// Album artwork comes from the url /rest/getCoverArt.view
2024-05-29 21:25:25 +00:00
// Cache album images in XDG_CACHE_HOME
for _, album := range randomAlbums {
albumId := album.ID
go loadAlbumCover(albumId)
}
2024-05-29 15:48:50 +00:00
}
// TODO: Do the loading
}
2024-05-29 21:25:25 +00:00
// Loads a single album cover and caches it in XDG_CACHE_HOME
func loadAlbumCover(albumId string) {
log.Print("Loading albumCover for ", albumId)
response, err := client.R().
// TODO: replace `fernando` with the username
Get(fmt.Sprintf(
"%s/rest/getCoverArt.view?id=%s&u=fernando&s=12e7f3&t=%s&v=1.13.0&c=wmusic&size=300",
serverUrl,
albumId,
"d7bbe92d7da363aa202ae16136887adc",
))
if err != nil {
log.Print("error loadAlbumCover: ", err)
return
}
if !response.IsSuccess() {
log.Print("error loadAlbumCover")
log.Printf("%s", response.Body())
return
}
imgBytes := response.Body()
// Write the image to cache
// TODO: Actually check in cache if the album art exists
cacheFile, err := xdg.CacheFile(fmt.Sprintf("%s/%s", appname, albumId))
if err != nil {
log.Print("error loadAlbumCover - CacheFile:", err)
return
}
err = os.WriteFile(cacheFile, imgBytes, 0666)
if err != nil {
panic(fmt.Sprintf("Error writing to cache file for album cover: %s", cacheFile))
}
log.Print("Loading albumCover for ", albumId, " successful")
}