实现封面缓存
All checks were successful
Go CI / test-and-build (push) Successful in 2m3s

This commit is contained in:
2026-04-08 20:56:18 +08:00
parent 4bc160284d
commit 169b2c1858
2 changed files with 21 additions and 12 deletions

View File

@@ -1,13 +1,10 @@
package service package service
import ( import (
"butterfliu/config"
"butterfliu/internal/model" "butterfliu/internal/model"
"butterfliu/internal/repository" "butterfliu/internal/repository"
"butterfliu/internal/util" "butterfliu/internal/util"
"errors" "errors"
"path"
"strconv"
) )
type CoverService struct { type CoverService struct {
@@ -37,9 +34,11 @@ func (s *CoverService) GetSongCover(id int) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
util.ExtractCover(file.Path, id) cover, err := util.ExtractCover(file.Path, id)
conf := config.LoadConfig() if err != nil {
return path.Join(conf.GetCachePath("cover"), strconv.Itoa(id)+".jpg"), nil return "", err
}
return cover, nil
} }
func (s *CoverService) GetAlbumCover(id int) (string, error) { func (s *CoverService) GetAlbumCover(id int) (string, error) {

View File

@@ -1,20 +1,30 @@
package util package util
import ( import (
"butterfliu/config"
"os"
"os/exec" "os/exec"
"path"
"strconv" "strconv"
) )
func ExtractCover(filePath string, id int) bool { func ExtractCover(filePath string, id int) (string, error) {
if _, err := exec.LookPath("ffmpeg"); err != nil { config := config.LoadConfig()
return false coverPath := path.Join(config.GetCachePath("cover"), strconv.Itoa(id)+".jpg")
_, err := os.Stat(coverPath)
if err == nil {
return coverPath, nil
}
if _, err := exec.LookPath("ffmpeg"); err != nil {
return "", err
} }
coverPath := "cache/cover/" + strconv.Itoa(id) + ".jpg"
cmd := exec.Command("ffmpeg", "-i", filePath, "-an", "-vcodec", "mjpeg", "-q:v", "5", "-f", "image2", "-y", coverPath) cmd := exec.Command("ffmpeg", "-i", filePath, "-an", "-vcodec", "mjpeg", "-q:v", "5", "-f", "image2", "-y", coverPath)
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
return false return "", err
} }
return true return coverPath, nil
} }