新增 migration 0021(background_key/url)、playlist/CRUD 返回 backgroundUrl、
POST .../music/{id}/background,并热更 popadmin 音乐列表背景上传;公读策略放行 perm/music/*。
Co-authored-by: Cursor <cursoragent@cursor.com>
207 lines
4.5 KiB
Go
207 lines
4.5 KiB
Go
package adminhandler
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
musiclogic "spark-server/internal/module/music/logic"
|
|
"spark-server/internal/pkg/errs"
|
|
)
|
|
|
|
// MusicHandler serves popadmin music CRUD (Java-style ApiResponse).
|
|
type MusicHandler struct {
|
|
svc *musiclogic.Service
|
|
}
|
|
|
|
func NewMusic(svc *musiclogic.Service) *MusicHandler {
|
|
return &MusicHandler{svc: svc}
|
|
}
|
|
|
|
func (h *MusicHandler) ready(c *gin.Context) bool {
|
|
if h == nil || h.svc == nil || !h.svc.Ready() {
|
|
Fail(c, http.StatusServiceUnavailable, "music 服务未就绪")
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (h *MusicHandler) List(c *gin.Context) {
|
|
if !h.ready(c) {
|
|
return
|
|
}
|
|
list, err := h.svc.ListAdmin(c.Request.Context())
|
|
if err != nil {
|
|
Fail(c, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
settings, err := h.svc.Settings(c.Request.Context())
|
|
if err != nil {
|
|
Fail(c, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
OK(c, gin.H{"items": list, "settings": settings})
|
|
}
|
|
|
|
func (h *MusicHandler) GetSettings(c *gin.Context) {
|
|
if !h.ready(c) {
|
|
return
|
|
}
|
|
out, err := h.svc.Settings(c.Request.Context())
|
|
if err != nil {
|
|
Fail(c, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
OK(c, out)
|
|
}
|
|
|
|
func (h *MusicHandler) PutSettings(c *gin.Context) {
|
|
if !h.ready(c) {
|
|
return
|
|
}
|
|
var body struct {
|
|
MemberPreviewSeconds int `json:"memberPreviewSeconds"`
|
|
}
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
Fail(c, http.StatusBadRequest, "无效 JSON")
|
|
return
|
|
}
|
|
out, err := h.svc.UpdateSettings(c.Request.Context(), body.MemberPreviewSeconds)
|
|
if err != nil {
|
|
failMusic(c, err)
|
|
return
|
|
}
|
|
OK(c, out)
|
|
}
|
|
|
|
func (h *MusicHandler) Create(c *gin.Context) {
|
|
if !h.ready(c) {
|
|
return
|
|
}
|
|
var in musiclogic.UpsertInput
|
|
if err := c.ShouldBindJSON(&in); err != nil {
|
|
Fail(c, http.StatusBadRequest, "无效 JSON")
|
|
return
|
|
}
|
|
out, err := h.svc.Create(c.Request.Context(), in)
|
|
if err != nil {
|
|
failMusic(c, err)
|
|
return
|
|
}
|
|
OK(c, out)
|
|
}
|
|
|
|
func (h *MusicHandler) Update(c *gin.Context) {
|
|
if !h.ready(c) {
|
|
return
|
|
}
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil || id <= 0 {
|
|
Fail(c, http.StatusBadRequest, "无效 id")
|
|
return
|
|
}
|
|
var in musiclogic.UpsertInput
|
|
if err := c.ShouldBindJSON(&in); err != nil {
|
|
Fail(c, http.StatusBadRequest, "无效 JSON")
|
|
return
|
|
}
|
|
out, err := h.svc.Update(c.Request.Context(), id, in)
|
|
if err != nil {
|
|
failMusic(c, err)
|
|
return
|
|
}
|
|
OK(c, out)
|
|
}
|
|
|
|
func (h *MusicHandler) Delete(c *gin.Context) {
|
|
if !h.ready(c) {
|
|
return
|
|
}
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil || id <= 0 {
|
|
Fail(c, http.StatusBadRequest, "无效 id")
|
|
return
|
|
}
|
|
if err := h.svc.Delete(c.Request.Context(), id); err != nil {
|
|
failMusic(c, err)
|
|
return
|
|
}
|
|
OK(c, nil)
|
|
}
|
|
|
|
func (h *MusicHandler) UploadCover(c *gin.Context) {
|
|
h.upload(c, "cover")
|
|
}
|
|
|
|
func (h *MusicHandler) UploadBackground(c *gin.Context) {
|
|
h.upload(c, "background")
|
|
}
|
|
|
|
func (h *MusicHandler) UploadAudio(c *gin.Context) {
|
|
h.upload(c, "audio")
|
|
}
|
|
|
|
func (h *MusicHandler) upload(c *gin.Context, kind string) {
|
|
if !h.ready(c) {
|
|
return
|
|
}
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil || id <= 0 {
|
|
Fail(c, http.StatusBadRequest, "无效 id")
|
|
return
|
|
}
|
|
file, hdr, err := c.Request.FormFile("file")
|
|
if err != nil {
|
|
Fail(c, http.StatusBadRequest, "请用 multipart 字段 file 上传")
|
|
return
|
|
}
|
|
defer file.Close()
|
|
body, err := io.ReadAll(io.LimitReader(file, 51<<20))
|
|
if err != nil {
|
|
Fail(c, http.StatusBadRequest, "读取文件失败")
|
|
return
|
|
}
|
|
ct := hdr.Header.Get("Content-Type")
|
|
var out any
|
|
switch kind {
|
|
case "cover":
|
|
out, err = h.svc.UploadCover(c.Request.Context(), id, hdr.Filename, body, ct)
|
|
case "background":
|
|
out, err = h.svc.UploadBackground(c.Request.Context(), id, hdr.Filename, body, ct)
|
|
default:
|
|
out, err = h.svc.UploadAudio(c.Request.Context(), id, hdr.Filename, body, ct)
|
|
}
|
|
if err != nil {
|
|
failMusic(c, err)
|
|
return
|
|
}
|
|
OK(c, out)
|
|
}
|
|
|
|
func failMusic(c *gin.Context, err error) {
|
|
if err == nil {
|
|
return
|
|
}
|
|
if errors.Is(err, musiclogic.ErrNotReady) {
|
|
Fail(c, http.StatusServiceUnavailable, err.Error())
|
|
return
|
|
}
|
|
if app, ok := errs.AsAppError(err); ok {
|
|
switch app.Code {
|
|
case errs.ErrNotFound.Code:
|
|
Fail(c, http.StatusNotFound, app.Msg)
|
|
case errs.ErrInvalidArgument.Code:
|
|
Fail(c, http.StatusBadRequest, app.Msg)
|
|
case errs.ErrAvailable.Code:
|
|
Fail(c, http.StatusServiceUnavailable, app.Msg)
|
|
default:
|
|
Fail(c, http.StatusBadRequest, app.Msg)
|
|
}
|
|
return
|
|
}
|
|
Fail(c, http.StatusInternalServerError, err.Error())
|
|
}
|