fix: proxy transparent MP4 effect ranges

This commit is contained in:
ZuoZuo 2026-07-23 04:42:48 +08:00
parent 6d52cffc44
commit 9a27923d69
2 changed files with 18 additions and 1 deletions

View File

@ -20,6 +20,7 @@ var (
"media.haiyihy.com": {},
}
effectMediaProxyExtensions = map[string]string{
".mp4": "video/mp4",
".pag": "application/octet-stream",
".svga": "application/octet-stream",
}
@ -42,6 +43,11 @@ func (h *Handler) proxyEffectMedia(writer http.ResponseWriter, request *http.Req
}
upstreamRequest.Header.Set("Accept", "application/octet-stream,*/*")
upstreamRequest.Header.Set("User-Agent", "hyapp-gateway-effect-media-proxy/1.0")
// 透明 MP4 的浏览器播放器需要 seek 多个采样帧来拆分 RGB/Alpha 通道;透传 Range
// 才能让 video 元素按需取片段,而不是每次检测都重新下载整段动效。
if value := strings.TrimSpace(request.Header.Get("Range")); value != "" {
upstreamRequest.Header.Set("Range", value)
}
upstream, err := effectMediaProxyHTTPClient.Do(upstreamRequest)
if err != nil {
@ -66,11 +72,17 @@ func (h *Handler) proxyEffectMedia(writer http.ResponseWriter, request *http.Req
header.Set("Content-Type", contentType)
header.Set("Cache-Control", "public, max-age=86400")
header.Set("X-Content-Type-Options", "nosniff")
if value := strings.TrimSpace(upstream.Header.Get("Accept-Ranges")); value != "" {
header.Set("Accept-Ranges", value)
}
if value := strings.TrimSpace(upstream.Header.Get("Content-Range")); value != "" {
header.Set("Content-Range", value)
}
if upstream.ContentLength >= 0 {
header.Set("Content-Length", strconv.FormatInt(upstream.ContentLength, 10))
}
writer.WriteHeader(http.StatusOK)
writer.WriteHeader(upstream.StatusCode)
_, _ = io.Copy(writer, io.LimitReader(upstream.Body, effectMediaProxyMaxBytes+1))
}

View File

@ -8,6 +8,11 @@ func TestValidateEffectMediaProxyURL(t *testing.T) {
url string
ok bool
}{
{
name: "mp4 media host",
url: "https://media.haiyihy.com/admin/files/1/20260602/file.mp4?sign=1",
ok: true,
},
{
name: "svga media host",
url: "https://media.haiyihy.com/admin/files/1/20260602/file.svga",