2020-10-26 21:24:40 +02:00
|
|
|
using System.ComponentModel.DataAnnotations;
|
2020-10-26 16:20:47 +02:00
|
|
|
using System.Threading.Tasks;
|
2020-10-26 17:06:29 +02:00
|
|
|
using MalwareMultiScan.Api.Attributes;
|
|
|
|
using MalwareMultiScan.Api.Data.Models;
|
2020-10-26 16:20:47 +02:00
|
|
|
using MalwareMultiScan.Api.Services;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2020-10-25 16:11:36 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
namespace MalwareMultiScan.Api.Controllers
|
|
|
|
{
|
|
|
|
[ApiController]
|
2020-10-27 09:57:39 +02:00
|
|
|
[Route("api/queue")]
|
2020-10-25 16:11:36 +02:00
|
|
|
[Produces("application/json")]
|
|
|
|
public class QueueController : Controller
|
|
|
|
{
|
2020-10-26 16:20:47 +02:00
|
|
|
private readonly ScanResultService _scanResultService;
|
|
|
|
|
2020-10-26 17:06:29 +02:00
|
|
|
public QueueController(ScanResultService scanResultService)
|
2020-10-26 16:20:47 +02:00
|
|
|
{
|
|
|
|
_scanResultService = scanResultService;
|
|
|
|
}
|
2020-10-26 17:06:29 +02:00
|
|
|
|
2020-10-26 16:20:47 +02:00
|
|
|
[HttpPost("file")]
|
2020-10-26 17:06:29 +02:00
|
|
|
[ProducesResponseType(typeof(ScanResult), StatusCodes.Status201Created)]
|
2020-10-26 16:20:47 +02:00
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
2020-10-26 21:24:40 +02:00
|
|
|
public async Task<IActionResult> ScanFile(
|
|
|
|
[Required, MaxFileSize] IFormFile file)
|
2020-10-25 16:11:36 +02:00
|
|
|
{
|
2020-10-26 16:20:47 +02:00
|
|
|
var result = await _scanResultService.CreateScanResult();
|
|
|
|
|
|
|
|
string storedFileId;
|
|
|
|
|
|
|
|
await using (var uploadFileStream = file.OpenReadStream())
|
|
|
|
storedFileId = await _scanResultService.StoreFile(file.Name, uploadFileStream);
|
2020-10-26 17:06:29 +02:00
|
|
|
|
|
|
|
await _scanResultService.QueueUrlScan(result, Url.Action("Index", "Download", new {id = storedFileId},
|
2020-10-26 16:20:47 +02:00
|
|
|
Request.Scheme, Request.Host.Value));
|
|
|
|
|
2020-10-26 21:24:40 +02:00
|
|
|
return CreatedAtAction("Index", "ScanResults", new {id = result.Id}, result);
|
2020-10-26 16:20:47 +02:00
|
|
|
}
|
2020-10-26 17:06:29 +02:00
|
|
|
|
2020-10-26 16:20:47 +02:00
|
|
|
[HttpPost("url")]
|
2020-10-26 17:06:29 +02:00
|
|
|
[ProducesResponseType(typeof(ScanResult), StatusCodes.Status201Created)]
|
2020-10-26 16:20:47 +02:00
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
2020-10-26 21:24:40 +02:00
|
|
|
public async Task<IActionResult> ScanUrl(
|
|
|
|
[FromForm, Required, IsHttpUrl] string url)
|
2020-10-26 16:20:47 +02:00
|
|
|
{
|
|
|
|
var result = await _scanResultService.CreateScanResult();
|
2020-10-26 17:06:29 +02:00
|
|
|
|
2020-10-26 21:24:40 +02:00
|
|
|
await _scanResultService.QueueUrlScan(result, url);
|
2020-10-26 16:20:47 +02:00
|
|
|
|
2020-10-26 21:24:40 +02:00
|
|
|
return CreatedAtAction("Index", "ScanResults", new {id = result.Id}, result);
|
2020-10-25 16:11:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|