Files
butterfliu/internal/service/song_svc.go
lzw-723 55c017f60d
All checks were successful
Go CI / test-and-build (push) Successful in 11s
拆分LibraryRepository
2026-04-08 14:42:52 +08:00

50 lines
1.2 KiB
Go

package service
import (
"butterfliu/config"
"butterfliu/internal/model"
"butterfliu/internal/repository"
"butterfliu/internal/util"
"path"
"strconv"
)
type SongService struct {
songRepo *repository.SongRepository
mediaRepo *repository.MediaRepository
}
func NewSongService(songRepo *repository.SongRepository, mediaRepo *repository.MediaRepository) *SongService {
return &SongService{
songRepo: songRepo,
mediaRepo: mediaRepo,
}
}
func (s *SongService) GetAllWithDetails() ([]model.SongDetail, error) {
return s.songRepo.GetAllWithDetails()
}
func (s *SongService) GetByIDWithDetails(id int) (model.SongDetail, error) {
return s.songRepo.GetWithDetails(id)
}
// GetMediaFilePath returns the file path of a song by its song ID.
func (s *SongService) 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 *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
}