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) }