|
1 | 1 | using System.Diagnostics; |
2 | 2 | using System.Net; |
3 | 3 | using System.Net.Http.Headers; |
4 | | -using System.Net.Http.Json; |
5 | 4 | using System.Text.Json; |
6 | 5 | using Altinn.App.Core.Configuration; |
7 | 6 | using Altinn.App.Core.Constants; |
8 | 7 | using Altinn.App.Core.Features.Correspondence.Exceptions; |
9 | 8 | using Altinn.App.Core.Features.Correspondence.Models; |
10 | | -using Altinn.App.Core.Features.Correspondence.Models.Response; |
11 | 9 | using Altinn.App.Core.Models; |
12 | 10 | using Microsoft.AspNetCore.Mvc; |
13 | 11 | using Microsoft.Extensions.Logging; |
@@ -52,57 +50,6 @@ public async Task<SendCorrespondenceResponse> Send( |
52 | 50 |
|
53 | 51 | try |
54 | 52 | { |
55 | | - if ( |
56 | | - payload.CorrespondenceRequest.Content.Attachments.Count > 0 |
57 | | - && payload.CorrespondenceRequest.Content.Attachments.All(a => a is CorrespondenceStreamedAttachment) |
58 | | - ) |
59 | | - { |
60 | | - var pollingJobs = new List<Task>(); |
61 | | - var premadeAttachments = new List<Guid>(); |
62 | | - foreach ( |
63 | | - CorrespondenceStreamedAttachment attachment in payload.CorrespondenceRequest.Content.Attachments |
64 | | - ) |
65 | | - { |
66 | | - var initializeAttachmentPayload = new AttachmentPayload |
67 | | - { |
68 | | - DisplayName = attachment.Filename, |
69 | | - FileName = attachment.Filename, |
70 | | - IsEncrypted = attachment.IsEncrypted ?? false, |
71 | | - ResourceId = payload.CorrespondenceRequest.ResourceId, |
72 | | - SendersReference = payload.CorrespondenceRequest.SendersReference + " - " + attachment.Filename, |
73 | | - }; |
74 | | - var initializeAttachmentRequest = await AuthenticatedHttpRequestFactory( |
75 | | - method: HttpMethod.Post, |
76 | | - uri: GetUri("attachment"), |
77 | | - content: JsonContent.Create(initializeAttachmentPayload), |
78 | | - payload: payload |
79 | | - ); |
80 | | - var initializeAttachmentResponse = await HandleServerCommunication<SendCorrespondenceResponse>( |
81 | | - initializeAttachmentRequest, |
82 | | - cancellationToken |
83 | | - ); |
84 | | - var attachmentId = await initializeAttachmentRequest.Content.ReadAsStringAsync(cancellationToken); |
85 | | - attachmentId = attachmentId.Trim('"'); |
86 | | - var attachmentDataContent = new StreamContent(attachment.Data); |
87 | | - attachmentDataContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); |
88 | | - var uploadAttachmentRequest = await AuthenticatedHttpRequestFactory( |
89 | | - method: HttpMethod.Post, |
90 | | - uri: GetUri("attachment/upload"), |
91 | | - content: attachmentDataContent, |
92 | | - payload: payload |
93 | | - ); |
94 | | - var uploadAttachmentResponse = await HandleServerCommunication<SendCorrespondenceResponse>( |
95 | | - uploadAttachmentRequest, |
96 | | - cancellationToken |
97 | | - ); |
98 | | - if (Guid.TryParse(attachmentId, out var guidId)) |
99 | | - { |
100 | | - pollingJobs.Add(PollAttachmentStatus(guidId, payload, cancellationToken)); |
101 | | - } |
102 | | - } |
103 | | - await Task.WhenAll(pollingJobs); |
104 | | - payload.CorrespondenceRequest.ExistingAttachments = premadeAttachments; |
105 | | - } |
106 | 53 | using MultipartFormDataContent content = payload.CorrespondenceRequest.Serialise(); |
107 | 54 | using HttpRequestMessage request = await AuthenticatedHttpRequestFactory( |
108 | 55 | method: HttpMethod.Post, |
@@ -141,55 +88,6 @@ CorrespondenceStreamedAttachment attachment in payload.CorrespondenceRequest.Con |
141 | 88 | } |
142 | 89 | } |
143 | 90 |
|
144 | | - /// <inheritdoc /> |
145 | | - private async Task PollAttachmentStatus( |
146 | | - Guid attachmentId, |
147 | | - CorrespondencePayloadBase payload, |
148 | | - CancellationToken cancellationToken |
149 | | - ) |
150 | | - { |
151 | | - const int maxAttempts = 3; |
152 | | - const int delaySeconds = 5; |
153 | | - |
154 | | - for (int attempt = 1; attempt <= maxAttempts; attempt++) |
155 | | - { |
156 | | - await Task.Delay(TimeSpan.FromSeconds(delaySeconds), cancellationToken); |
157 | | - |
158 | | - try |
159 | | - { |
160 | | - var statusResponse = await AuthenticatedHttpRequestFactory( |
161 | | - method: HttpMethod.Get, |
162 | | - uri: GetUri($"attachment/{attachmentId}"), |
163 | | - content: null, |
164 | | - payload: payload |
165 | | - ); |
166 | | - |
167 | | - var statusContent = await statusResponse.Content.ReadFromJsonAsync<AttachmentOverview>( |
168 | | - cancellationToken |
169 | | - ); |
170 | | - if (statusContent.Status == "Published") |
171 | | - { |
172 | | - break; |
173 | | - } |
174 | | - } |
175 | | - catch (Exception ex) |
176 | | - { |
177 | | - // Log the exception |
178 | | - if (attempt == maxAttempts) |
179 | | - { |
180 | | - // Handle or rethrow the exception after all attempts failed |
181 | | - throw; |
182 | | - } |
183 | | - } |
184 | | - throw new CorrespondenceRequestException( |
185 | | - $"Failure when uploading attachment. Attachment was not published in time. ", |
186 | | - null, |
187 | | - HttpStatusCode.InternalServerError, |
188 | | - "Polling failed" |
189 | | - ); |
190 | | - } |
191 | | - } |
192 | | - |
193 | 91 | /// <inheritdoc/> |
194 | 92 | public async Task<GetCorrespondenceStatusResponse> GetStatus( |
195 | 93 | GetCorrespondenceStatusPayload payload, |
|
0 commit comments