| | | 1 | | using System.IO.Compression; |
| | | 2 | | using Elsa.Abstractions; |
| | | 3 | | using Elsa.Workflows.Api.Constants; |
| | | 4 | | using Elsa.Workflows.Api.Requirements; |
| | | 5 | | using Elsa.Workflows.Management; |
| | | 6 | | using Elsa.Workflows.Management.Mappers; |
| | | 7 | | using Elsa.Workflows.Management.Models; |
| | | 8 | | using JetBrains.Annotations; |
| | | 9 | | using Microsoft.AspNetCore.Authorization; |
| | | 10 | | using Microsoft.AspNetCore.Http; |
| | | 11 | | |
| | | 12 | | namespace Elsa.Workflows.Api.Endpoints.WorkflowDefinitions.ImportFiles; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Imports JSON and/or ZIP files containing a workflow definitions. |
| | | 16 | | /// </summary> |
| | | 17 | | [PublicAPI] |
| | | 18 | | internal class ImportFiles : ElsaEndpoint<WorkflowDefinitionModel> |
| | | 19 | | { |
| | | 20 | | private readonly IWorkflowDefinitionService _workflowDefinitionService; |
| | | 21 | | private readonly IWorkflowDefinitionImporter _workflowDefinitionImporter; |
| | | 22 | | private readonly WorkflowDefinitionMapper _workflowDefinitionMapper; |
| | | 23 | | private readonly IApiSerializer _apiSerializer; |
| | | 24 | | private readonly IAuthorizationService _authorizationService; |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | 1 | 27 | | public ImportFiles( |
| | 1 | 28 | | IWorkflowDefinitionService workflowDefinitionService, |
| | 1 | 29 | | IWorkflowDefinitionImporter workflowDefinitionImporter, |
| | 1 | 30 | | WorkflowDefinitionMapper workflowDefinitionMapper, |
| | 1 | 31 | | IApiSerializer apiSerializer, |
| | 1 | 32 | | IAuthorizationService authorizationService) |
| | | 33 | | { |
| | 1 | 34 | | _workflowDefinitionService = workflowDefinitionService; |
| | 1 | 35 | | _workflowDefinitionImporter = workflowDefinitionImporter; |
| | 1 | 36 | | _workflowDefinitionMapper = workflowDefinitionMapper; |
| | 1 | 37 | | _apiSerializer = apiSerializer; |
| | 1 | 38 | | _authorizationService = authorizationService; |
| | 1 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <inheritdoc /> |
| | | 42 | | public override void Configure() |
| | | 43 | | { |
| | 1 | 44 | | Post("workflow-definitions/import-files"); |
| | 1 | 45 | | ConfigurePermissions("write:workflow-definitions"); |
| | 1 | 46 | | AllowFileUploads(); |
| | 1 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | | 50 | | public override async Task HandleAsync(WorkflowDefinitionModel model, CancellationToken cancellationToken) |
| | | 51 | | { |
| | 0 | 52 | | var authorizationResult = await _authorizationService.AuthorizeAsync(User, new NotReadOnlyResource(), Authorizat |
| | | 53 | | |
| | 0 | 54 | | if (!authorizationResult.Succeeded) |
| | | 55 | | { |
| | 0 | 56 | | await Send.ForbiddenAsync(cancellationToken); |
| | 0 | 57 | | return; |
| | | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | if (Files.Any()) |
| | | 61 | | { |
| | 0 | 62 | | var count = await ImportFilesAsync(Files, cancellationToken); |
| | | 63 | | |
| | 0 | 64 | | if (!ValidationFailed) |
| | 0 | 65 | | await Send.OkAsync(new { Count = count }, cancellationToken); |
| | | 66 | | } |
| | | 67 | | |
| | 0 | 68 | | if (ValidationFailed) |
| | 0 | 69 | | await Send.ErrorsAsync(400, cancellationToken); |
| | 0 | 70 | | } |
| | | 71 | | |
| | | 72 | | private async Task<int> ImportFilesAsync(IFormFileCollection files, CancellationToken cancellationToken) |
| | | 73 | | { |
| | 0 | 74 | | var count = 0; |
| | | 75 | | |
| | 0 | 76 | | foreach (var file in files) |
| | | 77 | | { |
| | 0 | 78 | | var fileStream = file.OpenReadStream(); |
| | | 79 | | |
| | | 80 | | // Check if the file is a JSON file or a ZIP file. |
| | 0 | 81 | | var isJsonFile = file.ContentType == "application/json"; |
| | | 82 | | |
| | | 83 | | // If the file is a JSON file, read it. |
| | 0 | 84 | | if (isJsonFile) |
| | | 85 | | { |
| | 0 | 86 | | await ImportJsonStreamAsync(fileStream, cancellationToken); |
| | 0 | 87 | | count++; |
| | | 88 | | } |
| | | 89 | | else |
| | | 90 | | { |
| | | 91 | | // If the file is a ZIP file, extract the JSON files and read them. |
| | 0 | 92 | | var zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Read); |
| | | 93 | | |
| | 0 | 94 | | foreach (var entry in zipArchive.Entries) |
| | | 95 | | { |
| | 0 | 96 | | if (!entry.FullName.EndsWith(".json", StringComparison.OrdinalIgnoreCase)) |
| | | 97 | | continue; |
| | | 98 | | |
| | 0 | 99 | | var jsonStream = entry.Open(); |
| | 0 | 100 | | await ImportJsonStreamAsync(jsonStream, cancellationToken); |
| | 0 | 101 | | count++; |
| | | 102 | | } |
| | | 103 | | } |
| | | 104 | | } |
| | | 105 | | |
| | 0 | 106 | | return count; |
| | 0 | 107 | | } |
| | | 108 | | |
| | | 109 | | private async Task ImportJsonStreamAsync(Stream jsonStream, CancellationToken cancellationToken) |
| | | 110 | | { |
| | 0 | 111 | | var json = await new StreamReader(jsonStream).ReadToEndAsync(cancellationToken); |
| | 0 | 112 | | var model = _apiSerializer.Deserialize<WorkflowDefinitionModel>(json); |
| | 0 | 113 | | await ImportSingleWorkflowDefinitionAsync(model, cancellationToken); |
| | 0 | 114 | | } |
| | | 115 | | |
| | | 116 | | private async Task<ImportWorkflowResult> ImportSingleWorkflowDefinitionAsync(WorkflowDefinitionModel model, Cancella |
| | | 117 | | { |
| | | 118 | | // Import workflow |
| | 0 | 119 | | var saveWorkflowRequest = new SaveWorkflowDefinitionRequest |
| | 0 | 120 | | { |
| | 0 | 121 | | Model = model, |
| | 0 | 122 | | Publish = false, |
| | 0 | 123 | | }; |
| | | 124 | | |
| | 0 | 125 | | var result = await _workflowDefinitionImporter.ImportAsync(saveWorkflowRequest, cancellationToken); |
| | | 126 | | |
| | 0 | 127 | | if (result.Succeeded) |
| | 0 | 128 | | return result; |
| | | 129 | | |
| | 0 | 130 | | foreach (var validationError in result.ValidationErrors) |
| | 0 | 131 | | AddError(validationError.Message); |
| | | 132 | | |
| | 0 | 133 | | return result; |
| | 0 | 134 | | } |
| | | 135 | | } |