提取cover服务
All checks were successful
Go CI / test-and-build (push) Successful in 11s

This commit is contained in:
2026-04-08 20:44:04 +08:00
parent 55c017f60d
commit 4bc160284d
6 changed files with 88 additions and 60 deletions

View File

@@ -0,0 +1,54 @@
package service
import (
"butterfliu/config"
"butterfliu/internal/model"
"butterfliu/internal/repository"
"butterfliu/internal/util"
"errors"
"path"
"strconv"
)
type CoverService struct {
mediaRepo *repository.MediaRepository
songRepo *repository.SongRepository
albumRepo *repository.AlbumRepository
}
func NewCoverService(mediaRepo *repository.MediaRepository, songRepo *repository.SongRepository, albumRepo *repository.AlbumRepository) *CoverService {
return &CoverService{
mediaRepo: mediaRepo,
songRepo: songRepo,
albumRepo: albumRepo,
}
}
func (s *CoverService) getMediaFile(id int) (model.MediaFile, error) {
song, err := s.songRepo.Get(id)
if err != nil {
return model.MediaFile{}, err
}
return s.mediaRepo.Get(song.MediaFileID)
}
func (s *CoverService) GetSongCover(id int) (string, error) {
file, err := s.getMediaFile(id)
if err != nil {
return "", err
}
util.ExtractCover(file.Path, id)
conf := config.LoadConfig()
return path.Join(conf.GetCachePath("cover"), strconv.Itoa(id)+".jpg"), nil
}
func (s *CoverService) GetAlbumCover(id int) (string, error) {
songs, err := s.albumRepo.GetSongIDs(id)
if err != nil {
return "", err
}
if len(songs) == 0 {
return "", errors.New("no songs found for album")
}
return s.GetSongCover(songs[0])
}

View File

@@ -1,16 +1,13 @@
package service
import (
"butterfliu/config"
"butterfliu/internal/model"
"butterfliu/internal/repository"
"butterfliu/internal/scanner"
"errors"
"fmt"
"log"
"path"
"path/filepath"
"strconv"
"strings"
)
@@ -23,7 +20,7 @@ type ScanReport struct {
}
type LibraryService struct {
libRepo *repository.LibraryRepository
libRepo *repository.LibraryRepository
artistRepo *repository.ArtistRepository
albumRepo *repository.AlbumRepository
songRepo *repository.SongRepository
@@ -169,18 +166,6 @@ func (s *LibraryService) GetAlbum(id int) (model.AlbumDetail, error) {
}, nil
}
func (s *LibraryService) GetAlbumCover(id int) (string, error) {
songs, err := s.albumRepo.GetSongIDs(id)
if err != nil {
return "", err
}
if len(songs) == 0 {
return "", errors.New("no songs found for album")
}
conf := config.LoadConfig()
return path.Join(conf.GetCachePath("cover"), strconv.Itoa(songs[0])+".jpg"), nil
}
func (s *LibraryService) Scan(id int) (*ScanReport, error) {
lib, err := s.libRepo.GetByID(id)
if err != nil {

View File

@@ -1,12 +1,8 @@
package service
import (
"butterfliu/config"
"butterfliu/internal/model"
"butterfliu/internal/repository"
"butterfliu/internal/util"
"path"
"strconv"
)
type SongService struct {
@@ -37,13 +33,3 @@ func (s *SongService) GetMediaFile(id int) (model.MediaFile, error) {
}
return s.mediaRepo.Get(song.MediaFileID)
}
func (s *SongService) GetCover(id int) (string, error) {
file, err := s.GetMediaFile(id)
if err != nil {
return "", err
}
util.ExtractCover(file.Path, id)
conf := config.LoadConfig()
return path.Join(conf.GetCachePath("cover"), strconv.Itoa(id)+".jpg"), nil
}