From aebce71e6fca4d7fa5bc1c114b1679f95ca29d5e Mon Sep 17 00:00:00 2001 From: 741075810 <31341354+741075810@users.noreply.github.com> Date: Thu, 13 Aug 2026 22:28:16 +0800 Subject: [PATCH] =?UTF-8?q?fix(video):=20=E8=BF=94=E5=9B=9E=E5=8F=AF?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E6=92=AD=E6=94=BE=E7=9A=84=E8=A7=86=E9=A2=91?= =?UTF-8?q?=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../transport/http/inference/handler.go | 21 +++++++++++++--- .../transport/http/inference/handler_test.go | 25 +++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/backend/internal/transport/http/inference/handler.go b/backend/internal/transport/http/inference/handler.go index efc81731..00d4f83d 100644 --- a/backend/internal/transport/http/inference/handler.go +++ b/backend/internal/transport/http/inference/handler.go @@ -908,11 +908,10 @@ func (h *Handler) getVideo(c *gin.Context) { writeGatewayError(c, err) return } - c.JSON(http.StatusOK, videoGenerationResponse(job, h.videoContentURL(job.ID))) + c.JSON(http.StatusOK, videoGenerationResponse(job, h.videoPlaybackURL(job))) } -func (h *Handler) videoContentURL(jobID string) string { - path := "/v1/videos/" + url.PathEscape(jobID) + "/content" +func (h *Handler) publicURL(path string) string { baseURL := h.publicAPIBaseURL if h.publicBaseURL != nil { baseURL = strings.TrimRight(strings.TrimSpace(h.publicBaseURL()), "/") @@ -923,6 +922,22 @@ func (h *Handler) videoContentURL(jobID string) string { return baseURL + path } +func (h *Handler) videoContentURL(jobID string) string { + return h.publicURL("/v1/videos/" + url.PathEscape(jobID) + "/content") +} + +// videoPlaybackURL prefers the stored asset served by the public media route, so the +// returned link opens directly in browsers and players. /v1/videos/{id}/content needs +// the client API key, which makes the URL unusable outside an authenticated client. +// Images already return their public media URL; this keeps video consistent. Jobs +// without a stored asset keep the protected content endpoint. +func (h *Handler) videoPlaybackURL(job mediadomain.Job) string { + if assetID := strings.TrimSpace(job.ResultAssetID); assetID != "" { + return h.publicURL("/v1/media/videos/" + url.PathEscape(assetID)) + } + return h.videoContentURL(job.ID) +} + func (h *Handler) getVideoContent(c *gin.Context) { clientKey, _, ok := requestIdentity(c) if !ok { diff --git a/backend/internal/transport/http/inference/handler_test.go b/backend/internal/transport/http/inference/handler_test.go index 75a7dc69..9e70aa17 100644 --- a/backend/internal/transport/http/inference/handler_test.go +++ b/backend/internal/transport/http/inference/handler_test.go @@ -134,6 +134,31 @@ func TestVideoContentURLUsesConfiguredPublicAPIBase(t *testing.T) { } } +// A completed job carries a stored asset, so the returned URL must point at the +// public media route. /v1/videos/{id}/content requires the client API key and is +// therefore not usable in a browser or player. +func TestVideoPlaybackURLPrefersPublicAssetRoute(t *testing.T) { + handler := NewHandler(nil, nil, 1<<20, "https://api.example.com/grok2api/") + job := mediadomain.Job{ + ID: "video_request_1", Status: mediadomain.StatusCompleted, + UpstreamURL: "https://assets.grok.com/source.mp4", ResultAssetID: "vid_abc123", + } + response := videoGenerationResponse(job, handler.videoPlaybackURL(job)) + video, ok := response["video"].(gin.H) + if !ok || video["url"] != "https://api.example.com/grok2api/v1/media/videos/vid_abc123" { + t.Fatalf("response = %#v", response) + } +} + +// Without a stored asset the protected content endpoint remains the only option. +func TestVideoPlaybackURLFallsBackToContentEndpoint(t *testing.T) { + handler := NewHandler(nil, nil, 1<<20, "https://api.example.com/grok2api/") + job := mediadomain.Job{ID: "video_request_1", Status: mediadomain.StatusCompleted} + if got := handler.videoPlaybackURL(job); got != "https://api.example.com/grok2api/v1/videos/video_request_1/content" { + t.Fatalf("fallback URL = %q", got) + } +} + func TestVideoContentURLFollowsRuntimePublicAPIBase(t *testing.T) { baseURL := "https://old.example.com" handler := NewHandler(nil, nil, 1<<20, "https://static.example.com").SetPublicAPIBaseURLResolver(func() string {