2020-09-01 07:46:07 +02:00
|
|
|
|
namespace Giants.WebApi.Controllers
|
|
|
|
|
{
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Giants.Services.Services;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2020-09-01 20:04:22 +02:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2020-09-01 07:46:07 +02:00
|
|
|
|
|
|
|
|
|
[ApiController]
|
2020-10-12 23:42:44 +02:00
|
|
|
|
[ApiVersion("1.0")]
|
2022-09-05 05:29:59 +02:00
|
|
|
|
[ApiVersion("1.1")]
|
2020-09-01 07:46:07 +02:00
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
public class CrashReportsController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly ICrashReportService crashReportService;
|
|
|
|
|
private readonly IHttpContextAccessor httpContextAccessor;
|
2020-09-01 20:04:22 +02:00
|
|
|
|
private readonly ILogger<CrashReportsController> logger;
|
|
|
|
|
private readonly IConfiguration configuration;
|
2020-09-21 08:19:10 +02:00
|
|
|
|
private const long MaximumSizeInBytes = 10485760; // 10MB
|
2020-09-01 07:46:07 +02:00
|
|
|
|
|
|
|
|
|
public CrashReportsController(
|
|
|
|
|
ICrashReportService crashReportService,
|
2020-09-01 20:04:22 +02:00
|
|
|
|
IHttpContextAccessor httpContextAccessor,
|
|
|
|
|
ILogger<CrashReportsController> logger,
|
|
|
|
|
IConfiguration configuration)
|
2020-09-01 07:46:07 +02:00
|
|
|
|
{
|
|
|
|
|
this.crashReportService = crashReportService;
|
|
|
|
|
this.httpContextAccessor = httpContextAccessor;
|
2020-09-01 20:04:22 +02:00
|
|
|
|
this.logger = logger;
|
|
|
|
|
this.configuration = configuration;
|
2020-09-01 07:46:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public async Task Upload()
|
|
|
|
|
{
|
|
|
|
|
this.ValidateFiles(this.Request.Form.Files);
|
|
|
|
|
|
|
|
|
|
var file = this.Request.Form.Files.First();
|
|
|
|
|
using (var stream = file.OpenReadStream())
|
|
|
|
|
{
|
|
|
|
|
await this.crashReportService.ProcessReport(file.FileName, this.GetRequestIpAddress(), stream).ConfigureAwait(false);
|
2020-09-01 20:04:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool sentryEnabled = Convert.ToBoolean(this.configuration["SentryEnabled"]);
|
|
|
|
|
if (!sentryEnabled)
|
|
|
|
|
{
|
|
|
|
|
this.logger.LogInformation("Skipping Sentry upload; disabled.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (var stream = file.OpenReadStream())
|
|
|
|
|
{
|
|
|
|
|
await this.crashReportService.UploadMinidumpToSentry(file.FileName, stream).ConfigureAwait(false);
|
2020-09-01 07:46:07 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ValidateFiles(IEnumerable<IFormFile> formFiles)
|
|
|
|
|
{
|
|
|
|
|
if (formFiles.Count() != 1)
|
|
|
|
|
{
|
|
|
|
|
// We only expect one .zip file
|
|
|
|
|
throw new ArgumentException("Only one file is accepted.", nameof(formFiles));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var file = this.Request.Form.Files.First();
|
|
|
|
|
if (file.Length > MaximumSizeInBytes)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("File too large.", nameof(formFiles));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string fileName = Path.GetFileNameWithoutExtension(file.FileName);
|
|
|
|
|
if (!Guid.TryParse(fileName, out Guid _) || file.Name != "crashrpt")
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Unexpected file name.", nameof(formFiles));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetRequestIpAddress()
|
|
|
|
|
{
|
|
|
|
|
return this.httpContextAccessor.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|