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

195 lines
8.0 KiB
C#
Raw Normal View History

2017-06-03 01:10:46 +01:00
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : UploadReportController.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
2017-06-03 01:10:46 +01:00
//
// Component : DiscImageChef Server.
2017-06-03 01:10:46 +01:00
//
// --[ Description ] ----------------------------------------------------------
//
// Handles report uploads.
2017-06-03 01:10:46 +01:00
//
// --[ 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
2017-06-03 01:10:46 +01:00
// 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.
2017-06-03 01:10:46 +01:00
//
// 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/>.
2017-06-03 01:10:46 +01:00
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2018 Natalia Portillo
2017-06-03 01:10:46 +01:00
// ****************************************************************************/
using System;
using System.Diagnostics;
using System.IO;
2017-12-21 14:30:38 +00:00
using System.Net;
using System.Net.Http;
2017-12-21 14:30:38 +00:00
using System.Text;
using System.Web;
using System.Web.Hosting;
2017-12-19 19:33:46 +00:00
using System.Web.Http;
using System.Xml.Serialization;
using Cinchoo.PGP;
using DiscImageChef.CommonTypes.Metadata;
using DiscImageChef.Server.Models;
using MailKit.Net.Smtp;
using MimeKit;
2018-12-20 00:01:53 +00:00
using Newtonsoft.Json;
2017-06-03 01:10:46 +01:00
namespace DiscImageChef.Server.Controllers
{
public class UploadReportController : ApiController
2017-06-03 01:10:46 +01:00
{
DicServerContext ctx = new DicServerContext();
/// <summary>
/// Receives a report from DiscImageChef.Core, verifies it's in the correct format and stores it on the server
/// </summary>
/// <returns>HTTP response</returns>
[Route("api/uploadreport")]
[HttpPost]
public HttpResponseMessage UploadReport()
2017-06-03 01:10:46 +01:00
{
HttpResponseMessage response = new HttpResponseMessage {StatusCode = HttpStatusCode.OK};
try
{
DeviceReport newReport = new DeviceReport();
2018-06-22 08:08:38 +01:00
HttpRequest request = HttpContext.Current.Request;
XmlSerializer xs = new XmlSerializer(newReport.GetType());
newReport = (DeviceReport)xs.Deserialize(request.InputStream);
if(newReport == null)
{
2017-12-21 14:30:38 +00:00
response.Content = new StringContent("notstats", Encoding.UTF8, "text/plain");
return response;
}
DeviceReportV2 reportV2 = new DeviceReportV2(newReport);
StringWriter jsonSw = new StringWriter();
jsonSw.Write(JsonConvert.SerializeObject(reportV2, Formatting.Indented,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
}));
string reportV2String = jsonSw.ToString();
jsonSw.Close();
ctx.Reports.Add(new UploadedReport(reportV2));
ctx.SaveChanges();
MemoryStream pgpIn = new MemoryStream(Encoding.UTF8.GetBytes(reportV2String));
MemoryStream pgpOut = new MemoryStream();
ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt();
pgp.Encrypt(pgpIn, pgpOut,
Path.Combine(HostingEnvironment.MapPath("~") ?? throw new InvalidOperationException(),
"public.asc"), true);
pgpOut.Position = 0;
reportV2String = Encoding.UTF8.GetString(pgpOut.ToArray());
MimeMessage message = new MimeMessage
{
Subject = "New device report (old version)",
Body = new TextPart("plain") {Text = reportV2String}
};
message.From.Add(new MailboxAddress("DiscImageChef", "dic@claunia.com"));
message.To.Add(new MailboxAddress("Natalia Portillo", "claunia@claunia.com"));
using(SmtpClient client = new SmtpClient())
{
client.Connect("mail.claunia.com", 25, false);
client.Send(message);
client.Disconnect(true);
}
2017-12-21 14:30:38 +00:00
response.Content = new StringContent("ok", Encoding.UTF8, "text/plain");
return response;
}
// ReSharper disable once RedundantCatchClause
catch
{
2018-06-22 08:08:38 +01:00
#if DEBUG
if(Debugger.IsAttached) throw;
#endif
response.Content = new StringContent("error", Encoding.UTF8, "text/plain");
return response;
2018-12-20 00:01:53 +00:00
}
}
2018-12-20 00:01:53 +00:00
/// <summary>
/// Receives a report from DiscImageChef.Core, verifies it's in the correct format and stores it on the server
/// </summary>
/// <returns>HTTP response</returns>
[Route("api/uploadreportv2")]
[HttpPost]
public HttpResponseMessage UploadReportV2()
{
HttpResponseMessage response = new HttpResponseMessage {StatusCode = HttpStatusCode.OK};
try
{
HttpRequest request = HttpContext.Current.Request;
2018-12-20 00:01:53 +00:00
StreamReader sr = new StreamReader(request.InputStream);
string reportJson = sr.ReadToEnd();
DeviceReportV2 newReport = JsonConvert.DeserializeObject<DeviceReportV2>(reportJson);
2018-12-20 00:01:53 +00:00
if(newReport == null)
{
response.Content = new StringContent("notstats", Encoding.UTF8, "text/plain");
return response;
}
ctx.Reports.Add(new UploadedReport(newReport));
ctx.SaveChanges();
2018-12-20 00:01:53 +00:00
MemoryStream pgpIn = new MemoryStream(Encoding.UTF8.GetBytes(reportJson));
MemoryStream pgpOut = new MemoryStream();
ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt();
pgp.Encrypt(pgpIn, pgpOut,
Path.Combine(HostingEnvironment.MapPath("~") ?? throw new InvalidOperationException(),
"public.asc"), true);
pgpOut.Position = 0;
reportJson = Encoding.UTF8.GetString(pgpOut.ToArray());
MimeMessage message = new MimeMessage
{
Subject = "New device report", Body = new TextPart("plain") {Text = reportJson}
};
message.From.Add(new MailboxAddress("DiscImageChef", "dic@claunia.com"));
message.To.Add(new MailboxAddress("Natalia Portillo", "claunia@claunia.com"));
using(SmtpClient client = new SmtpClient())
{
client.Connect("mail.claunia.com", 25, false);
client.Send(message);
client.Disconnect(true);
}
2018-12-20 00:01:53 +00:00
response.Content = new StringContent("ok", Encoding.UTF8, "text/plain");
return response;
}
// ReSharper disable once RedundantCatchClause
catch
{
#if DEBUG
if(Debugger.IsAttached) throw;
#endif
response.Content = new StringContent("error", Encoding.UTF8, "text/plain");
2018-12-20 00:01:53 +00:00
return response;
}
2017-06-03 01:10:46 +01:00
}
}
2017-12-19 20:33:03 +00:00
}