Load & cache album covers
This commit is contained in:
parent
15ea565601
commit
beca906979
@ -11,10 +11,10 @@ export function Home() {
|
|||||||
setTimeout(() => setHidden(false), 150);
|
setTimeout(() => setHidden(false), 150);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div class={`min-h-screen ${hidden() ? "opacity-0" : "opacity-100"} transition-opacity`}>
|
<div class={`min-h-screen ${hidden() ? "opacity-0" : "opacity-100"} transition-opacity`}>
|
||||||
<div class="py-10 h-64 overflow-scroll whitespace-nowrap">
|
<h1 class="font-black text-2xl pt-6 pb-4 pl-2">Random albums</h1>
|
||||||
|
<div class="pb-4 overflow-scroll whitespace-nowrap">
|
||||||
<For each={albums()}>
|
<For each={albums()}>
|
||||||
{(album) => <Album album={album} />}
|
{(album) => <Album album={album} />}
|
||||||
</For>
|
</For>
|
||||||
|
1
go.mod
1
go.mod
@ -5,6 +5,7 @@ go 1.21
|
|||||||
toolchain go1.22.3
|
toolchain go1.22.3
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/adrg/xdg v0.4.0
|
||||||
github.com/go-resty/resty/v2 v2.13.1
|
github.com/go-resty/resty/v2 v2.13.1
|
||||||
github.com/wailsapp/wails/v2 v2.8.2
|
github.com/wailsapp/wails/v2 v2.8.2
|
||||||
)
|
)
|
||||||
|
3
go.sum
3
go.sum
@ -1,3 +1,5 @@
|
|||||||
|
github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls=
|
||||||
|
github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E=
|
||||||
github.com/bep/debounce v1.2.1 h1:v67fRdBA9UQu2NhLFXrSg0Brw7CexQekrBwDMM8bzeY=
|
github.com/bep/debounce v1.2.1 h1:v67fRdBA9UQu2NhLFXrSg0Brw7CexQekrBwDMM8bzeY=
|
||||||
github.com/bep/debounce v1.2.1/go.mod h1:H8yggRPQKLUhUoqrJC1bO2xNya7vanpDl7xR3ISbCJ0=
|
github.com/bep/debounce v1.2.1/go.mod h1:H8yggRPQKLUhUoqrJC1bO2xNya7vanpDl7xR3ISbCJ0=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
@ -96,6 +98,7 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
|
|||||||
golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
62
session.go
62
session.go
@ -4,8 +4,10 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/adrg/xdg"
|
||||||
"github.com/go-resty/resty/v2"
|
"github.com/go-resty/resty/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -13,14 +15,19 @@ type AuthError struct {
|
|||||||
Error string `json:"error"`
|
Error string `json:"error"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const appname = "wMusic"
|
||||||
|
|
||||||
var LoggedUser AuthSuccess
|
var LoggedUser AuthSuccess
|
||||||
var randomAlbumWaitGroup sync.WaitGroup
|
var randomAlbumWaitGroup sync.WaitGroup
|
||||||
var randomAlbums []Album
|
var randomAlbums []Album
|
||||||
|
|
||||||
|
var serverUrl = ""
|
||||||
|
var client = resty.New()
|
||||||
|
|
||||||
// (Tries to) login to a remote navidrome server
|
// (Tries to) login to a remote navidrome server
|
||||||
func (a *App) Login(server, username, password string) (bool, error) {
|
func (a *App) Login(server, username, password string) (bool, error) {
|
||||||
log.Print("begin Login to server")
|
log.Print("begin Login to server")
|
||||||
client := resty.New()
|
// client := resty.New()
|
||||||
|
|
||||||
// TODO: check server for leading https and trailing /, normalize
|
// TODO: check server for leading https and trailing /, normalize
|
||||||
|
|
||||||
@ -42,6 +49,10 @@ func (a *App) Login(server, username, password string) (bool, error) {
|
|||||||
if response.IsSuccess() {
|
if response.IsSuccess() {
|
||||||
log.Printf("%+v", successData)
|
log.Printf("%+v", successData)
|
||||||
|
|
||||||
|
// Set the auth header globally
|
||||||
|
client.SetHeader("x-nd-authorization", successData.Token)
|
||||||
|
serverUrl = server
|
||||||
|
|
||||||
// Set global session
|
// Set global session
|
||||||
LoggedUser = successData
|
LoggedUser = successData
|
||||||
// Begin to load the list of albums on the background
|
// Begin to load the list of albums on the background
|
||||||
@ -89,7 +100,56 @@ func loadAlbums(serverUrl string) {
|
|||||||
log.Print("Get albums success")
|
log.Print("Get albums success")
|
||||||
// TODO: Begin to load album artwork in the background
|
// TODO: Begin to load album artwork in the background
|
||||||
// Album artwork comes from the url /rest/getCoverArt.view
|
// Album artwork comes from the url /rest/getCoverArt.view
|
||||||
|
// Cache album images in XDG_CACHE_HOME
|
||||||
|
for _, album := range randomAlbums {
|
||||||
|
albumId := album.ID
|
||||||
|
|
||||||
|
go loadAlbumCover(albumId)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Do the loading
|
// TODO: Do the loading
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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")
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user