This commit is contained in:
@@ -12,17 +12,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type LibraryController struct {
|
type LibraryController struct {
|
||||||
service *service.LibraryService
|
libraryService *service.LibraryService
|
||||||
|
coverService *service.CoverService
|
||||||
scanMutex sync.Mutex
|
scanMutex sync.Mutex
|
||||||
isScanning bool
|
isScanning bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLibraryController(service *service.LibraryService) *LibraryController {
|
func NewLibraryController(libraryService *service.LibraryService, coverSvc *service.CoverService) *LibraryController {
|
||||||
return &LibraryController{service: service}
|
return &LibraryController{libraryService: libraryService, coverService: coverSvc}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *LibraryController) GetAll(w http.ResponseWriter, r *http.Request) {
|
func (c *LibraryController) GetAll(w http.ResponseWriter, r *http.Request) {
|
||||||
libraries, err := c.service.GetAll()
|
libraries, err := c.libraryService.GetAll()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jsonError(w, err.Error(), http.StatusInternalServerError)
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -43,7 +44,7 @@ func (c *LibraryController) Create(w http.ResponseWriter, r *http.Request) {
|
|||||||
jsonError(w, "name and path are required", http.StatusBadRequest)
|
jsonError(w, "name and path are required", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, err := c.service.Create(req.Name, req.Path)
|
_, err := c.libraryService.Create(req.Name, req.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jsonError(w, err.Error(), http.StatusInternalServerError)
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -58,7 +59,7 @@ func (c *LibraryController) GetByID(w http.ResponseWriter, r *http.Request) {
|
|||||||
jsonError(w, err.Error(), http.StatusBadRequest)
|
jsonError(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
l, err := c.service.GetByID(id)
|
l, err := c.libraryService.GetByID(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jsonError(w, err.Error(), http.StatusInternalServerError)
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -80,7 +81,7 @@ func (c *LibraryController) UpdateName(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = c.service.UpdateName(id, name)
|
err = c.libraryService.UpdateName(id, name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jsonError(w, err.Error(), http.StatusInternalServerError)
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -102,7 +103,7 @@ func (c *LibraryController) UpdatePath(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = c.service.UpdatePath(id, path)
|
err = c.libraryService.UpdatePath(id, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jsonError(w, err.Error(), http.StatusInternalServerError)
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -137,7 +138,7 @@ func (c *LibraryController) Scan(w http.ResponseWriter, r *http.Request) {
|
|||||||
c.scanMutex.Unlock()
|
c.scanMutex.Unlock()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
report, err := c.service.Scan(id)
|
report, err := c.libraryService.Scan(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Scan failed for library %d: %v", id, err)
|
log.Printf("Scan failed for library %d: %v", id, err)
|
||||||
return
|
return
|
||||||
@@ -163,7 +164,7 @@ func (c *LibraryController) Delete(w http.ResponseWriter, r *http.Request) {
|
|||||||
jsonError(w, err.Error(), http.StatusBadRequest)
|
jsonError(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = c.service.Delete(id)
|
err = c.libraryService.Delete(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jsonError(w, err.Error(), http.StatusInternalServerError)
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -178,7 +179,7 @@ func (c *LibraryController) GetSongs(w http.ResponseWriter, r *http.Request) {
|
|||||||
jsonError(w, err.Error(), http.StatusBadRequest)
|
jsonError(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
songs, err := c.service.GetSongs(id)
|
songs, err := c.libraryService.GetSongs(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jsonError(w, err.Error(), http.StatusInternalServerError)
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -193,7 +194,7 @@ func (c *LibraryController) GetSongsByArtist(w http.ResponseWriter, r *http.Requ
|
|||||||
jsonError(w, err.Error(), http.StatusBadRequest)
|
jsonError(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
songs, err := c.service.GetSongsByArtist(id)
|
songs, err := c.libraryService.GetSongsByArtist(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jsonError(w, err.Error(), http.StatusInternalServerError)
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -208,7 +209,7 @@ func (c *LibraryController) GetSongsByAlbum(w http.ResponseWriter, r *http.Reque
|
|||||||
jsonError(w, err.Error(), http.StatusBadRequest)
|
jsonError(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
songs, err := c.service.GetSongsByAlbum(id)
|
songs, err := c.libraryService.GetSongsByAlbum(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jsonError(w, err.Error(), http.StatusInternalServerError)
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -217,7 +218,7 @@ func (c *LibraryController) GetSongsByAlbum(w http.ResponseWriter, r *http.Reque
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *LibraryController) GetArtists(w http.ResponseWriter, r *http.Request) {
|
func (c *LibraryController) GetArtists(w http.ResponseWriter, r *http.Request) {
|
||||||
artists, err := c.service.GetArtists()
|
artists, err := c.libraryService.GetArtists()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jsonError(w, err.Error(), http.StatusInternalServerError)
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -232,7 +233,7 @@ func (c *LibraryController) GetArtist(w http.ResponseWriter, r *http.Request) {
|
|||||||
jsonError(w, err.Error(), http.StatusBadRequest)
|
jsonError(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
artist, err := c.service.GetArtist(id)
|
artist, err := c.libraryService.GetArtist(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jsonError(w, err.Error(), http.StatusInternalServerError)
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -241,7 +242,7 @@ func (c *LibraryController) GetArtist(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *LibraryController) GetAlbums(w http.ResponseWriter, r *http.Request) {
|
func (c *LibraryController) GetAlbums(w http.ResponseWriter, r *http.Request) {
|
||||||
albums, err := c.service.GetAlbums()
|
albums, err := c.libraryService.GetAlbums()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jsonError(w, err.Error(), http.StatusInternalServerError)
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -256,7 +257,7 @@ func (c *LibraryController) GetAlbumsByArtist(w http.ResponseWriter, r *http.Req
|
|||||||
jsonError(w, err.Error(), http.StatusBadRequest)
|
jsonError(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
albums, err := c.service.GetAlbumsByArtistWithDetail(id)
|
albums, err := c.libraryService.GetAlbumsByArtistWithDetail(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jsonError(w, err.Error(), http.StatusInternalServerError)
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -271,7 +272,7 @@ func (c *LibraryController) GetAlbum(w http.ResponseWriter, r *http.Request) {
|
|||||||
jsonError(w, err.Error(), http.StatusBadRequest)
|
jsonError(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
album, err := c.service.GetAlbum(id)
|
album, err := c.libraryService.GetAlbum(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jsonError(w, err.Error(), http.StatusInternalServerError)
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -286,7 +287,7 @@ func (c *LibraryController) GetAlbumCover(w http.ResponseWriter, r *http.Request
|
|||||||
jsonError(w, err.Error(), http.StatusBadRequest)
|
jsonError(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
coverPath, err := c.service.GetAlbumCover(id)
|
coverPath, err := c.coverService.GetAlbumCover(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jsonError(w, err.Error(), http.StatusInternalServerError)
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -9,15 +9,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type SongController struct {
|
type SongController struct {
|
||||||
service *service.SongService
|
songService *service.SongService
|
||||||
|
coverService *service.CoverService
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSongController(service *service.SongService) *SongController {
|
func NewSongController(songService *service.SongService, coverService *service.CoverService) *SongController {
|
||||||
return &SongController{service: service}
|
return &SongController{songService: songService, coverService: coverService}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *SongController) GetAllWithDetails(w http.ResponseWriter, r *http.Request) {
|
func (c *SongController) GetAllWithDetails(w http.ResponseWriter, r *http.Request) {
|
||||||
songs, err := c.service.GetAllWithDetails()
|
songs, err := c.songService.GetAllWithDetails()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jsonError(w, err.Error(), http.StatusInternalServerError)
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -32,7 +33,7 @@ func (c *SongController) GetByIDWithDetails(w http.ResponseWriter, r *http.Reque
|
|||||||
http.Error(w, "无效的歌曲 ID", http.StatusBadRequest)
|
http.Error(w, "无效的歌曲 ID", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
song, err := c.service.GetByIDWithDetails(id)
|
song, err := c.songService.GetByIDWithDetails(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jsonError(w, err.Error(), http.StatusInternalServerError)
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -48,7 +49,7 @@ func (c *SongController) Stream(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
mediaFile, err := c.service.GetMediaFile(id)
|
mediaFile, err := c.songService.GetMediaFile(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "未找到该媒体文件", http.StatusNotFound)
|
http.Error(w, "未找到该媒体文件", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
@@ -64,7 +65,7 @@ func (c *SongController) Cover(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, "无效的歌曲 ID", http.StatusBadRequest)
|
http.Error(w, "无效的歌曲 ID", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
coverPath, err := c.service.GetCover(id)
|
coverPath, err := c.coverService.GetSongCover(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "获取封面异常", http.StatusNotFound)
|
http.Error(w, "获取封面异常", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
|
|||||||
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
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"butterfliu/config"
|
|
||||||
"butterfliu/internal/model"
|
"butterfliu/internal/model"
|
||||||
"butterfliu/internal/repository"
|
"butterfliu/internal/repository"
|
||||||
"butterfliu/internal/scanner"
|
"butterfliu/internal/scanner"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -169,18 +166,6 @@ func (s *LibraryService) GetAlbum(id int) (model.AlbumDetail, error) {
|
|||||||
}, nil
|
}, 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) {
|
func (s *LibraryService) Scan(id int) (*ScanReport, error) {
|
||||||
lib, err := s.libRepo.GetByID(id)
|
lib, err := s.libRepo.GetByID(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -1,12 +1,8 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"butterfliu/config"
|
|
||||||
"butterfliu/internal/model"
|
"butterfliu/internal/model"
|
||||||
"butterfliu/internal/repository"
|
"butterfliu/internal/repository"
|
||||||
"butterfliu/internal/util"
|
|
||||||
"path"
|
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type SongService struct {
|
type SongService struct {
|
||||||
@@ -37,13 +33,3 @@ func (s *SongService) GetMediaFile(id int) (model.MediaFile, error) {
|
|||||||
}
|
}
|
||||||
return s.mediaRepo.Get(song.MediaFileID)
|
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
|
|
||||||
}
|
|
||||||
|
|||||||
5
main.go
5
main.go
@@ -29,8 +29,9 @@ func main() {
|
|||||||
mediaRepo := repository.NewMediaRepository(GetDB())
|
mediaRepo := repository.NewMediaRepository(GetDB())
|
||||||
libraryService := service.NewLibraryService(libraryRepo, artistRepo, albumRepo, songRepo, mediaRepo)
|
libraryService := service.NewLibraryService(libraryRepo, artistRepo, albumRepo, songRepo, mediaRepo)
|
||||||
songService := service.NewSongService(songRepo, mediaRepo)
|
songService := service.NewSongService(songRepo, mediaRepo)
|
||||||
libraryController := controller.NewLibraryController(libraryService)
|
coverService := service.NewCoverService(mediaRepo, songRepo, albumRepo)
|
||||||
songController := controller.NewSongController(songService)
|
libraryController := controller.NewLibraryController(libraryService, coverService)
|
||||||
|
songController := controller.NewSongController(songService, coverService)
|
||||||
|
|
||||||
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte("Hello World!"))
|
w.Write([]byte("Hello World!"))
|
||||||
|
|||||||
Reference in New Issue
Block a user