🎬 Enable browser transcode for run artifacts

This commit is contained in:
eust-w 2026-06-26 00:59:03 +08:00
parent d84c2ffcc4
commit f55dbfc52b
5 changed files with 125 additions and 4 deletions

View File

@ -296,7 +296,7 @@ When `data.dataRef.payloadPath` points to a local file, data-asset endpoints tre
`GET /api/runs/:id/artifacts/:artifactId/content`
Streams the selected artifact content for authenticated users with access to the run. Media previews returned by run and artifact APIs use this endpoint instead of embedding video/audio/image bytes as `data:*;base64` in JSON responses. The endpoint supports `GET` and `HEAD`, browser `Range` requests, `206 Partial Content`, `Content-Range`, `Accept-Ranges`, and stable `Content-Type` detection from artifact metadata or file extension. It serves persisted local artifact files when they still exist, redirects HTTP(S) preview URLs with `307 Temporary Redirect`, and can still decode older persisted data-URL previews for backward compatibility.
Streams the selected artifact content for authenticated users with access to the run. Media previews returned by run and artifact APIs use this endpoint instead of embedding video/audio/image bytes as `data:*;base64` in JSON responses. The endpoint supports `GET` and `HEAD`, browser `Range` requests, `206 Partial Content`, `Content-Range`, `Accept-Ranges`, and stable `Content-Type` detection from artifact metadata or file extension. It serves persisted local artifact files when they still exist, redirects HTTP(S) preview URLs with `307 Temporary Redirect`, and can still decode older persisted data-URL previews for backward compatibility. For video artifacts, append `?transcode=browser` to request a cached browser-compatible MP4 preview generated through the deployed API's `ffmpeg` path; Monitoring adds this automatically for artifact video previews.
`GET /api/runs/:id/lineage`

View File

@ -3466,6 +3466,32 @@ func artifactContentMimeType(item domain.ExecutionArtifact, fileName string, pay
return "application/octet-stream"
}
func artifactBrowserTranscodeRequested(request *http.Request, item domain.ExecutionArtifact, mimeType string) bool {
if !strings.EqualFold(strings.TrimSpace(request.URL.Query().Get("transcode")), "browser") {
return false
}
if artifactPreviewKind(item) == "video" {
return true
}
return strings.HasPrefix(strings.ToLower(strings.TrimSpace(mimeType)), "video/")
}
func writeArtifactBrowserVideoPreview(writer http.ResponseWriter, request *http.Request, fileName string, previewRequest sourceBrowserPreviewRequest) error {
if previewPath, err := sourceBrowserVideoPreviewPath(request.Context(), previewRequest); err == nil && previewPath != "" {
return writeSourceContentFile(writer, request, strings.TrimSuffix(fileName, path.Ext(fileName))+"-browser.mp4", "video/mp4", previewPath)
}
writer.Header().Set("Content-Type", "video/mp4")
writer.Header().Set("Cache-Control", "no-store")
writer.WriteHeader(http.StatusOK)
if flusher, ok := writer.(http.Flusher); ok {
flusher.Flush()
}
if err := sourceBrowserVideoPreviewWriter(request.Context(), writer, previewRequest); err != nil {
fmt.Printf("[api] artifact browser preview stream failed: %v\n", err)
}
return nil
}
func (server *server) handleRunsArtifactContent(writer http.ResponseWriter, request *http.Request) {
item, err := server.loadRun(request.Context(), chi.URLParam(request, "id"))
if err != nil {
@ -3485,9 +3511,20 @@ func (server *server) handleRunsArtifactContent(writer http.ResponseWriter, requ
return
}
fileName := artifactContentFileName(*artifact)
mimeType := artifactContentMimeType(*artifact, fileName, nil)
if filePath := strings.TrimSpace(artifact.Path); filePath != "" && !strings.Contains(filePath, "://") {
if info, err := os.Stat(filePath); err == nil && !info.IsDir() {
if err := writeSourceContentFile(writer, request, fileName, artifactContentMimeType(*artifact, fileName, nil), filePath); err != nil {
if artifactBrowserTranscodeRequested(request, *artifact, mimeType) {
previewRequest := sourceBrowserPreviewRequest{
FileName: fileName,
RedirectURL: filePath,
}
if err := writeArtifactBrowserVideoPreview(writer, request, fileName, previewRequest); err != nil {
renderError(writer, http.StatusInternalServerError, err)
}
return
}
if err := writeSourceContentFile(writer, request, fileName, mimeType, filePath); err != nil {
renderError(writer, http.StatusInternalServerError, err)
}
return
@ -3495,14 +3532,34 @@ func (server *server) handleRunsArtifactContent(writer http.ResponseWriter, requ
}
previewURL := packageArtifactPreviewURL(*artifact)
if strings.HasPrefix(previewURL, "http://") || strings.HasPrefix(previewURL, "https://") {
if artifactBrowserTranscodeRequested(request, *artifact, mimeType) {
previewRequest := sourceBrowserPreviewRequest{
FileName: fileName,
RedirectURL: previewURL,
}
if err := writeArtifactBrowserVideoPreview(writer, request, fileName, previewRequest); err != nil {
renderError(writer, http.StatusInternalServerError, err)
}
return
}
http.Redirect(writer, request, previewURL, http.StatusTemporaryRedirect)
return
}
if payload, decodedMimeType, err := contentBytesForDataURL(previewURL); err == nil && len(payload) > 0 {
mimeType := artifactContentMimeType(*artifact, fileName, payload)
mimeType = artifactContentMimeType(*artifact, fileName, payload)
if strings.TrimSpace(mimeType) == "application/octet-stream" && strings.TrimSpace(decodedMimeType) != "" {
mimeType = decodedMimeType
}
if artifactBrowserTranscodeRequested(request, *artifact, mimeType) {
previewRequest := sourceBrowserPreviewRequest{
FileName: fileName,
Payload: payload,
}
if err := writeArtifactBrowserVideoPreview(writer, request, fileName, previewRequest); err != nil {
renderError(writer, http.StatusInternalServerError, err)
}
return
}
writeSourceContent(writer, request, fileName, mimeType, payload)
return
}

View File

@ -3805,6 +3805,42 @@ func TestRunArtifactContentStreamsLocalMediaWithRangeAndPreviewURL(t *testing.T)
if contentType := contentRecorder.Header().Get("Content-Type"); !strings.HasPrefix(contentType, "video/mp4") {
t.Fatalf("content type = %q, want video/mp4", contentType)
}
previewPath := filepath.Join(t.TempDir(), "artifact-browser-preview.mp4")
if err := os.WriteFile(previewPath, []byte("artifact-browser-preview"), 0o644); err != nil {
t.Fatalf("write browser preview: %v", err)
}
originalPreviewResolver := sourceBrowserVideoPreviewPath
sourceBrowserVideoPreviewPath = func(ctx context.Context, request sourceBrowserPreviewRequest) (string, error) {
if request.RedirectURL != videoPath {
t.Fatalf("unexpected artifact preview input: %s", request.RedirectURL)
}
if request.FileName != "clip.mp4" {
t.Fatalf("unexpected artifact preview file name: %s", request.FileName)
}
if len(request.Payload) != 0 {
t.Fatalf("local artifact preview should not materialize payload bytes")
}
return previewPath, nil
}
t.Cleanup(func() {
sourceBrowserVideoPreviewPath = originalPreviewResolver
})
transcodedRecorder := httptest.NewRecorder()
handler.ServeHTTP(
transcodedRecorder,
httptest.NewRequest(http.MethodGet, "/api/runs/run-media/artifacts/artifact-video/content?transcode=browser", nil),
)
if transcodedRecorder.Code != http.StatusOK {
t.Fatalf("browser preview status: %d body=%s", transcodedRecorder.Code, transcodedRecorder.Body.String())
}
if contentType := transcodedRecorder.Header().Get("Content-Type"); contentType != "video/mp4" {
t.Fatalf("browser preview content type = %q, want video/mp4", contentType)
}
if got := transcodedRecorder.Body.String(); got != "artifact-browser-preview" {
t.Fatalf("browser preview body = %q", got)
}
}
func TestWorkflowRunsListReturnsWorkflowOnlySummaries(t *testing.T) {

View File

@ -1,6 +1,7 @@
const SOURCE_DATA_CONTENT_PATH_PREFIX = "/api/source-data/collections/";
const PACKAGE_CONTENT_PATH_PREFIX = "/api/packages/";
const OBJECT_STORAGE_CONTENT_PATH = "/api/object-storage/content";
const RUN_ARTIFACT_CONTENT_PATH_PREFIX = "/api/runs/";
function normalizeURL(input) {
if (!input || typeof input !== "string") {
@ -43,13 +44,26 @@ export function isObjectStorageContentURL(input) {
return normalized.pathname === OBJECT_STORAGE_CONTENT_PATH;
}
export function isRunArtifactContentURL(input) {
const normalized = normalizeURL(input);
if (!normalized) {
return false;
}
return (
normalized.pathname.startsWith(RUN_ARTIFACT_CONTENT_PATH_PREFIX) &&
normalized.pathname.includes("/artifacts/") &&
normalized.pathname.endsWith("/content")
);
}
export function buildBrowserPlayableVideoURL(input) {
const normalized = normalizeURL(input);
if (
!normalized ||
(!isSourceDataContentURL(input) &&
!isPackageContentURL(input) &&
!isObjectStorageContentURL(input))
!isObjectStorageContentURL(input) &&
!isRunArtifactContentURL(input))
) {
return typeof input === "string" ? input : "";
}

View File

@ -5,6 +5,7 @@ import {
buildBrowserPlayableVideoURL,
isObjectStorageContentURL,
isPackageContentURL,
isRunArtifactContentURL,
isSourceDataContentURL,
} from "../../src/lib/video-preview.js";
@ -34,6 +35,12 @@ test("recognizes object storage content urls", () => {
assert.equal(isObjectStorageContentURL("https://example.com/video.mp4"), false);
});
test("recognizes run artifact content urls", () => {
assert.equal(isRunArtifactContentURL("/api/runs/run-1/artifacts/artifact-video/content"), true);
assert.equal(isRunArtifactContentURL("/api/runs/run-1/lineage"), false);
assert.equal(isRunArtifactContentURL("https://example.com/video.mp4"), false);
});
test("adds browser transcode flag for source data video previews", () => {
assert.equal(
buildBrowserPlayableVideoURL(
@ -61,6 +68,13 @@ test("adds browser transcode flag for object storage video previews", () => {
);
});
test("adds browser transcode flag for run artifact video previews", () => {
assert.equal(
buildBrowserPlayableVideoURL("/api/runs/run-1/artifacts/artifact-video/content"),
"/api/runs/run-1/artifacts/artifact-video/content?transcode=browser",
);
});
test("does not duplicate browser transcode flag", () => {
assert.equal(
buildBrowserPlayableVideoURL(