-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDownloadFileQueryHandler.cs
53 lines (48 loc) · 2.03 KB
/
DownloadFileQueryHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using Altinn.Broker.Core.Application;
using Altinn.Broker.Core.Domain.Enums;
using Altinn.Broker.Core.Repositories;
using Microsoft.Extensions.Logging;
using OneOf;
namespace Altinn.Broker.Application.DownloadFileQuery;
public class DownloadFileQueryHandler : IHandler<DownloadFileQueryRequest, DownloadFileQueryResponse>
{
private readonly IServiceOwnerRepository _serviceOwnerRepository;
private readonly IFileRepository _fileRepository;
private readonly IBrokerStorageService _brokerStorageService;
private readonly ILogger<DownloadFileQueryHandler> _logger;
public DownloadFileQueryHandler(IServiceOwnerRepository serviceOwnerRepository, IFileRepository fileRepository, IBrokerStorageService brokerStorageService, ILogger<DownloadFileQueryHandler> logger)
{
_serviceOwnerRepository = serviceOwnerRepository;
_fileRepository = fileRepository;
_brokerStorageService = brokerStorageService;
_logger = logger;
}
public async Task<OneOf<DownloadFileQueryResponse, Error>> Process(DownloadFileQueryRequest request)
{
var serviceOwner = await _serviceOwnerRepository.GetServiceOwner(request.Supplier);
if (serviceOwner is null)
{
return Errors.ServiceOwnerNotConfigured;
};
var file = await _fileRepository.GetFile(request.FileId);
if (file is null)
{
return Errors.FileNotFound;
}
if (!file.ActorEvents.Any(actorEvent => actorEvent.Actor.ActorExternalId == request.Consumer))
{
return Errors.FileNotFound;
}
if (string.IsNullOrWhiteSpace(file?.FileLocation))
{
return Errors.NoFileUploaded;
}
var downloadStream = await _brokerStorageService.DownloadFile(serviceOwner, file);
await _fileRepository.AddReceipt(request.FileId, ActorFileStatus.DownloadStarted, request.Consumer);
return new DownloadFileQueryResponse()
{
Filename = file.Filename,
Stream = downloadStream
};
}
}