| | | 1 | | using Elsa.WorkflowProviders.BlobStorage.Contracts; |
| | | 2 | | using Elsa.Workflows; |
| | | 3 | | using Elsa.Workflows.Management.Mappers; |
| | | 4 | | using Elsa.Workflows.Management.Materializers; |
| | | 5 | | using Elsa.Workflows.Management.Models; |
| | | 6 | | using Elsa.Workflows.Runtime; |
| | | 7 | | using FluentStorage.Blobs; |
| | | 8 | | using Microsoft.Extensions.Logging; |
| | | 9 | | |
| | | 10 | | namespace Elsa.WorkflowProviders.BlobStorage.Handlers; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Handles JSON-formatted workflow definitions from blob storage. |
| | | 14 | | /// </summary> |
| | 266 | 15 | | public class JsonBlobWorkflowFormatHandler( |
| | 266 | 16 | | IActivitySerializer activitySerializer, |
| | 266 | 17 | | WorkflowDefinitionMapper workflowDefinitionMapper, |
| | 266 | 18 | | ILogger<JsonBlobWorkflowFormatHandler> logger) : IBlobWorkflowFormatHandler |
| | | 19 | | { |
| | | 20 | | /// <inheritdoc /> |
| | 0 | 21 | | public string Name => "Json"; |
| | | 22 | | |
| | | 23 | | /// <inheritdoc /> |
| | 8 | 24 | | public IEnumerable<string> SupportedExtensions => ["json"]; |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | public bool CanHandle(Blob blob, string? contentType) |
| | | 28 | | { |
| | | 29 | | // Extension filtering is already handled by the provider via SupportedExtensions. |
| | | 30 | | // Here we can optionally check content type for additional validation. |
| | 168 | 31 | | if (!string.IsNullOrEmpty(contentType) && contentType.Contains("json", StringComparison.OrdinalIgnoreCase)) |
| | 0 | 32 | | return true; |
| | | 33 | | |
| | | 34 | | // If no content type is available, assume we can handle it (since extension was already validated) |
| | 168 | 35 | | return true; |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <inheritdoc /> |
| | | 39 | | public ValueTask<MaterializedWorkflow?> TryParseAsync(Blob blob, string content, CancellationToken cancellationToken |
| | | 40 | | { |
| | | 41 | | try |
| | | 42 | | { |
| | 168 | 43 | | var workflowDefinitionModel = activitySerializer.Deserialize<WorkflowDefinitionModel>(content); |
| | 168 | 44 | | var workflow = workflowDefinitionMapper.Map(workflowDefinitionModel); |
| | | 45 | | |
| | 168 | 46 | | var materialized = new MaterializedWorkflow( |
| | 168 | 47 | | workflow, |
| | 168 | 48 | | ProviderName: "FluentStorage", |
| | 168 | 49 | | MaterializerName: JsonWorkflowMaterializer.MaterializerName, |
| | 168 | 50 | | MaterializerContext: null, |
| | 168 | 51 | | OriginalSource: content // Preserve full JSON for symmetric round-trip |
| | 168 | 52 | | ); |
| | | 53 | | |
| | 168 | 54 | | return new(materialized); |
| | | 55 | | } |
| | 0 | 56 | | catch (Exception ex) |
| | | 57 | | { |
| | | 58 | | // Intentionally catching all exceptions here to gracefully handle any failure during workflow parsing. |
| | | 59 | | // This includes JsonException, IOException, deserialization errors, or any unexpected exceptions. |
| | | 60 | | // Since this is a format handler for user-provided files, we want to log and skip invalid files |
| | | 61 | | // rather than crashing the workflow loading process. |
| | 0 | 62 | | logger.LogWarning(ex, "Failed to parse JSON workflow from blob '{BlobPath}'. The file will be skipped.", blo |
| | | 63 | | |
| | | 64 | | // Return null to indicate this handler can't process the content |
| | 0 | 65 | | return new((MaterializedWorkflow?)null); |
| | | 66 | | } |
| | 168 | 67 | | } |
| | | 68 | | } |