实现音频服务api
This commit is contained in:
44
internal/controller/song.go
Normal file
44
internal/controller/song.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user