| | | 1 | | using System.IO.Compression; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using Elsa.Abstractions; |
| | | 4 | | using Elsa.Workflows.Api.Models; |
| | | 5 | | using Elsa.Workflows.Management; |
| | | 6 | | using Elsa.Workflows.Runtime; |
| | | 7 | | using Elsa.Workflows.Runtime.Entities; |
| | | 8 | | using JetBrains.Annotations; |
| | | 9 | | using Microsoft.AspNetCore.Http; |
| | | 10 | | |
| | | 11 | | namespace Elsa.Workflows.Api.Endpoints.WorkflowInstances.Import; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Imports JSON and/or ZIP files containing a workflow instances. |
| | | 15 | | /// </summary> |
| | | 16 | | [PublicAPI] |
| | | 17 | | internal class Import : ElsaEndpointWithoutRequest<Response> |
| | | 18 | | { |
| | | 19 | | private readonly IWorkflowInstanceManager _workflowInstanceManager; |
| | | 20 | | private readonly IWorkflowInstanceStore _workflowInstanceStore; |
| | | 21 | | private readonly IActivityExecutionStore _activityExecutionStore; |
| | | 22 | | private readonly IWorkflowExecutionLogStore _workflowExecutionLogStore; |
| | | 23 | | private readonly IBookmarkStore _bookmarkStore; |
| | | 24 | | private readonly IWorkflowStateSerializer _workflowStateSerializer; |
| | | 25 | | private readonly IPayloadSerializer _payloadSerializer; |
| | | 26 | | private readonly ISafeSerializer _safeSerializer; |
| | | 27 | | |
| | | 28 | | /// <inheritdoc /> |
| | 1 | 29 | | public Import( |
| | 1 | 30 | | IWorkflowInstanceManager workflowInstanceManager, |
| | 1 | 31 | | IWorkflowInstanceStore workflowInstanceStore, |
| | 1 | 32 | | IActivityExecutionStore activityExecutionStore, |
| | 1 | 33 | | IWorkflowExecutionLogStore workflowExecutionLogStore, |
| | 1 | 34 | | IBookmarkStore bookmarkStore, |
| | 1 | 35 | | IWorkflowStateSerializer workflowStateSerializer, |
| | 1 | 36 | | IPayloadSerializer payloadSerializer, |
| | 1 | 37 | | ISafeSerializer safeSerializer) |
| | | 38 | | { |
| | 1 | 39 | | _workflowInstanceManager = workflowInstanceManager; |
| | 1 | 40 | | _workflowInstanceStore = workflowInstanceStore; |
| | 1 | 41 | | _activityExecutionStore = activityExecutionStore; |
| | 1 | 42 | | _workflowExecutionLogStore = workflowExecutionLogStore; |
| | 1 | 43 | | _bookmarkStore = bookmarkStore; |
| | 1 | 44 | | _workflowStateSerializer = workflowStateSerializer; |
| | 1 | 45 | | _payloadSerializer = payloadSerializer; |
| | 1 | 46 | | _safeSerializer = safeSerializer; |
| | 1 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | | 50 | | public override void Configure() |
| | | 51 | | { |
| | 1 | 52 | | Post("/bulk-actions/import/workflow-instances"); |
| | 1 | 53 | | ConfigurePermissions("write:workflow-instances"); |
| | 1 | 54 | | AllowFileUploads(); |
| | 1 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | | 58 | | public override async Task HandleAsync(CancellationToken cancellationToken) |
| | | 59 | | { |
| | 0 | 60 | | var count = await ImportFilesAsync(Files, cancellationToken); |
| | 0 | 61 | | var response = new Response { Imported = count }; |
| | | 62 | | |
| | 0 | 63 | | await Send.OkAsync(response, cancellationToken); |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | private async Task<int> ImportFilesAsync(IFormFileCollection files, CancellationToken cancellationToken) |
| | | 67 | | { |
| | 0 | 68 | | var count = 0; |
| | | 69 | | |
| | 0 | 70 | | foreach (var file in files) |
| | | 71 | | { |
| | 0 | 72 | | var fileStream = file.OpenReadStream(); |
| | | 73 | | |
| | | 74 | | // Check if the file is a JSON file or a ZIP file. |
| | 0 | 75 | | var isJsonFile = file.ContentType == "application/json"; |
| | | 76 | | |
| | | 77 | | // If the file is a JSON file, read it. |
| | 0 | 78 | | if (isJsonFile) |
| | | 79 | | { |
| | 0 | 80 | | await ImportJsonStreamAsync(fileStream, cancellationToken); |
| | 0 | 81 | | count++; |
| | | 82 | | } |
| | | 83 | | else |
| | | 84 | | { |
| | | 85 | | // If the file is a ZIP file, extract the JSON files and read them. |
| | 0 | 86 | | var zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Read); |
| | | 87 | | |
| | 0 | 88 | | foreach (var entry in zipArchive.Entries) |
| | | 89 | | { |
| | 0 | 90 | | if (!entry.FullName.EndsWith(".json", StringComparison.OrdinalIgnoreCase)) |
| | | 91 | | continue; |
| | | 92 | | |
| | 0 | 93 | | var jsonStream = entry.Open(); |
| | 0 | 94 | | await ImportJsonStreamAsync(jsonStream, cancellationToken); |
| | 0 | 95 | | count++; |
| | | 96 | | } |
| | | 97 | | } |
| | | 98 | | } |
| | | 99 | | |
| | 0 | 100 | | return count; |
| | 0 | 101 | | } |
| | | 102 | | |
| | | 103 | | private async Task ImportJsonStreamAsync(Stream jsonStream, CancellationToken cancellationToken) |
| | | 104 | | { |
| | 0 | 105 | | var json = await new StreamReader(jsonStream).ReadToEndAsync(); |
| | 0 | 106 | | var model = JsonSerializer.Deserialize<ExportedWorkflowState>(json)!; |
| | 0 | 107 | | await ImportSingleWorkflowInstanceAsync(model, cancellationToken); |
| | 0 | 108 | | } |
| | | 109 | | |
| | | 110 | | private async Task ImportSingleWorkflowInstanceAsync(ExportedWorkflowState model, CancellationToken cancellationToke |
| | | 111 | | { |
| | 0 | 112 | | var workflowState = _workflowStateSerializer.Deserialize(model.WorkflowState); |
| | 0 | 113 | | await _workflowInstanceManager.SaveAsync(workflowState, cancellationToken); |
| | | 114 | | |
| | 0 | 115 | | if (model.Bookmarks != null) |
| | | 116 | | { |
| | 0 | 117 | | var bookmarksElement = model.Bookmarks.Value.EnumerateArray().ToList(); |
| | 0 | 118 | | var bookmarks = bookmarksElement.Select(x => |
| | 0 | 119 | | { |
| | 0 | 120 | | var bookmarkId = x.GetProperty("id").GetString()!; |
| | 0 | 121 | | var activityTypeName = x.GetProperty("activityTypeName").GetString()!; |
| | 0 | 122 | | var workflowInstanceId = x.GetProperty("workflowInstanceId").GetString()!; |
| | 0 | 123 | | var activityInstanceId = x.GetProperty("activityInstanceId").GetString(); |
| | 0 | 124 | | var hash = x.GetProperty("hash").GetString()!; |
| | 0 | 125 | | var correlationId = x.GetProperty("correlationId").GetString(); |
| | 0 | 126 | | var createdAt = x.GetProperty("createdAt").GetDateTimeOffset(); |
| | 0 | 127 | | var payloadElement = x.GetProperty("payload"); |
| | 0 | 128 | | var metadataElement = x.GetProperty("metadata"); |
| | 0 | 129 | | var payload = _payloadSerializer.Deserialize<object>(payloadElement); |
| | 0 | 130 | | var metadata = _payloadSerializer.Deserialize<IDictionary<string, string>>(metadataElement); |
| | 0 | 131 | | |
| | 0 | 132 | | return new StoredBookmark |
| | 0 | 133 | | { |
| | 0 | 134 | | Id = bookmarkId, |
| | 0 | 135 | | Name = activityTypeName, |
| | 0 | 136 | | Hash = hash, |
| | 0 | 137 | | WorkflowInstanceId = workflowInstanceId, |
| | 0 | 138 | | CreatedAt = createdAt, |
| | 0 | 139 | | ActivityInstanceId = activityInstanceId, |
| | 0 | 140 | | CorrelationId = correlationId, |
| | 0 | 141 | | Payload = payload, |
| | 0 | 142 | | Metadata = metadata |
| | 0 | 143 | | }; |
| | 0 | 144 | | }).ToList(); |
| | 0 | 145 | | await _bookmarkStore.SaveManyAsync(bookmarks, cancellationToken); |
| | | 146 | | } |
| | | 147 | | |
| | 0 | 148 | | if (model.ActivityExecutionRecords != null) |
| | | 149 | | { |
| | 0 | 150 | | var activityExecutionRecords = _safeSerializer.Deserialize<ICollection<ActivityExecutionRecord>>(model.Activ |
| | 0 | 151 | | await _activityExecutionStore.SaveManyAsync(activityExecutionRecords, cancellationToken); |
| | | 152 | | } |
| | | 153 | | |
| | 0 | 154 | | if (model.WorkflowExecutionLogRecords != null) |
| | | 155 | | { |
| | 0 | 156 | | var workflowExecutionLogRecords = _safeSerializer.Deserialize<ICollection<WorkflowExecutionLogRecord>>(model |
| | 0 | 157 | | await _workflowExecutionLogStore.SaveManyAsync(workflowExecutionLogRecords, cancellationToken); |
| | | 158 | | } |
| | 0 | 159 | | } |
| | | 160 | | } |