Files
Aaru.Server/DiscImageChef.Server/Controllers/UploadReportController.cs

396 lines
17 KiB
C#
Raw Normal View History

2019-11-02 01:40:41 +00:00
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : UploadReportController.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : DiscImageChef Server.
//
// --[ Description ] ----------------------------------------------------------
//
// Handles report uploads.
//
// --[ License ] --------------------------------------------------------------
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2019 Natalia Portillo
// ****************************************************************************/
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
2019-11-02 01:40:41 +00:00
using System.Net;
using System.Text;
2019-11-02 21:01:25 +00:00
using System.Threading.Tasks;
2019-11-02 01:40:41 +00:00
using System.Xml.Serialization;
using Cinchoo.PGP;
using DiscImageChef.CommonTypes.Metadata;
using DiscImageChef.Server.Models;
using MailKit.Net.Smtp;
2019-11-02 21:01:25 +00:00
using Microsoft.AspNetCore.Hosting;
2019-11-02 23:52:33 +00:00
using Microsoft.AspNetCore.Http;
2019-11-02 21:01:25 +00:00
using Microsoft.AspNetCore.Mvc;
2019-11-02 01:40:41 +00:00
using MimeKit;
using Newtonsoft.Json;
namespace DiscImageChef.Server.Controllers
{
2019-11-02 21:01:25 +00:00
public class UploadReportController : Controller
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
readonly IWebHostEnvironment _environment;
readonly DicServerContext ctx;
2019-11-02 21:01:25 +00:00
public UploadReportController(IWebHostEnvironment environment, DicServerContext _ctx)
{
_environment = environment;
2019-11-02 23:52:33 +00:00
ctx = _ctx;
2019-11-02 21:01:25 +00:00
}
2019-11-02 01:40:41 +00:00
2019-11-02 23:52:33 +00:00
/// <summary>Receives a report from DiscImageChef.Core, verifies it's in the correct format and stores it on the server</summary>
2019-11-02 01:40:41 +00:00
/// <returns>HTTP response</returns>
2019-11-02 23:52:33 +00:00
[Route("api/uploadreport"), HttpPost]
2019-11-02 21:01:25 +00:00
public async Task<IActionResult> UploadReport()
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
var response = new ContentResult
{
StatusCode = (int)HttpStatusCode.OK, ContentType = "text/plain"
};
2019-11-02 01:40:41 +00:00
try
{
2019-11-02 23:52:33 +00:00
var newReport = new DeviceReport();
HttpRequest request = HttpContext.Request;
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
var xs = new XmlSerializer(newReport.GetType());
2019-11-02 01:40:41 +00:00
2019-11-02 23:52:33 +00:00
newReport =
(DeviceReport)
xs.Deserialize(new StringReader(await new StreamReader(request.Body).ReadToEndAsync()));
if(newReport == null)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:01:25 +00:00
response.Content = "notstats";
2019-11-02 23:52:33 +00:00
2019-11-02 01:40:41 +00:00
return response;
}
2019-11-02 21:37:09 +00:00
var reportV2 = new DeviceReportV2(newReport);
2019-11-02 23:52:33 +00:00
var jsonSw = new StringWriter();
jsonSw.Write(JsonConvert.SerializeObject(reportV2, Formatting.Indented, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
}));
string reportV2String = jsonSw.ToString();
2019-11-02 01:40:41 +00:00
jsonSw.Close();
var newUploadedReport = new UploadedReport(reportV2);
// Ensure CHS and CurrentCHS are not duplicates
if(newUploadedReport.ATA?.ReadCapabilities?.CHS != null &&
newUploadedReport.ATA?.ReadCapabilities?.CurrentCHS != null)
{
if(newUploadedReport.ATA.ReadCapabilities.CHS.Cylinders ==
newUploadedReport.ATA.ReadCapabilities.CurrentCHS.Cylinders &&
newUploadedReport.ATA.ReadCapabilities.CHS.Heads ==
newUploadedReport.ATA.ReadCapabilities.CurrentCHS.Heads &&
newUploadedReport.ATA.ReadCapabilities.CHS.Sectors ==
newUploadedReport.ATA.ReadCapabilities.CurrentCHS.Sectors)
{
newUploadedReport.ATA.ReadCapabilities.CHS = newUploadedReport.ATA.ReadCapabilities.CurrentCHS;
}
}
// Check if the CHS or CurrentCHS of this report already exist in the database
if(newUploadedReport.ATA?.ReadCapabilities?.CHS != null)
{
Chs existingChs =
ctx.Chs.FirstOrDefault(c =>
c.Cylinders == newUploadedReport.
ATA.ReadCapabilities.CHS.Cylinders &&
c.Heads == newUploadedReport.ATA.ReadCapabilities.CHS.Heads &&
c.Sectors == newUploadedReport.ATA.ReadCapabilities.CHS.Sectors);
if(existingChs != null)
newUploadedReport.ATA.ReadCapabilities.CHS = existingChs;
}
if(newUploadedReport.ATA?.ReadCapabilities?.CurrentCHS != null)
{
Chs existingChs =
ctx.Chs.FirstOrDefault(c =>
c.Cylinders == newUploadedReport.
ATA.ReadCapabilities.CurrentCHS.Cylinders &&
c.Heads == newUploadedReport.ATA.ReadCapabilities.CurrentCHS.Heads &&
c.Sectors == newUploadedReport.
ATA.ReadCapabilities.CurrentCHS.Sectors);
if(existingChs != null)
newUploadedReport.ATA.ReadCapabilities.CurrentCHS = existingChs;
}
if(newUploadedReport.ATA?.RemovableMedias != null)
{
foreach(TestedMedia media in newUploadedReport.ATA.RemovableMedias)
{
if(media.CHS != null &&
media.CurrentCHS != null)
{
if(media.CHS.Cylinders == media.CurrentCHS.Cylinders &&
media.CHS.Heads == media.CurrentCHS.Heads &&
media.CHS.Sectors == media.CurrentCHS.Sectors)
{
media.CHS = media.CurrentCHS;
}
}
if(media.CHS != null)
{
Chs existingChs =
ctx.Chs.FirstOrDefault(c => c.Cylinders == media.CHS.Cylinders &&
c.Heads == media.CHS.Heads &&
c.Sectors == media.CHS.Sectors);
if(existingChs != null)
media.CHS = existingChs;
}
if(media.CHS != null)
{
Chs existingChs =
ctx.Chs.FirstOrDefault(c => c.Cylinders == media.CurrentCHS.Cylinders &&
c.Heads == media.CurrentCHS.Heads &&
c.Sectors == media.CurrentCHS.Sectors);
if(existingChs != null)
media.CurrentCHS = existingChs;
}
}
}
ctx.Reports.Add(newUploadedReport);
2019-11-02 01:40:41 +00:00
ctx.SaveChanges();
2019-11-02 23:52:33 +00:00
var pgpIn = new MemoryStream(Encoding.UTF8.GetBytes(reportV2String));
2019-11-02 21:37:09 +00:00
var pgpOut = new MemoryStream();
2019-11-02 23:52:33 +00:00
var pgp = new ChoPGPEncryptDecrypt();
2019-11-02 01:40:41 +00:00
pgp.Encrypt(pgpIn, pgpOut,
2019-11-02 23:52:33 +00:00
Path.Combine(_environment.ContentRootPath ?? throw new InvalidOperationException(),
"public.asc"));
2019-11-02 01:40:41 +00:00
pgpOut.Position = 0;
2019-11-02 23:52:33 +00:00
reportV2String = Encoding.UTF8.GetString(pgpOut.ToArray());
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
var message = new MimeMessage
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
Subject = "New device report (old version)", Body = new TextPart("plain")
{
Text = reportV2String
}
2019-11-02 01:40:41 +00:00
};
2019-11-02 23:52:33 +00:00
2019-11-02 21:37:09 +00:00
message.From.Add(new MailboxAddress("DiscImageChef", "dic@claunia.com"));
2019-11-02 01:40:41 +00:00
message.To.Add(new MailboxAddress("Natalia Portillo", "claunia@claunia.com"));
2019-11-02 23:52:33 +00:00
using(var client = new SmtpClient())
2019-11-02 01:40:41 +00:00
{
client.Connect("mail.claunia.com", 25, false);
client.Send(message);
client.Disconnect(true);
}
2019-11-02 21:01:25 +00:00
response.Content = "ok";
2019-11-02 23:52:33 +00:00
2019-11-02 01:40:41 +00:00
return response;
}
2019-11-02 23:52:33 +00:00
2019-11-02 01:40:41 +00:00
// ReSharper disable once RedundantCatchClause
catch
{
2019-11-02 23:52:33 +00:00
#if DEBUG
if(Debugger.IsAttached)
throw;
#endif
2019-11-02 21:01:25 +00:00
response.Content = "error";
2019-11-02 23:52:33 +00:00
2019-11-02 01:40:41 +00:00
return response;
}
}
2019-11-02 23:52:33 +00:00
/// <summary>Receives a report from DiscImageChef.Core, verifies it's in the correct format and stores it on the server</summary>
2019-11-02 01:40:41 +00:00
/// <returns>HTTP response</returns>
2019-11-02 23:52:33 +00:00
[Route("api/uploadreportv2"), HttpPost]
2019-11-02 21:01:25 +00:00
public async Task<IActionResult> UploadReportV2()
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
var response = new ContentResult
{
StatusCode = (int)HttpStatusCode.OK, ContentType = "text/plain"
};
2019-11-02 01:40:41 +00:00
try
{
2019-11-02 23:52:33 +00:00
HttpRequest request = HttpContext.Request;
2019-11-02 01:40:41 +00:00
2019-11-02 23:52:33 +00:00
var sr = new StreamReader(request.Body);
string reportJson = await sr.ReadToEndAsync();
var newReport = JsonConvert.DeserializeObject<DeviceReportV2>(reportJson);
2019-11-02 01:40:41 +00:00
2019-11-02 23:52:33 +00:00
if(newReport == null)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:01:25 +00:00
response.Content = "notstats";
2019-11-02 23:52:33 +00:00
2019-11-02 01:40:41 +00:00
return response;
}
var newUploadedReport = new UploadedReport(newReport);
// Ensure CHS and CurrentCHS are not duplicates
if(newUploadedReport.ATA?.ReadCapabilities?.CHS != null &&
newUploadedReport.ATA?.ReadCapabilities?.CurrentCHS != null)
{
if(newUploadedReport.ATA.ReadCapabilities.CHS.Cylinders ==
newUploadedReport.ATA.ReadCapabilities.CurrentCHS.Cylinders &&
newUploadedReport.ATA.ReadCapabilities.CHS.Heads ==
newUploadedReport.ATA.ReadCapabilities.CurrentCHS.Heads &&
newUploadedReport.ATA.ReadCapabilities.CHS.Sectors ==
newUploadedReport.ATA.ReadCapabilities.CurrentCHS.Sectors)
{
newUploadedReport.ATA.ReadCapabilities.CHS = newUploadedReport.ATA.ReadCapabilities.CurrentCHS;
}
}
// Check if the CHS or CurrentCHS of this report already exist in the database
if(newUploadedReport.ATA?.ReadCapabilities?.CHS != null)
{
Chs existingChs =
ctx.Chs.FirstOrDefault(c =>
c.Cylinders == newUploadedReport.
ATA.ReadCapabilities.CHS.Cylinders &&
c.Heads == newUploadedReport.ATA.ReadCapabilities.CHS.Heads &&
c.Sectors == newUploadedReport.ATA.ReadCapabilities.CHS.Sectors);
if(existingChs != null)
newUploadedReport.ATA.ReadCapabilities.CHS = existingChs;
}
if(newUploadedReport.ATA?.ReadCapabilities?.CurrentCHS != null)
{
Chs existingChs =
ctx.Chs.FirstOrDefault(c =>
c.Cylinders == newUploadedReport.
ATA.ReadCapabilities.CurrentCHS.Cylinders &&
c.Heads == newUploadedReport.ATA.ReadCapabilities.CurrentCHS.Heads &&
c.Sectors == newUploadedReport.
ATA.ReadCapabilities.CurrentCHS.Sectors);
if(existingChs != null)
newUploadedReport.ATA.ReadCapabilities.CurrentCHS = existingChs;
}
if(newUploadedReport.ATA?.RemovableMedias != null)
{
foreach(TestedMedia media in newUploadedReport.ATA.RemovableMedias)
{
if(media.CHS != null &&
media.CurrentCHS != null)
{
if(media.CHS.Cylinders == media.CurrentCHS.Cylinders &&
media.CHS.Heads == media.CurrentCHS.Heads &&
media.CHS.Sectors == media.CurrentCHS.Sectors)
{
media.CHS = media.CurrentCHS;
}
}
if(media.CHS != null)
{
Chs existingChs =
ctx.Chs.FirstOrDefault(c => c.Cylinders == media.CHS.Cylinders &&
c.Heads == media.CHS.Heads &&
c.Sectors == media.CHS.Sectors);
if(existingChs != null)
media.CHS = existingChs;
}
if(media.CHS != null)
{
Chs existingChs =
ctx.Chs.FirstOrDefault(c => c.Cylinders == media.CurrentCHS.Cylinders &&
c.Heads == media.CurrentCHS.Heads &&
c.Sectors == media.CurrentCHS.Sectors);
if(existingChs != null)
media.CurrentCHS = existingChs;
}
}
}
ctx.Reports.Add(newUploadedReport);
2019-11-02 01:40:41 +00:00
ctx.SaveChanges();
2019-11-02 23:52:33 +00:00
var pgpIn = new MemoryStream(Encoding.UTF8.GetBytes(reportJson));
2019-11-02 21:37:09 +00:00
var pgpOut = new MemoryStream();
2019-11-02 23:52:33 +00:00
var pgp = new ChoPGPEncryptDecrypt();
2019-11-02 01:40:41 +00:00
pgp.Encrypt(pgpIn, pgpOut,
2019-11-02 23:52:33 +00:00
Path.Combine(_environment.ContentRootPath ?? throw new InvalidOperationException(),
"public.asc"));
2019-11-02 01:40:41 +00:00
pgpOut.Position = 0;
2019-11-02 23:52:33 +00:00
reportJson = Encoding.UTF8.GetString(pgpOut.ToArray());
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
var message = new MimeMessage
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
Subject = "New device report", Body = new TextPart("plain")
{
Text = reportJson
}
2019-11-02 01:40:41 +00:00
};
2019-11-02 23:52:33 +00:00
2019-11-02 21:37:09 +00:00
message.From.Add(new MailboxAddress("DiscImageChef", "dic@claunia.com"));
2019-11-02 01:40:41 +00:00
message.To.Add(new MailboxAddress("Natalia Portillo", "claunia@claunia.com"));
2019-11-02 23:52:33 +00:00
using(var client = new SmtpClient())
2019-11-02 01:40:41 +00:00
{
client.Connect("mail.claunia.com", 25, false);
client.Send(message);
client.Disconnect(true);
}
2019-11-02 21:01:25 +00:00
response.Content = "ok";
2019-11-02 23:52:33 +00:00
2019-11-02 01:40:41 +00:00
return response;
}
2019-11-02 23:52:33 +00:00
2019-11-02 01:40:41 +00:00
// ReSharper disable once RedundantCatchClause
catch
{
2019-11-02 23:52:33 +00:00
#if DEBUG
if(Debugger.IsAttached)
throw;
#endif
2019-11-02 21:37:09 +00:00
response.Content = "error";
2019-11-02 23:52:33 +00:00
2019-11-02 01:40:41 +00:00
return response;
}
}
}
}