50 lines
1.2 KiB
Go
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() ([]repository.SongDetail, error) {
|
|
return s.songRepo.GetAllWithDetails()
|
|
}
|
|
|
|
func (s *SongService) GetByIDWithDetails(id int) (repository.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
|
|
}
|