Add media proxy feature for resource downloading

- Introduced a new API endpoint `GET /api/v1/video/proxy` to facilitate media resource downloads, allowing users to bypass domain restrictions imposed by WeChat.
- Updated configuration to include proxy settings such as `SHORT_VIDEO_PROXY_ENABLED`, `SHORT_VIDEO_PROXY_ALLOWED_DOMAINS`, `SHORT_VIDEO_PROXY_MAX_SIZE_MB`, and `SHORT_VIDEO_PROXY_TIMEOUT_SECONDS`.
- Enhanced the `ShortVideoConfig` struct to accommodate new proxy-related fields.
- Improved error handling for proxy requests, including checks for allowed domains and file size limits.
- Updated documentation to reflect the new proxy functionality and its configuration options, ensuring clarity for users and developers.
This commit is contained in:
nepiedg
2026-02-06 11:28:02 +00:00
parent 9200600b1c
commit 1b8ff310eb
7 changed files with 1049 additions and 3 deletions
@@ -2,8 +2,10 @@ package handler
import (
"errors"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"time"
@@ -36,6 +38,10 @@ type reportDownloadFailureRequest struct {
UserAgent string `json:"userAgent"`
}
type proxyMediaRequest struct {
URL string `form:"url" binding:"required"`
}
func (h *VideoHandler) RemoveWatermark(c *gin.Context) {
var req removeWatermarkRequest
if err := c.ShouldBindJSON(&req); err != nil {
@@ -136,3 +142,65 @@ func (h *VideoHandler) ReportDownloadFailure(c *gin.Context) {
"reported": true,
}))
}
// ProxyMedia 代理媒体资源下载
// 该接口用于中转视频/图片等媒体资源,使小程序可以通过当前域名下载,避免微信域名限制
func (h *VideoHandler) ProxyMedia(c *gin.Context) {
var req proxyMediaRequest
if err := c.ShouldBindQuery(&req); err != nil {
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "请求参数错误,缺少 url 参数"))
return
}
targetURL := strings.TrimSpace(req.URL)
if targetURL == "" {
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "url 参数不能为空"))
return
}
// 调用 service 层进行代理
result, err := h.videoService.ProxyMedia(c.Request.Context(), targetURL)
if err != nil {
switch {
case errors.Is(err, service.ErrProxyDisabled):
c.JSON(http.StatusServiceUnavailable, model.Error(http.StatusServiceUnavailable, "代理服务未启用"))
return
case errors.Is(err, service.ErrProxyInvalidURL):
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "无效的代理地址"))
return
case errors.Is(err, service.ErrProxyDomainBlocked):
c.JSON(http.StatusForbidden, model.Error(http.StatusForbidden, "该域名不在允许列表中"))
return
case errors.Is(err, service.ErrProxyFileTooLarge):
c.JSON(http.StatusRequestEntityTooLarge, model.Error(http.StatusRequestEntityTooLarge, "文件过大,超出限制"))
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
}
}
// 确保关闭响应体
defer result.Body.Close()
// 设置响应头
c.Header("Content-Type", result.ContentType)
if result.ContentLength > 0 {
c.Header("Content-Length", strconv.FormatInt(result.ContentLength, 10))
}
// 允许跨域(如果需要)
c.Header("Access-Control-Allow-Origin", "*")
// 设置缓存头,减少重复请求
c.Header("Cache-Control", "public, max-age=86400")
// 流式输出响应体
c.Status(result.StatusCode)
// 使用 io.Copy 进行流式传输,避免一次性加载到内存
_, _ = io.Copy(c.Writer, result.Body)
}