writeTaskMediaContentPath answers with an artifact the upstream delivers inside a long-running operation: the base64 string at the descriptor's ContentPath of the JSON reply. An operation that failed is an upstream error, one not yet done is not ready, and one without the string, expired or written
(c *gin.Context, body io.Reader, descriptor *relaychannel.TaskContentRequest)
| 341 | // error, one not yet done is not ready, and one without the string, expired |
| 342 | // or written to storage instead, no longer has the content. |
| 343 | func writeTaskMediaContentPath(c *gin.Context, body io.Reader, descriptor *relaychannel.TaskContentRequest) error { |
| 344 | limitMB := constant.MaxFileDownloadMB |
| 345 | if limitMB <= 0 { |
| 346 | limitMB = 64 |
| 347 | } |
| 348 | // Base64 makes the media a third larger; the rest of the reply is small. |
| 349 | limit := int64(limitMB)<<20*4/3 + 1<<20 |
| 350 | raw, err := io.ReadAll(io.LimitReader(body, limit+1)) |
| 351 | if err != nil { |
| 352 | return &taskMediaProxyError{ |
| 353 | status: http.StatusBadGateway, code: "artifact_upstream_error", |
| 354 | message: "Failed to fetch artifact content", err: err, |
| 355 | } |
| 356 | } |
| 357 | if int64(len(raw)) > limit { |
| 358 | return &taskMediaProxyError{ |
| 359 | status: http.StatusBadGateway, code: "artifact_request_rejected", |
| 360 | message: "Artifact request was rejected", err: errTaskMediaRequestRejected, |
| 361 | } |
| 362 | } |
| 363 | if !ojson.ValidBytes(raw) { |
| 364 | return &taskMediaProxyError{ |
| 365 | status: http.StatusBadGateway, code: "artifact_upstream_error", |
| 366 | message: "Failed to decode artifact content", |
| 367 | } |
| 368 | } |
| 369 | // The root shares memory with raw, which stays unchanged until the |
| 370 | // artifact is written, so the base64 payload is not copied. |
| 371 | operation := ojson.GetBytesNoCopy(raw, "@this") |
| 372 | if failure := operation.Get("error"); failure.Exists() && failure.Type != ojson.Null { |
| 373 | return &taskMediaProxyError{ |
| 374 | status: http.StatusBadGateway, code: "artifact_upstream_error", |
| 375 | message: "Artifact upstream returned an error", |
| 376 | } |
| 377 | } |
| 378 | // An interaction reports completion through status, not done. |
| 379 | if operation.Get("done").Type != ojson.True && operation.Get("status").String() != "completed" { |
| 380 | return &taskMediaProxyError{ |
| 381 | status: http.StatusConflict, code: "artifact_not_ready", |
| 382 | message: "Task artifacts are not ready", |
| 383 | } |
| 384 | } |
| 385 | content := operation |
| 386 | for _, segment := range descriptor.ContentPath { |
| 387 | if key, isKey := segment.(string); isKey && content.IsObject() { |
| 388 | content = content.Get(ojson.Escape(key)) |
| 389 | continue |
| 390 | } |
| 391 | index := "" |
| 392 | switch typed := segment.(type) { |
| 393 | case int: |
| 394 | index = strconv.Itoa(typed) |
| 395 | case int64: |
| 396 | index = strconv.FormatInt(typed, 10) |
| 397 | case float64: |
| 398 | index = strconv.FormatFloat(typed, 'f', -1, 64) |
| 399 | } |
| 400 | if index == "" || !content.IsArray() { |
no test coverage detected