Files
2026-06-12 15:50:01 +08:00

68 lines
1.6 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package handler
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/luxsin/app-api/internal/response"
"github.com/luxsin/app-api/internal/search"
"github.com/luxsin/app-api/pkg/encode"
"go.uber.org/zap"
)
type ModelListHandler struct {
search *search.Client
log *zap.Logger
}
func NewModelListHandler(searchClient *search.Client, log *zap.Logger) *ModelListHandler {
return &ModelListHandler{
search: searchClient,
log: log,
}
}
// ModelList 搜索型号列表
//
// @Summary 搜索型号列表(基于 Meilisearch
// @Tags Model
// @Produce json
// @Param key query string false "搜索关键词"
// @Param count query int false "返回数量上限"default(100)
// @Param base64Resp query string false "是否返回 base64 编码响应"
// @Success 200 {array} string
// @Failure 500 {object} object
// @Router /audio/modelList [get]
func (h *ModelListHandler) ModelList(c *gin.Context) {
key := c.Query("key")
base64Resp := encode.ParseBase64Param(c)
count := 100
if v := c.Query("count"); v != "" {
if n, err := strconv.Atoi(v); err == nil && n > 0 {
count = n
}
}
list, err := h.search.ModelList(c.Request.Context(), key, count)
if err != nil {
h.log.Error("model list search failed", zap.Error(err))
response.InternalError(c, "failed to search models")
return
}
if base64Resp {
encoded, err := encode.EncodeJSON(list)
if err != nil {
h.log.Error("encode response failed", zap.Error(err))
response.InternalError(c, "failed to encode response")
return
}
c.String(http.StatusOK, encoded)
return
}
c.JSON(http.StatusOK, list)
}