mirror of
https://github.com/chenyme/grok2api.git
synced 2026-12-05 16:02:12 +00:00
fix(video): 返回可直接播放的视频地址
This commit is contained in:
1 parent
18f0c27fc1
commit
aebce71e6f
2 files changed
+43
-3
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user