This commit is contained in:
54
internal/service/cover_svc.go
Normal file
54
internal/service/cover_svc.go
Normal 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])
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user