Add option to consolidate USB devices.

This commit is contained in:
2019-11-17 23:32:57 +00:00
parent 4302a291f8
commit 6c49a7b407
5 changed files with 162 additions and 1 deletions

View File

@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DiscImageChef.CommonTypes.Metadata;
@@ -5,6 +6,7 @@ using DiscImageChef.Server.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
namespace DiscImageChef.Server.Areas.Admin.Controllers
{
@@ -66,5 +68,87 @@ namespace DiscImageChef.Server.Areas.Admin.Controllers
return RedirectToAction(nameof(Index));
}
public IActionResult Consolidate()
{
List<UsbModel> dups = _context.Usb.GroupBy(x => new
{
x.Manufacturer, x.Product, x.VendorID, x.ProductID
}).Where(x => x.Count() > 1).Select(x => new UsbModel
{
Manufacturer = x.Key.Manufacturer, Product = x.Key.Product, VendorID = x.Key.VendorID,
ProductID = x.Key.ProductID
}).ToList();
return View(new UsbModelForView
{
List = dups, Json = JsonConvert.SerializeObject(dups)
});
}
[HttpPost, ActionName("Consolidate"), ValidateAntiForgeryToken]
public IActionResult ConsolidateConfirmed(string models)
{
UsbModel[] duplicates;
try
{
duplicates = JsonConvert.DeserializeObject<UsbModel[]>(models);
}
catch(JsonSerializationException)
{
return BadRequest();
}
if(duplicates is null)
return BadRequest();
foreach(UsbModel duplicate in duplicates)
{
Usb master = _context.Usb.FirstOrDefault(m => m.Manufacturer == duplicate.Manufacturer &&
m.Product == duplicate.Product &&
m.VendorID == duplicate.VendorID &&
m.ProductID == duplicate.ProductID);
if(master is null)
continue;
foreach(Usb slave in _context.Usb.Where(m => m.Manufacturer == duplicate.Manufacturer &&
m.Product == duplicate.Product &&
m.VendorID == duplicate.VendorID &&
m.ProductID == duplicate.ProductID).Skip(1).ToArray())
{
if(slave.Descriptors != null &&
master.Descriptors != null)
{
if(!master.Descriptors.SequenceEqual(slave.Descriptors))
continue;
}
foreach(Device device in _context.Devices.Where(d => d.USB.Id == slave.Id))
{
device.USB = master;
}
foreach(UploadedReport report in _context.Reports.Where(d => d.USB.Id == slave.Id))
{
report.USB = master;
}
if(master.Descriptors is null &&
slave.Descriptors != null)
{
master.Descriptors = slave.Descriptors;
_context.Usb.Update(master);
}
_context.Usb.Remove(slave);
}
}
_context.SaveChanges();
return RedirectToAction(nameof(Index));
}
}
}

View File

@@ -0,0 +1,55 @@
@model UsbModelForView
@{
ViewBag.Title = "Consolidate duplicate USB devices";
Layout = "_Layout";
}
<h2>Consolidate duplicate ATAs</h2>
<div>
The following USB devices have duplicates.
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.List[0].Manufacturer)
</th>
<th>
@Html.DisplayNameFor(model => model.List[0].Product)
</th>
<th>
@Html.DisplayNameFor(model => model.List[0].VendorID)
</th>
<th>
@Html.DisplayNameFor(model => model.List[0].ProductID)
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.List)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Manufacturer)
</td>
<td>
@Html.DisplayFor(modelItem => item.Product)
</td>
<td>
@Html.DisplayFor(modelItem => item.VendorID)
</td>
<td>
@Html.DisplayFor(modelItem => item.ProductID)
</td>
</tr>
}
</tbody>
</table>
</div>
<div>
Do you want to remove the duplicates?
<form asp-action="Consolidate" enctype="multipart/form-data">
<input type="hidden" asp-for="Json" name="models" />
<a asp-action="Index" class="btn btn-primary">Back to List</a>
<input class="btn btn-danger" type="submit" value="Consolidate" />
</form>
</div>

View File

@@ -33,6 +33,10 @@
// Copyright © 2011-2019 Natalia Portillo
// ****************************************************************************/
}
USB devices
<div>
<a asp-action="Consolidate" class="btn btn-danger">Consolidate duplicates</a>
</div>
<table class="table">
<thead>
<tr>

View File

@@ -0,0 +1,18 @@
using System.Collections.Generic;
namespace DiscImageChef.Server.Models
{
public class UsbModel
{
public string Manufacturer { get; set; }
public string Product { get; set; }
public ushort VendorID { get; set; }
public ushort ProductID { get; set; }
}
public class UsbModelForView
{
public List<UsbModel> List { get; set; }
public string Json { get; set; }
}
}