mirror of
https://github.com/aaru-dps/Aaru.Server.git
synced 2025-12-16 19:24:27 +00:00
Fix reformatting.
This commit is contained in:
@@ -41,6 +41,7 @@ using System.Xml.Serialization;
|
||||
using DiscImageChef.CommonTypes.Metadata;
|
||||
using DiscImageChef.Server.Models;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Newtonsoft.Json;
|
||||
using OperatingSystem = DiscImageChef.Server.Models.OperatingSystem;
|
||||
@@ -50,176 +51,216 @@ namespace DiscImageChef.Server.Controllers
|
||||
{
|
||||
public class UploadStatsController : Controller
|
||||
{
|
||||
private readonly DicServerContext _ctx;
|
||||
private IWebHostEnvironment _environment;
|
||||
readonly DicServerContext _ctx;
|
||||
IWebHostEnvironment _environment;
|
||||
|
||||
public UploadStatsController(IWebHostEnvironment environment, DicServerContext ctx)
|
||||
{
|
||||
_environment = environment;
|
||||
_ctx = ctx;
|
||||
_ctx = ctx;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Receives statistics from DiscImageChef.Core, processes them and adds them to a server-side global statistics XML
|
||||
/// Receives statistics from DiscImageChef.Core, processes them and adds them to a server-side global statistics
|
||||
/// XML
|
||||
/// </summary>
|
||||
/// <returns>HTTP response</returns>
|
||||
[Route("api/uploadstats")]
|
||||
[HttpPost]
|
||||
[Route("api/uploadstats"), HttpPost]
|
||||
public async Task<IActionResult> UploadStats()
|
||||
{
|
||||
var response = new ContentResult {StatusCode = (int) HttpStatusCode.OK, ContentType = "text/plain"};
|
||||
var response = new ContentResult
|
||||
{
|
||||
StatusCode = (int)HttpStatusCode.OK, ContentType = "text/plain"
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var newStats = new Stats();
|
||||
var request = HttpContext.Request;
|
||||
var newStats = new Stats();
|
||||
HttpRequest request = HttpContext.Request;
|
||||
|
||||
var xs = new XmlSerializer(newStats.GetType());
|
||||
newStats = (Stats) xs.Deserialize(
|
||||
new StringReader(await new StreamReader(request.Body).ReadToEndAsync()));
|
||||
|
||||
if (newStats == null)
|
||||
newStats =
|
||||
(Stats)xs.Deserialize(new StringReader(await new StreamReader(request.Body).ReadToEndAsync()));
|
||||
|
||||
if(newStats == null)
|
||||
{
|
||||
response.Content = "notstats";
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
StatsConverter.Convert(newStats);
|
||||
|
||||
response.Content = "ok";
|
||||
|
||||
return response;
|
||||
}
|
||||
catch (Exception ex)
|
||||
catch(Exception ex)
|
||||
{
|
||||
#if DEBUG
|
||||
if (Debugger.IsAttached) throw;
|
||||
#endif
|
||||
#if DEBUG
|
||||
if(Debugger.IsAttached)
|
||||
throw;
|
||||
#endif
|
||||
response.Content = "error";
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Receives a report from DiscImageChef.Core, verifies it's in the correct format and stores it on the server
|
||||
/// </summary>
|
||||
/// <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/uploadstatsv2")]
|
||||
[HttpPost]
|
||||
[Route("api/uploadstatsv2"), HttpPost]
|
||||
public async Task<IActionResult> UploadStatsV2()
|
||||
{
|
||||
var response = new ContentResult {StatusCode = (int) HttpStatusCode.OK, ContentType = "text/plain"};
|
||||
var response = new ContentResult
|
||||
{
|
||||
StatusCode = (int)HttpStatusCode.OK, ContentType = "text/plain"
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var request = HttpContext.Request;
|
||||
HttpRequest request = HttpContext.Request;
|
||||
|
||||
var sr = new StreamReader(request.Body);
|
||||
var statsString = await sr.ReadToEndAsync();
|
||||
var newstats = JsonConvert.DeserializeObject<StatsDto>(statsString);
|
||||
var sr = new StreamReader(request.Body);
|
||||
string statsString = await sr.ReadToEndAsync();
|
||||
var newstats = JsonConvert.DeserializeObject<StatsDto>(statsString);
|
||||
|
||||
if (newstats == null)
|
||||
if(newstats == null)
|
||||
{
|
||||
response.Content = "notstats";
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
if (newstats.Commands != null)
|
||||
foreach (var nvs in newstats.Commands)
|
||||
if(newstats.Commands != null)
|
||||
foreach(NameValueStats nvs in newstats.Commands)
|
||||
{
|
||||
var existing = _ctx.Commands.FirstOrDefault(c => c.Name == nvs.name);
|
||||
Command existing = _ctx.Commands.FirstOrDefault(c => c.Name == nvs.name);
|
||||
|
||||
if (existing == null) _ctx.Commands.Add(new Command {Name = nvs.name, Count = nvs.Value});
|
||||
else existing.Count += nvs.Value;
|
||||
if(existing == null)
|
||||
_ctx.Commands.Add(new Command
|
||||
{
|
||||
Name = nvs.name, Count = nvs.Value
|
||||
});
|
||||
else
|
||||
existing.Count += nvs.Value;
|
||||
}
|
||||
|
||||
if (newstats.Versions != null)
|
||||
foreach (var nvs in newstats.Versions)
|
||||
if(newstats.Versions != null)
|
||||
foreach(NameValueStats nvs in newstats.Versions)
|
||||
{
|
||||
var existing = _ctx.Versions.FirstOrDefault(c => c.Value == nvs.name);
|
||||
Version existing = _ctx.Versions.FirstOrDefault(c => c.Value == nvs.name);
|
||||
|
||||
if (existing == null) _ctx.Versions.Add(new Version {Value = nvs.name, Count = nvs.Value});
|
||||
else existing.Count += nvs.Value;
|
||||
if(existing == null)
|
||||
_ctx.Versions.Add(new Version
|
||||
{
|
||||
Value = nvs.name, Count = nvs.Value
|
||||
});
|
||||
else
|
||||
existing.Count += nvs.Value;
|
||||
}
|
||||
|
||||
if (newstats.Filesystems != null)
|
||||
foreach (var nvs in newstats.Filesystems)
|
||||
if(newstats.Filesystems != null)
|
||||
foreach(NameValueStats nvs in newstats.Filesystems)
|
||||
{
|
||||
var existing = _ctx.Filesystems.FirstOrDefault(c => c.Name == nvs.name);
|
||||
Filesystem existing = _ctx.Filesystems.FirstOrDefault(c => c.Name == nvs.name);
|
||||
|
||||
if (existing == null) _ctx.Filesystems.Add(new Filesystem {Name = nvs.name, Count = nvs.Value});
|
||||
else existing.Count += nvs.Value;
|
||||
if(existing == null)
|
||||
_ctx.Filesystems.Add(new Filesystem
|
||||
{
|
||||
Name = nvs.name, Count = nvs.Value
|
||||
});
|
||||
else
|
||||
existing.Count += nvs.Value;
|
||||
}
|
||||
|
||||
if (newstats.Partitions != null)
|
||||
foreach (var nvs in newstats.Partitions)
|
||||
if(newstats.Partitions != null)
|
||||
foreach(NameValueStats nvs in newstats.Partitions)
|
||||
{
|
||||
var existing = _ctx.Partitions.FirstOrDefault(c => c.Name == nvs.name);
|
||||
Partition existing = _ctx.Partitions.FirstOrDefault(c => c.Name == nvs.name);
|
||||
|
||||
if (existing == null) _ctx.Partitions.Add(new Partition {Name = nvs.name, Count = nvs.Value});
|
||||
else existing.Count += nvs.Value;
|
||||
if(existing == null)
|
||||
_ctx.Partitions.Add(new Partition
|
||||
{
|
||||
Name = nvs.name, Count = nvs.Value
|
||||
});
|
||||
else
|
||||
existing.Count += nvs.Value;
|
||||
}
|
||||
|
||||
if (newstats.MediaFormats != null)
|
||||
foreach (var nvs in newstats.MediaFormats)
|
||||
if(newstats.MediaFormats != null)
|
||||
foreach(NameValueStats nvs in newstats.MediaFormats)
|
||||
{
|
||||
var existing = _ctx.MediaFormats.FirstOrDefault(c => c.Name == nvs.name);
|
||||
MediaFormat existing = _ctx.MediaFormats.FirstOrDefault(c => c.Name == nvs.name);
|
||||
|
||||
if (existing == null)
|
||||
_ctx.MediaFormats.Add(new MediaFormat {Name = nvs.name, Count = nvs.Value});
|
||||
else existing.Count += nvs.Value;
|
||||
if(existing == null)
|
||||
_ctx.MediaFormats.Add(new MediaFormat
|
||||
{
|
||||
Name = nvs.name, Count = nvs.Value
|
||||
});
|
||||
else
|
||||
existing.Count += nvs.Value;
|
||||
}
|
||||
|
||||
if (newstats.Filters != null)
|
||||
foreach (var nvs in newstats.Filters)
|
||||
if(newstats.Filters != null)
|
||||
foreach(NameValueStats nvs in newstats.Filters)
|
||||
{
|
||||
var existing = _ctx.Filters.FirstOrDefault(c => c.Name == nvs.name);
|
||||
Filter existing = _ctx.Filters.FirstOrDefault(c => c.Name == nvs.name);
|
||||
|
||||
if (existing == null) _ctx.Filters.Add(new Filter {Name = nvs.name, Count = nvs.Value});
|
||||
else existing.Count += nvs.Value;
|
||||
if(existing == null)
|
||||
_ctx.Filters.Add(new Filter
|
||||
{
|
||||
Name = nvs.name, Count = nvs.Value
|
||||
});
|
||||
else
|
||||
existing.Count += nvs.Value;
|
||||
}
|
||||
|
||||
if (newstats.OperatingSystems != null)
|
||||
foreach (var operatingSystem in newstats.OperatingSystems)
|
||||
if(newstats.OperatingSystems != null)
|
||||
foreach(OsStats operatingSystem in newstats.OperatingSystems)
|
||||
{
|
||||
var existing =
|
||||
_ctx.OperatingSystems.FirstOrDefault(c => c.Name == operatingSystem.name &&
|
||||
OperatingSystem existing =
|
||||
_ctx.OperatingSystems.FirstOrDefault(c => c.Name == operatingSystem.name &&
|
||||
c.Version == operatingSystem.version);
|
||||
|
||||
if (existing == null)
|
||||
if(existing == null)
|
||||
_ctx.OperatingSystems.Add(new OperatingSystem
|
||||
{
|
||||
Name = operatingSystem.name,
|
||||
Version = operatingSystem.version,
|
||||
Name = operatingSystem.name, Version = operatingSystem.version,
|
||||
Count = operatingSystem.Value
|
||||
});
|
||||
else existing.Count += operatingSystem.Value;
|
||||
else
|
||||
existing.Count += operatingSystem.Value;
|
||||
}
|
||||
|
||||
if (newstats.Medias != null)
|
||||
foreach (var media in newstats.Medias)
|
||||
if(newstats.Medias != null)
|
||||
foreach(MediaStats media in newstats.Medias)
|
||||
{
|
||||
var existing = _ctx.Medias.FirstOrDefault(c => c.Type == media.type && c.Real == media.real);
|
||||
Media existing = _ctx.Medias.FirstOrDefault(c => c.Type == media.type && c.Real == media.real);
|
||||
|
||||
if (existing == null)
|
||||
_ctx.Medias.Add(new Media {Type = media.type, Real = media.real, Count = media.Value});
|
||||
else existing.Count += media.Value;
|
||||
if(existing == null)
|
||||
_ctx.Medias.Add(new Media
|
||||
{
|
||||
Type = media.type, Real = media.real, Count = media.Value
|
||||
});
|
||||
else
|
||||
existing.Count += media.Value;
|
||||
}
|
||||
|
||||
if (newstats.Devices != null)
|
||||
foreach (var device in newstats.Devices)
|
||||
if(newstats.Devices != null)
|
||||
foreach(DeviceStats device in newstats.Devices)
|
||||
{
|
||||
var existing =
|
||||
_ctx.DeviceStats.FirstOrDefault(c => c.Bus == device.Bus &&
|
||||
DeviceStat existing =
|
||||
_ctx.DeviceStats.FirstOrDefault(c => c.Bus == device.Bus &&
|
||||
c.Manufacturer == device.Manufacturer &&
|
||||
c.Model == device.Model &&
|
||||
c.Revision == device.Revision);
|
||||
c.Model == device.Model &&
|
||||
c.Revision == device.Revision);
|
||||
|
||||
if (existing == null)
|
||||
if(existing == null)
|
||||
_ctx.DeviceStats.Add(new DeviceStat
|
||||
{
|
||||
Bus = device.Bus,
|
||||
Manufacturer = device.Manufacturer,
|
||||
Model = device.Model,
|
||||
Bus = device.Bus, Manufacturer = device.Manufacturer, Model = device.Model,
|
||||
Revision = device.Revision
|
||||
});
|
||||
}
|
||||
@@ -227,32 +268,40 @@ namespace DiscImageChef.Server.Controllers
|
||||
_ctx.SaveChanges();
|
||||
|
||||
response.Content = "ok";
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
// ReSharper disable once RedundantCatchClause
|
||||
catch
|
||||
{
|
||||
#if DEBUG
|
||||
if (Debugger.IsAttached) throw;
|
||||
#endif
|
||||
#if DEBUG
|
||||
if(Debugger.IsAttached)
|
||||
throw;
|
||||
#endif
|
||||
response.Content = "error";
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
private FileStream WaitForFile(string fullPath, FileMode mode, FileAccess access, FileShare share)
|
||||
FileStream WaitForFile(string fullPath, FileMode mode, FileAccess access, FileShare share)
|
||||
{
|
||||
for (var numTries = 0; numTries < 100; numTries++)
|
||||
for(int numTries = 0; numTries < 100; numTries++)
|
||||
{
|
||||
FileStream fs = null;
|
||||
|
||||
try
|
||||
{
|
||||
fs = new FileStream(fullPath, mode, access, share);
|
||||
|
||||
return fs;
|
||||
}
|
||||
catch (IOException)
|
||||
catch(IOException)
|
||||
{
|
||||
if (fs != null) fs.Dispose();
|
||||
if(fs != null)
|
||||
fs.Dispose();
|
||||
|
||||
Thread.Sleep(50);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user