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