wails-player/session.go

110 lines
2.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 15:48:50 +00:00
"sync"
2024-05-27 22:13:13 +00:00
"github.com/go-resty/resty/v2"
)
type AuthError struct {
Error string `json:"error"`
}
2024-05-29 22:19:47 +00:00
// Name of the app, used to create directories in XDG_<folder> and store files inside
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
}
2024-05-29 21:25:25 +00:00
}