| | | 1 | | using Bpmn.Interchange; |
| | | 2 | | using Elsa.Abstractions; |
| | | 3 | | using Elsa.Bpmn.Interchange.Services; |
| | | 4 | | using JetBrains.Annotations; |
| | | 5 | | using Microsoft.AspNetCore.Http; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Bpmn.Interchange.Endpoints.Bpmn.Analyze; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Reads a <c>.bpmn</c> document and reports the element-scoped Info/Degraded/Dropped findings a read would produce, |
| | | 11 | | /// without persisting anything. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// A thin wrapper over <see cref="BpmnInterchangeDocumentService.Analyze"/>, which runs the same code |
| | | 15 | | /// <see cref="Import.Import"/> reads with — see its remarks for why that is what makes this a genuine preview rather |
| | | 16 | | /// than a second opinion that can disagree with the import that follows it. |
| | | 17 | | /// </remarks> |
| | | 18 | | [UsedImplicitly] |
| | 14 | 19 | | internal sealed class Analyze(BpmnInterchangeDocumentService documentService) : ElsaEndpointWithoutRequest<BpmnImportAna |
| | | 20 | | { |
| | | 21 | | /// <inheritdoc /> |
| | | 22 | | public override void Configure() |
| | | 23 | | { |
| | 11 | 24 | | Post("bpmn/analyze"); |
| | 11 | 25 | | AllowFileUploads(); |
| | 11 | 26 | | ConfigurePermissions("read:workflow-definitions"); |
| | 11 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <inheritdoc /> |
| | | 30 | | public override async Task HandleAsync(CancellationToken cancellationToken) |
| | | 31 | | { |
| | 3 | 32 | | if (Files.Count != 1) |
| | | 33 | | { |
| | 2 | 34 | | AddError("Upload exactly one .bpmn file."); |
| | 2 | 35 | | await Send.ErrorsAsync(StatusCodes.Status400BadRequest, cancellationToken); |
| | 2 | 36 | | return; |
| | | 37 | | } |
| | | 38 | | |
| | 1 | 39 | | var xml = await BpmnUploadedFileReader.ReadTextAsync(Files[0], cancellationToken); |
| | | 40 | | |
| | | 41 | | try |
| | | 42 | | { |
| | 1 | 43 | | var analysis = documentService.Analyze(xml); |
| | 1 | 44 | | await Send.OkAsync(BpmnImportAnalysisModel.From(analysis), cancellationToken); |
| | 1 | 45 | | } |
| | 0 | 46 | | catch (BpmnInterchangeException exception) |
| | | 47 | | { |
| | 0 | 48 | | AddError(exception.Message); |
| | 0 | 49 | | await Send.ErrorsAsync(StatusCodes.Status400BadRequest, cancellationToken); |
| | | 50 | | } |
| | 3 | 51 | | } |
| | | 52 | | } |