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

312 lines
11 KiB
C#
Raw Normal View History

2019-11-02 01:40:41 +00:00
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : UploadStatsController.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : DiscImageChef Server.
//
// --[ Description ] ----------------------------------------------------------
//
// Handles statistics 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;
using System.Net;
using System.Threading;
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 DiscImageChef.CommonTypes.Metadata;
using DiscImageChef.Server.Models;
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 Newtonsoft.Json;
using OperatingSystem = DiscImageChef.Server.Models.OperatingSystem;
using Version = DiscImageChef.Server.Models.Version;
namespace DiscImageChef.Server.Controllers
{
2019-11-02 21:01:25 +00:00
public class UploadStatsController : Controller
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
readonly DicServerContext _ctx;
IWebHostEnvironment _environment;
2019-11-02 21:01:25 +00:00
public UploadStatsController(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 21:37:09 +00:00
2019-11-02 01:40:41 +00:00
/// <summary>
2019-11-02 23:52:33 +00:00
/// Receives statistics from DiscImageChef.Core, processes them and adds them to a server-side global statistics
/// XML
2019-11-02 01:40:41 +00:00
/// </summary>
/// <returns>HTTP response</returns>
2019-11-02 23:52:33 +00:00
[Route("api/uploadstats"), HttpPost]
2019-11-02 21:01:25 +00:00
public async Task<IActionResult> UploadStats()
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 newStats = new Stats();
HttpRequest request = HttpContext.Request;
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
var xs = new XmlSerializer(newStats.GetType());
2019-11-02 01:40:41 +00:00
2019-11-02 23:52:33 +00:00
newStats =
(Stats)xs.Deserialize(new StringReader(await new StreamReader(request.Body).ReadToEndAsync()));
if(newStats == 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;
}
StatsConverter.Convert(newStats);
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
catch(Exception ex)
2019-11-02 01:40:41 +00:00
{
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/uploadstatsv2"), HttpPost]
2019-11-02 21:01:25 +00:00
public async Task<IActionResult> UploadStatsV2()
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 statsString = await sr.ReadToEndAsync();
var newstats = JsonConvert.DeserializeObject<StatsDto>(statsString);
2019-11-02 01:40:41 +00:00
2019-11-02 23:52:33 +00:00
if(newstats == 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 23:52:33 +00:00
if(newstats.Commands != null)
foreach(NameValueStats nvs in newstats.Commands)
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
Command existing = _ctx.Commands.FirstOrDefault(c => c.Name == nvs.name);
2019-11-02 01:40:41 +00:00
2019-11-02 23:52:33 +00:00
if(existing == null)
_ctx.Commands.Add(new Command
{
Name = nvs.name, Count = nvs.Value
});
else
existing.Count += nvs.Value;
2019-11-02 01:40:41 +00:00
}
2019-11-02 23:52:33 +00:00
if(newstats.Versions != null)
foreach(NameValueStats nvs in newstats.Versions)
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
Version existing = _ctx.Versions.FirstOrDefault(c => c.Value == nvs.name);
2019-11-02 01:40:41 +00:00
2019-11-02 23:52:33 +00:00
if(existing == null)
_ctx.Versions.Add(new Version
{
Value = nvs.name, Count = nvs.Value
});
else
existing.Count += nvs.Value;
2019-11-02 01:40:41 +00:00
}
2019-11-02 23:52:33 +00:00
if(newstats.Filesystems != null)
foreach(NameValueStats nvs in newstats.Filesystems)
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
Filesystem existing = _ctx.Filesystems.FirstOrDefault(c => c.Name == nvs.name);
2019-11-02 01:40:41 +00:00
2019-11-02 23:52:33 +00:00
if(existing == null)
_ctx.Filesystems.Add(new Filesystem
{
Name = nvs.name, Count = nvs.Value
});
else
existing.Count += nvs.Value;
2019-11-02 01:40:41 +00:00
}
2019-11-02 23:52:33 +00:00
if(newstats.Partitions != null)
foreach(NameValueStats nvs in newstats.Partitions)
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
Partition existing = _ctx.Partitions.FirstOrDefault(c => c.Name == nvs.name);
2019-11-02 01:40:41 +00:00
2019-11-02 23:52:33 +00:00
if(existing == null)
_ctx.Partitions.Add(new Partition
{
Name = nvs.name, Count = nvs.Value
});
else
existing.Count += nvs.Value;
2019-11-02 01:40:41 +00:00
}
2019-11-02 23:52:33 +00:00
if(newstats.MediaFormats != null)
foreach(NameValueStats nvs in newstats.MediaFormats)
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
MediaFormat existing = _ctx.MediaFormats.FirstOrDefault(c => c.Name == nvs.name);
2019-11-02 01:40:41 +00:00
2019-11-02 23:52:33 +00:00
if(existing == null)
_ctx.MediaFormats.Add(new MediaFormat
{
Name = nvs.name, Count = nvs.Value
});
else
existing.Count += nvs.Value;
2019-11-02 01:40:41 +00:00
}
2019-11-02 23:52:33 +00:00
if(newstats.Filters != null)
foreach(NameValueStats nvs in newstats.Filters)
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
Filter existing = _ctx.Filters.FirstOrDefault(c => c.Name == nvs.name);
2019-11-02 01:40:41 +00:00
2019-11-02 23:52:33 +00:00
if(existing == null)
_ctx.Filters.Add(new Filter
{
Name = nvs.name, Count = nvs.Value
});
else
existing.Count += nvs.Value;
2019-11-02 01:40:41 +00:00
}
2019-11-02 23:52:33 +00:00
if(newstats.OperatingSystems != null)
foreach(OsStats operatingSystem in newstats.OperatingSystems)
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
OperatingSystem existing =
_ctx.OperatingSystems.FirstOrDefault(c => c.Name == operatingSystem.name &&
2019-11-02 21:37:09 +00:00
c.Version == operatingSystem.version);
2019-11-02 01:40:41 +00:00
2019-11-02 23:52:33 +00:00
if(existing == null)
2019-11-02 21:01:25 +00:00
_ctx.OperatingSystems.Add(new OperatingSystem
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
Name = operatingSystem.name, Version = operatingSystem.version,
2019-11-02 21:37:09 +00:00
Count = operatingSystem.Value
2019-11-02 01:40:41 +00:00
});
2019-11-02 23:52:33 +00:00
else
existing.Count += operatingSystem.Value;
2019-11-02 01:40:41 +00:00
}
2019-11-02 23:52:33 +00:00
if(newstats.Medias != null)
foreach(MediaStats media in newstats.Medias)
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
Media existing = _ctx.Medias.FirstOrDefault(c => c.Type == media.type && c.Real == media.real);
2019-11-02 01:40:41 +00:00
2019-11-02 23:52:33 +00:00
if(existing == null)
_ctx.Medias.Add(new Media
{
Type = media.type, Real = media.real, Count = media.Value
});
else
existing.Count += media.Value;
2019-11-02 01:40:41 +00:00
}
2019-11-02 23:52:33 +00:00
if(newstats.Devices != null)
foreach(DeviceStats device in newstats.Devices)
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
DeviceStat existing =
_ctx.DeviceStats.FirstOrDefault(c => c.Bus == device.Bus &&
2019-11-02 21:37:09 +00:00
c.Manufacturer == device.Manufacturer &&
2019-11-02 23:52:33 +00:00
c.Model == device.Model &&
c.Revision == device.Revision);
2019-11-02 01:40:41 +00:00
2019-11-02 23:52:33 +00:00
if(existing == null)
2019-11-02 21:01:25 +00:00
_ctx.DeviceStats.Add(new DeviceStat
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
Bus = device.Bus, Manufacturer = device.Manufacturer, Model = device.Model,
2019-11-02 21:37:09 +00:00
Revision = device.Revision
2019-11-02 01:40:41 +00:00
});
}
2019-11-02 21:01:25 +00:00
_ctx.SaveChanges();
2019-11-02 01:40:41 +00:00
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
FileStream WaitForFile(string fullPath, FileMode mode, FileAccess access, FileShare share)
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
for(int numTries = 0; numTries < 100; numTries++)
2019-11-02 01:40:41 +00:00
{
FileStream fs = null;
2019-11-02 23:52:33 +00:00
2019-11-02 01:40:41 +00:00
try
{
fs = new FileStream(fullPath, mode, access, share);
2019-11-02 23:52:33 +00:00
2019-11-02 01:40:41 +00:00
return fs;
}
2019-11-02 23:52:33 +00:00
catch(IOException)
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
if(fs != null)
fs.Dispose();
2019-11-02 01:40:41 +00:00
Thread.Sleep(50);
}
}
return null;
}
}
}