97cadb033e
- Updated .env.example to include SHORT_VIDEO_API_KEY, SHORT_VIDEO_FREE_QUOTA, and SHORT_VIDEO_TIMEOUT_SECONDS. - Enhanced main.go to auto-migrate new VideoParseLog and VideoParseUnlock models. - Introduced VideoService and VideoHandler for handling video-related operations. - Added protected API routes for removing watermarks and unlocking video features. - Updated config.go to support short video configuration settings. - Expanded documentation to reflect new features and configuration options.
96 lines
2.6 KiB
Go
96 lines
2.6 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, "invalid request payload"))
|
|
return
|
|
}
|
|
|
|
user, ok := getCurrentUser(c)
|
|
if !ok {
|
|
c.JSON(http.StatusUnauthorized, model.Error(http.StatusUnauthorized, "unauthorized"))
|
|
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, "content must contain a valid url"))
|
|
return
|
|
case errors.Is(err, service.ErrDailyQuotaExceeded):
|
|
c.JSON(http.StatusForbidden, model.Error(http.StatusForbidden, err.Error()))
|
|
return
|
|
case errors.Is(err, service.ErrShortVideoAPIKey):
|
|
c.JSON(http.StatusServiceUnavailable, model.Error(http.StatusServiceUnavailable, "short video api key missing"))
|
|
return
|
|
default:
|
|
var thirdPartyErr *service.ThirdPartyError
|
|
if errors.As(err, &thirdPartyErr) {
|
|
c.JSON(http.StatusBadGateway, model.Error(http.StatusBadGateway, thirdPartyErr.Error()))
|
|
return
|
|
}
|
|
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "remove watermark failed"))
|
|
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, "unauthorized"))
|
|
return
|
|
}
|
|
|
|
if err := h.videoService.UnlockForToday(c.Request.Context(), user); err != nil {
|
|
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "unlock failed"))
|
|
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
|
|
}
|