实现音频服务api

This commit is contained in:
2026-01-09 23:33:14 +08:00
parent aae6a08850
commit 0e72dc7b6a
10 changed files with 240 additions and 150 deletions

View File

@@ -0,0 +1,44 @@
package controller
import (
"butterfliu/internal/service"
"encoding/json"
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
)
type SongController struct {
service *service.SongService
}
func NewSongController(service *service.SongService) *SongController {
return &SongController{service: service}
}
func (c *SongController) GetAllWithDetails(w http.ResponseWriter, r *http.Request) {
songs, err := c.service.GetAllWithDetails()
if err != nil {
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
return
}
json.NewEncoder(w).Encode(songs)
}
func (c *SongController) Stream(w http.ResponseWriter, r *http.Request) {
parmaID := chi.URLParam(r, "id")
id, err := strconv.Atoi(parmaID)
if err != nil {
http.Error(w, "无效的歌曲 ID", http.StatusBadRequest)
return
}
mediaFile, err := c.service.GetMediaFile(id)
if err != nil {
http.Error(w, "未找到该媒体文件", http.StatusNotFound)
return
}
http.ServeFile(w, r, mediaFile.Path)
}