2020-10-25 16:11:36 +02:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Threading.Tasks;
|
2020-10-26 16:20:47 +02:00
|
|
|
using EasyNetQ;
|
2020-10-25 16:11:36 +02:00
|
|
|
using MalwareMultiScan.Api.Data.Configuration;
|
2020-10-26 16:20:47 +02:00
|
|
|
using MalwareMultiScan.Api.Data.Models;
|
|
|
|
using MalwareMultiScan.Shared.Data.Messages;
|
2020-10-25 16:11:36 +02:00
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using YamlDotNet.Serialization;
|
|
|
|
using YamlDotNet.Serialization.NamingConventions;
|
|
|
|
|
|
|
|
namespace MalwareMultiScan.Api.Services
|
|
|
|
{
|
|
|
|
public class ScanBackendService
|
|
|
|
{
|
2020-10-26 16:20:47 +02:00
|
|
|
private readonly IBus _bus;
|
2020-10-25 16:11:36 +02:00
|
|
|
|
2020-10-26 16:20:47 +02:00
|
|
|
public ScanBackendService(IConfiguration configuration, IBus bus)
|
2020-10-25 16:11:36 +02:00
|
|
|
{
|
2020-10-26 16:20:47 +02:00
|
|
|
_bus = bus;
|
|
|
|
|
2020-10-25 16:11:36 +02:00
|
|
|
var configurationPath = configuration.GetValue<string>("BackendsConfiguration");
|
|
|
|
|
|
|
|
if (!File.Exists(configurationPath))
|
|
|
|
throw new FileNotFoundException("Missing BackendsConfiguration YAML file", configurationPath);
|
|
|
|
|
|
|
|
var configurationContent = File.ReadAllText(configurationPath);
|
|
|
|
|
|
|
|
var deserializer = new DeserializerBuilder()
|
|
|
|
.WithNamingConvention(CamelCaseNamingConvention.Instance)
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
List = deserializer.Deserialize<ScanBackend[]>(configurationContent);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ScanBackend[] List { get; }
|
2020-10-26 16:20:47 +02:00
|
|
|
|
|
|
|
public async Task QueueUrlScan(ScanResult result, ScanBackend backend, string fileUrl)
|
2020-10-25 16:11:36 +02:00
|
|
|
{
|
2020-10-26 16:20:47 +02:00
|
|
|
await _bus.SendAsync(backend.Id, new ScanRequestMessage
|
2020-10-25 16:11:36 +02:00
|
|
|
{
|
2020-10-26 16:20:47 +02:00
|
|
|
Id = result.Id,
|
|
|
|
Uri = new Uri(fileUrl)
|
2020-10-25 16:11:36 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|