refactor: split playback proxy logic into separate handler files
This commit is contained in:
74
internal/playback/handler/proxy_subtitle.go
Normal file
74
internal/playback/handler/proxy_subtitle.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"mal/internal/observability"
|
||||
netutil "mal/pkg/net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func (h *PlaybackHandler) HandleProxySubtitle(c *gin.Context) {
|
||||
targetURL, referer, ok := h.resolveProxyRequestTarget(c, "subtitle")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
if data, contentType, ok := h.subtitleCache.Get(targetURL, time.Now()); ok {
|
||||
c.Data(http.StatusOK, contentType, data)
|
||||
return
|
||||
}
|
||||
|
||||
req, err := newProxyRequest(c.Request.Context(), targetURL, referer)
|
||||
if err != nil {
|
||||
c.Status(http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := h.proxyClient.Do(req)
|
||||
if err != nil {
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
observability.ErrorContext(c.Request.Context(), "proxy_subtitle_upstream_failed", "playback", "", map[string]any{"target_url": targetURL}, err)
|
||||
_ = c.Error(err).SetType(gin.ErrorTypePrivate)
|
||||
}
|
||||
c.Status(http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
body, err := io.ReadAll(io.LimitReader(resp.Body, netutil.MiB2))
|
||||
if err != nil {
|
||||
observability.ErrorContext(c.Request.Context(), "proxy_subtitle_read_failed", "playback", "", map[string]any{"target_url": targetURL}, err)
|
||||
_ = c.Error(err).SetType(gin.ErrorTypePrivate)
|
||||
c.Status(http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
|
||||
contentType := resp.Header.Get("Content-Type")
|
||||
if contentType == "" {
|
||||
contentType = detectSubtitleType(targetURL)
|
||||
}
|
||||
|
||||
h.subtitleCache.Set(targetURL, body, contentType, time.Now())
|
||||
|
||||
c.Data(http.StatusOK, contentType, body)
|
||||
}
|
||||
|
||||
func detectSubtitleType(url string) string {
|
||||
lower := strings.ToLower(url)
|
||||
switch {
|
||||
case strings.Contains(lower, ".vtt"):
|
||||
return "text/vtt"
|
||||
case strings.Contains(lower, ".srt"):
|
||||
return "text/plain; charset=utf-8"
|
||||
case strings.Contains(lower, ".ass") || strings.Contains(lower, ".ssa"):
|
||||
return "text/plain; charset=utf-8"
|
||||
default:
|
||||
return "text/plain; charset=utf-8"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user