package mpd import ( "bytes" "image" "strings" "github.com/sirupsen/logrus" "github.com/BeatGlow/beatglow/player" _ "image/jpeg" _ "image/png" ) type artRequest struct { Path string Art chan image.Image } func (d *Player) AlbumArt(track *player.Track) (image.Image, bool) { if track == nil || track.Path == "" { return nil, false } if cached, ok := d.artCache.Load(track.Path); ok { return cached.(image.Image), true } // Not in cache, request artwork through the art connection. art := make(chan image.Image) d.artRequest <- &artRequest{Path: track.Path, Art: art} // Respond with image. if image := <-art; image != nil { return image, true } return nil, false } func (d *Player) artLoop(conn *clientConn) { loop: for { select { case request := <-d.artRequest: log := conn.log().WithFields(logrus.Fields{ "path": request.Path, }) log.Debug("requesting album art") var ( offset, size int b, chunk []byte i image.Image err error ) for { if chunk, size, err = d.sendBinaryCommand(conn, "albumart %q %d", request.Path, offset); err != nil { request.Art <- nil if !strings.Contains(err.Error(), "No file exists") { log.WithError(err).Error("error reading album art") break loop } log.WithError(err).Debug("no album art found") continue loop } b = append(b, chunk...) offset = len(b) if offset >= size { break } } if i, _, err = image.Decode(bytes.NewBuffer(b)); err != nil { request.Art <- nil log.WithError(err).Warn("image corrupt?") log.WithField("data", string(b)).Debug("image data") continue loop } request.Art <- i case <-d.quit: return } } }