| | | 1 | | using System.IO.Compression; |
| | | 2 | | using Elsa.Workflows.Management.Models; |
| | | 3 | | using Microsoft.AspNetCore.Http; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Workflows.Api.Endpoints.WorkflowDefinitions.ImportFiles; |
| | | 6 | | |
| | | 7 | | internal static class WorkflowDefinitionImportFileReader |
| | | 8 | | { |
| | | 9 | | public static async Task<IReadOnlyCollection<WorkflowDefinitionModel>> ReadAsync(IFormFileCollection files, IApiSeri |
| | | 10 | | { |
| | 1 | 11 | | var models = new List<WorkflowDefinitionModel>(); |
| | | 12 | | |
| | 6 | 13 | | foreach (var file in files) |
| | | 14 | | { |
| | 2 | 15 | | if (hasResponseStarted()) |
| | 0 | 16 | | return models; |
| | | 17 | | |
| | 2 | 18 | | await using var fileStream = file.OpenReadStream(); |
| | | 19 | | |
| | 2 | 20 | | if (file.ContentType == "application/json") |
| | | 21 | | { |
| | 2 | 22 | | models.Add(await ReadJsonStreamAsync(fileStream, apiSerializer, cancellationToken)); |
| | 2 | 23 | | continue; |
| | | 24 | | } |
| | | 25 | | |
| | 0 | 26 | | using var zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Read); |
| | | 27 | | |
| | 0 | 28 | | foreach (var entry in zipArchive.Entries) |
| | | 29 | | { |
| | 0 | 30 | | if (hasResponseStarted()) |
| | 0 | 31 | | return models; |
| | | 32 | | |
| | 0 | 33 | | if (!entry.FullName.EndsWith(".json", StringComparison.OrdinalIgnoreCase)) |
| | | 34 | | continue; |
| | | 35 | | |
| | 0 | 36 | | await using var jsonStream = entry.Open(); |
| | 0 | 37 | | models.Add(await ReadJsonStreamAsync(jsonStream, apiSerializer, cancellationToken)); |
| | 0 | 38 | | } |
| | 0 | 39 | | } |
| | | 40 | | |
| | 1 | 41 | | return models; |
| | 1 | 42 | | } |
| | | 43 | | |
| | | 44 | | private static async Task<WorkflowDefinitionModel> ReadJsonStreamAsync(Stream jsonStream, IApiSerializer apiSerializ |
| | | 45 | | { |
| | 2 | 46 | | using var reader = new StreamReader(jsonStream); |
| | 2 | 47 | | var json = await reader.ReadToEndAsync(cancellationToken); |
| | 2 | 48 | | return apiSerializer.Deserialize<WorkflowDefinitionModel>(json); |
| | 2 | 49 | | } |
| | | 50 | | } |