diff --git a/internal/service/stream_normalize_test.go b/internal/service/stream_normalize_test.go index 52ab92d..775bb62 100644 --- a/internal/service/stream_normalize_test.go +++ b/internal/service/stream_normalize_test.go @@ -22,11 +22,18 @@ func TestNormalizeCloudPlayTarget(t *testing.T) { if parsed.IsAbs() || parsed.Host != "" { t.Fatalf("normalized target should be relative, got %q", got) } - if parsed.Query().Get("ref") != ref { - t.Fatalf("ref round-trip failed: %q", parsed.Query().Get("ref")) - } + if parsed.Query().Get("ref") != ref { + t.Fatalf("ref round-trip failed: %q", parsed.Query().Get("ref")) + } - // 非云盘播放 URL 保持原样(WebDAV/直链等)。 + strmStale := "http://bwg.linkmy.fun:1314/api/strm/play/cloud115/video.mkv?acct=abc&pickcode=123" + gotStrm := normalizeCloudPlayTarget(strmStale) + wantStrm := "/api/strm/play/cloud115/video.mkv?acct=abc&pickcode=123" + if gotStrm != wantStrm { + t.Fatalf("normalizeCloudPlayTarget(strm) = %q, want %q", gotStrm, wantStrm) + } + + // 非云盘播放 URL 保持原样(WebDAV/直链等)。 passthrough := "https://dav.example.com/media/file.mkv" if got := normalizeCloudPlayTarget(passthrough); got != passthrough { t.Fatalf("non-cloud target should pass through, got %q", got) diff --git a/internal/service/stream_redirect.go b/internal/service/stream_redirect.go index 2b56f37..eaa24d3 100644 --- a/internal/service/stream_redirect.go +++ b/internal/service/stream_redirect.go @@ -15,11 +15,22 @@ import ( // /api/cloud/play 路径,由 absoluteInternalRedirect 基于「当前请求」补全 // host,从而对历史脏数据免疫。 func normalizeCloudPlayTarget(raw string) string { - typ, ref, ok := parseCloudMediaPlaybackURL(raw) - if !ok { + raw = strings.TrimSpace(raw) + if raw == "" { return raw } - return BuildRelativeCloudPlayURL(typ, ref) + if typ, ref, ok := parseCloudMediaPlaybackURL(raw); ok { + return BuildRelativeCloudPlayURL(typ, ref) + } + if u, err := url.Parse(raw); err == nil { + path := strings.ToLower(u.Path) + if strings.HasPrefix(path, "/api/strm/play/") || strings.HasPrefix(path, "/api/cloud/play/") || strings.HasPrefix(path, "/api/stream/") { + u.Scheme = "" + u.Host = "" + return u.String() + } + } + return raw } // BuildRelativeCloudPlayURL 构造相对的云盘播放 API 路径。