Files
wx_service/internal/remove_watermark/handler/video_handler.go
T
nepiedg 9200600b1c Enhance smoking tracking API with new features and improvements
- Added a new API endpoint `GET /api/v1/smoke/home` to consolidate core modules for the home dashboard, reducing the need for multiple requests.
- Updated the `smoke` routes to include the new home endpoint and improved user profile management with the addition of a `quit_date` field.
- Enhanced the algorithm for calculating daily targets and next smoke suggestions, ensuring accurate future time handling and user-specific recommendations.
- Improved API documentation to reflect new endpoints, response formats, and detailed field descriptions for better clarity and usability.
- Refactored user authentication handling in various handlers to streamline the process and ensure consistent error responses.
2026-01-29 17:16:35 +00:00

139 lines
3.8 KiB
Go

package handler
import (
"errors"
"net/http"
"net/url"
"strings"
"time"
"github.com/gin-gonic/gin"
"wx_service/internal/middleware"
"wx_service/internal/model"
"wx_service/internal/remove_watermark/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"`
}
type reportDownloadFailureRequest struct {
Domain string `json:"domain"`
FailedURL string `json:"failedUrl" binding:"required"`
ErrorMessage string `json:"errorMessage"`
Timestamp int64 `json:"timestamp"`
UserAgent string `json:"userAgent"`
}
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 := middleware.MustCurrentUser(c)
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 := middleware.MustCurrentUser(c)
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 (h *VideoHandler) ReportDownloadFailure(c *gin.Context) {
var req reportDownloadFailureRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "请求参数错误"))
return
}
failedURL := strings.TrimSpace(req.FailedURL)
if failedURL == "" {
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "failedUrl 不能为空"))
return
}
reportedAt := time.Now()
if req.Timestamp > 0 {
reportedAt = time.UnixMilli(req.Timestamp)
}
domain := strings.TrimSpace(req.Domain)
if domain == "" {
if parsed, err := url.Parse(failedURL); err == nil {
domain = parsed.Host
}
}
userAgent := strings.TrimSpace(req.UserAgent)
if userAgent == "" {
userAgent = c.Request.UserAgent()
}
report := service.DownloadFailureReport{
Domain: domain,
FailedURL: failedURL,
ErrorMessage: req.ErrorMessage,
ReportedAt: reportedAt,
UserAgent: userAgent,
ClientIP: c.ClientIP(),
}
if err := h.videoService.ReportDownloadFailure(c.Request.Context(), report); err != nil {
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "上报失败,请稍后重试"))
return
}
c.JSON(http.StatusOK, model.Success(gin.H{
"reported": true,
}))
}