| | | 1 | | using System.IO.Compression; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using Elsa.Abstractions; |
| | | 4 | | using Elsa.Common.Models; |
| | | 5 | | using Elsa.Workflows.Management; |
| | | 6 | | using Elsa.Workflows.Management.Entities; |
| | | 7 | | using Elsa.Workflows.Management.Filters; |
| | | 8 | | using Elsa.Workflows.Management.Mappers; |
| | | 9 | | using Elsa.Workflows.Management.Models; |
| | | 10 | | using Humanizer; |
| | | 11 | | using JetBrains.Annotations; |
| | | 12 | | |
| | | 13 | | namespace Elsa.Workflows.Api.Endpoints.WorkflowDefinitions.Export; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Exports the specified workflow definition as JSON download. |
| | | 17 | | /// </summary> |
| | | 18 | | [UsedImplicitly] |
| | | 19 | | internal class Export : ElsaEndpoint<Request> |
| | | 20 | | { |
| | | 21 | | private readonly IApiSerializer _serializer; |
| | | 22 | | private readonly IWorkflowDefinitionStore _store; |
| | | 23 | | private readonly WorkflowDefinitionMapper _workflowDefinitionMapper; |
| | | 24 | | |
| | | 25 | | /// <inheritdoc /> |
| | 1 | 26 | | public Export( |
| | 1 | 27 | | IWorkflowDefinitionStore store, |
| | 1 | 28 | | IWorkflowDefinitionService workflowDefinitionService, |
| | 1 | 29 | | IApiSerializer serializer, |
| | 1 | 30 | | WorkflowDefinitionMapper workflowDefinitionMapper, |
| | 1 | 31 | | VariableDefinitionMapper variableDefinitionMapper) |
| | | 32 | | { |
| | 1 | 33 | | _store = store; |
| | 1 | 34 | | _serializer = serializer; |
| | 1 | 35 | | _workflowDefinitionMapper = workflowDefinitionMapper; |
| | 1 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <inheritdoc /> |
| | | 39 | | public override void Configure() |
| | | 40 | | { |
| | 1 | 41 | | Routes("/bulk-actions/export/workflow-definitions", "/workflow-definitions/{definitionId}/export"); |
| | 1 | 42 | | Verbs(FastEndpoints.Http.GET, FastEndpoints.Http.POST); |
| | 1 | 43 | | ConfigurePermissions("read:workflow-definitions"); |
| | 1 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <inheritdoc /> |
| | | 47 | | public override async Task HandleAsync(Request request, CancellationToken cancellationToken) |
| | | 48 | | { |
| | 0 | 49 | | if (request.DefinitionId != null) |
| | 0 | 50 | | await DownloadSingleWorkflowAsync(request.DefinitionId, request.VersionOptions, cancellationToken); |
| | 0 | 51 | | else if (request.Ids != null) |
| | 0 | 52 | | await DownloadMultipleWorkflowsAsync(request.Ids, cancellationToken); |
| | 0 | 53 | | else await Send.NoContentAsync(cancellationToken); |
| | 0 | 54 | | } |
| | | 55 | | |
| | | 56 | | private async Task DownloadMultipleWorkflowsAsync(ICollection<string> ids, CancellationToken cancellationToken) |
| | | 57 | | { |
| | 0 | 58 | | List<WorkflowDefinition> definitions = (await _store.FindManyAsync(new() |
| | 0 | 59 | | { |
| | 0 | 60 | | Ids = ids |
| | 0 | 61 | | }, cancellationToken)).ToList(); |
| | | 62 | | |
| | 0 | 63 | | if (!definitions.Any()) |
| | | 64 | | { |
| | 0 | 65 | | await Send.NoContentAsync(cancellationToken); |
| | 0 | 66 | | return; |
| | | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | var zipStream = new MemoryStream(); |
| | 0 | 70 | | using (var zipArchive = new ZipArchive(zipStream, ZipArchiveMode.Create, true)) |
| | | 71 | | { |
| | | 72 | | // Create a JSON file for each workflow definition: |
| | 0 | 73 | | foreach (var definition in definitions) |
| | | 74 | | { |
| | 0 | 75 | | var model = await CreateWorkflowModelAsync(definition, cancellationToken); |
| | 0 | 76 | | var binaryJson = await SerializeWorkflowDefinitionAsync(model, cancellationToken); |
| | 0 | 77 | | var fileName = GetFileName(model); |
| | 0 | 78 | | var entry = zipArchive.CreateEntry(fileName, CompressionLevel.Optimal); |
| | 0 | 79 | | await using var entryStream = entry.Open(); |
| | 0 | 80 | | await entryStream.WriteAsync(binaryJson, cancellationToken); |
| | 0 | 81 | | } |
| | 0 | 82 | | } |
| | | 83 | | |
| | | 84 | | // Send the zip file to the client: |
| | 0 | 85 | | zipStream.Position = 0; |
| | 0 | 86 | | await Send.BytesAsync(zipStream.ToArray(), "workflow-definitions.zip", cancellation: cancellationToken); |
| | 0 | 87 | | } |
| | | 88 | | |
| | | 89 | | private async Task DownloadSingleWorkflowAsync(string definitionId, string? versionOptions, CancellationToken cancel |
| | | 90 | | { |
| | 0 | 91 | | var parsedVersionOptions = string.IsNullOrEmpty(versionOptions) ? VersionOptions.Latest : VersionOptions.FromStr |
| | 0 | 92 | | WorkflowDefinition? definition = (await _store.FindManyAsync(new() |
| | 0 | 93 | | { |
| | 0 | 94 | | DefinitionId = definitionId, |
| | 0 | 95 | | VersionOptions = parsedVersionOptions |
| | 0 | 96 | | }, cancellationToken)).FirstOrDefault(); |
| | | 97 | | |
| | 0 | 98 | | if (definition == null) |
| | | 99 | | { |
| | 0 | 100 | | await Send.NotFoundAsync(cancellationToken); |
| | 0 | 101 | | return; |
| | | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | var model = await CreateWorkflowModelAsync(definition, cancellationToken); |
| | 0 | 105 | | var binaryJson = await SerializeWorkflowDefinitionAsync(model, cancellationToken); |
| | 0 | 106 | | var fileName = GetFileName(model); |
| | | 107 | | |
| | 0 | 108 | | await Send.BytesAsync(binaryJson, fileName, cancellation: cancellationToken); |
| | 0 | 109 | | } |
| | | 110 | | |
| | | 111 | | private string GetFileName(WorkflowDefinitionModel definition) |
| | | 112 | | { |
| | 0 | 113 | | var hasWorkflowName = !string.IsNullOrWhiteSpace(definition.Name); |
| | 0 | 114 | | var workflowName = hasWorkflowName ? definition.Name!.Trim() : definition.DefinitionId; |
| | 0 | 115 | | var fileName = $"workflow-definition-{workflowName.Underscore().Dasherize().ToLowerInvariant()}.json"; |
| | 0 | 116 | | return fileName; |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | private async Task<byte[]> SerializeWorkflowDefinitionAsync(WorkflowDefinitionModel model, CancellationToken cancell |
| | | 120 | | { |
| | 0 | 121 | | var serializerOptions = _serializer.GetOptions(); |
| | 0 | 122 | | var document = JsonSerializer.SerializeToDocument(model, serializerOptions); |
| | 0 | 123 | | var rootElement = document.RootElement; |
| | | 124 | | |
| | 0 | 125 | | using var output = new MemoryStream(); |
| | 0 | 126 | | await using var writer = new Utf8JsonWriter(output); |
| | | 127 | | |
| | 0 | 128 | | writer.WriteStartObject(); |
| | 0 | 129 | | writer.WriteString("$schema", "https://elsaworkflows.io/schemas/workflow-definition/v3.0.0/schema.json"); |
| | | 130 | | |
| | 0 | 131 | | foreach (var property in rootElement.EnumerateObject()) |
| | 0 | 132 | | property.WriteTo(writer); |
| | | 133 | | |
| | 0 | 134 | | writer.WriteEndObject(); |
| | | 135 | | |
| | 0 | 136 | | await writer.FlushAsync(cancellationToken); |
| | 0 | 137 | | var binaryJson = output.ToArray(); |
| | 0 | 138 | | return binaryJson; |
| | 0 | 139 | | } |
| | | 140 | | |
| | | 141 | | private async Task<WorkflowDefinitionModel> CreateWorkflowModelAsync(WorkflowDefinition definition, CancellationToke |
| | | 142 | | { |
| | 0 | 143 | | return await _workflowDefinitionMapper.MapAsync(definition, cancellationToken); |
| | 0 | 144 | | } |
| | | 145 | | } |