Files
wx_service/internal/handler/video_handler.go
T
nepiedg d23b253609 Enhance API and error handling for video services
- Updated .gitignore to exclude cache files.
- Refactored main.go to streamline route registration and improve code organization.
- Added detailed comments in auth_handler.go, video_handler.go, and service files for better clarity on request handling and service logic.
- Improved error messages in video_handler.go to provide clearer feedback to users in Chinese.
- Introduced context handling in service methods to manage request timeouts effectively.
2025-12-31 02:30:20 +00:00

96 lines
2.7 KiB
Go

package handler
import (
"errors"
"net/http"
"github.com/gin-gonic/gin"
"wx_service/internal/middleware"
"wx_service/internal/model"
"wx_service/internal/service"
)
type VideoHandler struct {
videoService *service.VideoService
}
func NewVideoHandler(videoService *service.VideoService) *VideoHandler {
return &VideoHandler{
videoService: videoService,
}
}
type removeWatermarkRequest struct {
Content string `json:"content" binding:"required"`
}
func (h *VideoHandler) RemoveWatermark(c *gin.Context) {
var req removeWatermarkRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "请求参数错误"))
return
}
user, ok := getCurrentUser(c)
if !ok {
c.JSON(http.StatusUnauthorized, model.Error(http.StatusUnauthorized, "未登录或登录已过期"))
return
}
result, err := h.videoService.RemoveWatermark(c.Request.Context(), user, req.Content)
if err != nil {
switch {
case errors.Is(err, service.ErrURLNotFound):
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "请检查分享链接是否正确"))
return
case errors.Is(err, service.ErrDailyQuotaExceeded):
c.JSON(http.StatusForbidden, model.Error(http.StatusForbidden, "今日免费次数已用完,观看广告后今天可无限制使用"))
return
case errors.Is(err, service.ErrShortVideoAPIKey):
c.JSON(http.StatusServiceUnavailable, model.Error(http.StatusServiceUnavailable, "服务暂不可用,请联系管理员"))
return
default:
var thirdPartyErr *service.ThirdPartyError
if errors.As(err, &thirdPartyErr) {
c.JSON(http.StatusBadGateway, model.Error(http.StatusBadGateway, "解析服务异常,请稍后重试"))
return
}
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "去水印失败,请稍后重试"))
return
}
}
c.JSON(http.StatusOK, model.Success(gin.H{
"provider": result.Provider,
"raw": result.Raw,
"free_quota_used": result.FreeQuotaUsed,
}))
}
func (h *VideoHandler) UnlockQuota(c *gin.Context) {
user, ok := getCurrentUser(c)
if !ok {
c.JSON(http.StatusUnauthorized, model.Error(http.StatusUnauthorized, "未登录或登录已过期"))
return
}
if err := h.videoService.UnlockForToday(c.Request.Context(), user); err != nil {
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "解锁失败,请稍后重试"))
return
}
c.JSON(http.StatusOK, model.Success(gin.H{
"unlocked": true,
}))
}
func getCurrentUser(c *gin.Context) (*model.User, bool) {
userVal, exists := c.Get(middleware.ContextCurrentUserKey)
if !exists {
return nil, false
}
user, ok := userVal.(*model.User)
return user, ok
}