add album art caching
This commit is contained in:
+42
-3
@@ -1,36 +1,75 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/20after4/configdir"
|
||||
"github.com/bluele/gcache"
|
||||
subsonic "github.com/dweymouth/go-subsonic"
|
||||
)
|
||||
|
||||
type ImageManager struct {
|
||||
s *subsonic.Client
|
||||
cacheDir string
|
||||
thumbnailCache gcache.Cache
|
||||
}
|
||||
|
||||
func NewImageManager(s *subsonic.Client) *ImageManager {
|
||||
func NewImageManager(s *subsonic.Client, cacheDir string) *ImageManager {
|
||||
cache := gcache.New(100).LRU().Build()
|
||||
if err := configdir.MakePath(cacheDir); err != nil {
|
||||
log.Println("failed to create album cover cache dir")
|
||||
cacheDir = ""
|
||||
}
|
||||
return &ImageManager{
|
||||
s: s,
|
||||
cacheDir: cacheDir,
|
||||
thumbnailCache: cache,
|
||||
}
|
||||
}
|
||||
|
||||
func (i *ImageManager) GetAlbumThumbnail(albumID string) (image.Image, error) {
|
||||
// in-memory cache
|
||||
if i.thumbnailCache.Has(albumID) {
|
||||
if img, err := i.thumbnailCache.Get(albumID); err == nil {
|
||||
return img.(image.Image), nil
|
||||
}
|
||||
}
|
||||
// TODO: on disc cache
|
||||
img, err := i.s.GetCoverArt(albumID, map[string]string{"size": "250"})
|
||||
|
||||
// on disc cache
|
||||
path := filepath.Join(i.cacheDir, fmt.Sprintf("%s.jpg", albumID))
|
||||
if i.cacheDir != "" {
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
// serve image from on-disc cache
|
||||
// TODO: image may have changed on server.
|
||||
// first, return cached image, then fetch fresh img from server in background
|
||||
if f, err := os.Open(path); err == nil {
|
||||
defer f.Close()
|
||||
if img, _, err := image.Decode(f); err == nil {
|
||||
i.thumbnailCache.Set(albumID, img)
|
||||
return img, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// fetch from server
|
||||
img, err := i.s.GetCoverArt(albumID, map[string]string{"size": "300"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if i.cacheDir != "" {
|
||||
if f, err := os.Create(path); err == nil {
|
||||
defer f.Close()
|
||||
if err := jpeg.Encode(f, img, nil /*options*/); err != nil {
|
||||
log.Printf("failed to cache image: %s", err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
i.thumbnailCache.Set(albumID, img)
|
||||
return img, nil
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ require (
|
||||
|
||||
require (
|
||||
fyne.io/systray v1.10.1-0.20220621085403-9a2652634e93 // indirect
|
||||
github.com/20after4/configdir v0.1.1 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/fredbi/uri v0.0.0-20181227131451-3dcfdacbaaf3 // indirect
|
||||
github.com/fsnotify/fsnotify v1.5.4 // indirect
|
||||
|
||||
@@ -39,6 +39,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9
|
||||
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
|
||||
fyne.io/systray v1.10.1-0.20220621085403-9a2652634e93 h1:V2IC9t0Zj9Ur6qDbfhUuzVmIvXKFyxZXRJyigUvovs4=
|
||||
fyne.io/systray v1.10.1-0.20220621085403-9a2652634e93/go.mod h1:oM2AQqGJ1AMo4nNqZFYU8xYygSBZkW2hmdJ7n4yjedE=
|
||||
github.com/20after4/configdir v0.1.1 h1:ylL5dO+aGxBV4jDtG9Ej9hZfeE8Fw9jULQVq0N+ErJ8=
|
||||
github.com/20after4/configdir v0.1.1/go.mod h1:kZ7yOiD6MFUABqBI2/N62QA1wyEB8sGeI+mygme1pl4=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
|
||||
|
||||
@@ -6,51 +6,43 @@ import (
|
||||
"gomuse/backend"
|
||||
"gomuse/player"
|
||||
"gomuse/ui"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"fyne.io/fyne/v2"
|
||||
"fyne.io/fyne/v2/app"
|
||||
"fyne.io/fyne/v2/container"
|
||||
"github.com/20after4/configdir"
|
||||
"github.com/dweymouth/go-subsonic"
|
||||
)
|
||||
|
||||
const appname = "gomuse"
|
||||
|
||||
func main() {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cachePath := configdir.LocalCache(appname)
|
||||
log.Println(cachePath)
|
||||
|
||||
myApp := app.New()
|
||||
myWindow := myApp.NewWindow("gomuse")
|
||||
|
||||
p := player.NewWithClientName("gomuse")
|
||||
p := player.NewWithClientName(appname)
|
||||
fmt.Println(p.Init())
|
||||
|
||||
s := &subsonic.Client{
|
||||
Client: &http.Client{},
|
||||
BaseUrl: "***REMOVED***",
|
||||
User: "drew",
|
||||
ClientName: "gomuse",
|
||||
ClientName: appname,
|
||||
}
|
||||
|
||||
lm := backend.NewLibraryManager(s)
|
||||
pm := backend.NewPlaybackManager(ctx, s, p)
|
||||
im := backend.NewImageManager(s)
|
||||
im := backend.NewImageManager(s, configdir.LocalCache(appname, "covers"))
|
||||
|
||||
if err := s.Authenticate("***REMOVED***"); err != nil {
|
||||
fmt.Printf("error authenticating: %v\n", err)
|
||||
}
|
||||
|
||||
/*
|
||||
if len(os.Args) > 1 {
|
||||
// search albums by args[1] and load first matching album
|
||||
log.Printf("Searching for %q\n", os.Args[1])
|
||||
if res, err := s.Search3(os.Args[1], map[string]string{}); err == nil {
|
||||
if len(res.Album) > 0 {
|
||||
log.Println("Got album search result")
|
||||
album := res.Album[0]
|
||||
pm.LoadAlbum(album.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
myApp := app.New()
|
||||
myWindow := myApp.NewWindow(appname)
|
||||
|
||||
b := ui.NewBottomPanel(p, pm, im)
|
||||
ag := ui.NewAlbumGrid(lm.RecentlyAddedIter(), pm, im)
|
||||
|
||||
+1
-1
@@ -64,7 +64,7 @@ func NewAlbumCard() *AlbumCard {
|
||||
a.title.Wrapping = fyne.TextTruncate
|
||||
a.artist.Wrapping = fyne.TextTruncate
|
||||
a.title.TextStyle = fyne.TextStyle{Bold: true}
|
||||
a.Cover.im.SetMinSize(fyne.NewSize(225, 225))
|
||||
a.Cover.im.SetMinSize(fyne.NewSize(200, 200))
|
||||
a.Cover.im.FillMode = canvas.ImageFillContain
|
||||
|
||||
a.createContainer()
|
||||
|
||||
Reference in New Issue
Block a user