2019-11-09 19:23:49 +00:00
|
|
|
using System.Collections.Generic;
|
2019-11-08 00:03:43 +00:00
|
|
|
using System.Linq;
|
2019-11-09 23:49:48 +00:00
|
|
|
using System.Reflection;
|
2019-11-08 00:03:43 +00:00
|
|
|
using System.Threading.Tasks;
|
2019-11-09 23:49:48 +00:00
|
|
|
using DiscImageChef.Decoders.ATA;
|
2019-11-08 00:03:43 +00:00
|
|
|
using DiscImageChef.Server.Models;
|
2019-11-08 00:37:51 +00:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2019-11-08 20:30:13 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2019-11-09 19:23:49 +00:00
|
|
|
using Newtonsoft.Json;
|
2019-11-08 00:03:43 +00:00
|
|
|
|
|
|
|
|
namespace DiscImageChef.Server.Areas.Admin.Controllers
|
|
|
|
|
{
|
2019-11-08 20:30:13 +00:00
|
|
|
[Area("Admin"), Authorize]
|
2019-11-08 00:03:43 +00:00
|
|
|
public class AtasController : Controller
|
|
|
|
|
{
|
2019-11-08 20:30:13 +00:00
|
|
|
readonly DicServerContext _context;
|
2019-11-08 00:03:43 +00:00
|
|
|
|
2019-11-08 20:30:13 +00:00
|
|
|
public AtasController(DicServerContext context) => _context = context;
|
2019-11-08 00:03:43 +00:00
|
|
|
|
|
|
|
|
// GET: Admin/Atas
|
2019-11-08 20:30:13 +00:00
|
|
|
public IActionResult Index() => View(_context.Ata.AsEnumerable().Where(m => m.IdentifyDevice?.Model != null).
|
|
|
|
|
OrderBy(m => m.IdentifyDevice.Value.Model));
|
2019-11-08 00:03:43 +00:00
|
|
|
|
|
|
|
|
// GET: Admin/Atas/Details/5
|
|
|
|
|
public async Task<IActionResult> Details(int? id)
|
|
|
|
|
{
|
2019-11-08 20:30:13 +00:00
|
|
|
if(id == null)
|
2019-11-08 00:03:43 +00:00
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-08 20:30:13 +00:00
|
|
|
CommonTypes.Metadata.Ata ata = await _context.Ata.FirstOrDefaultAsync(m => m.Id == id);
|
|
|
|
|
|
|
|
|
|
if(ata == null)
|
2019-11-08 00:03:43 +00:00
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return View(ata);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GET: Admin/Atas/Delete/5
|
|
|
|
|
public async Task<IActionResult> Delete(int? id)
|
|
|
|
|
{
|
2019-11-08 20:30:13 +00:00
|
|
|
if(id == null)
|
2019-11-08 00:03:43 +00:00
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-08 20:30:13 +00:00
|
|
|
CommonTypes.Metadata.Ata ata = await _context.Ata.FirstOrDefaultAsync(m => m.Id == id);
|
|
|
|
|
|
|
|
|
|
if(ata == null)
|
2019-11-08 00:03:43 +00:00
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return View(ata);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// POST: Admin/Atas/Delete/5
|
2019-11-08 20:30:13 +00:00
|
|
|
[HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
|
2019-11-08 00:03:43 +00:00
|
|
|
public async Task<IActionResult> DeleteConfirmed(int id)
|
|
|
|
|
{
|
2019-11-08 20:30:13 +00:00
|
|
|
CommonTypes.Metadata.Ata ata = await _context.Ata.FindAsync(id);
|
2019-11-08 00:03:43 +00:00
|
|
|
_context.Ata.Remove(ata);
|
|
|
|
|
await _context.SaveChangesAsync();
|
2019-11-08 20:30:13 +00:00
|
|
|
|
2019-11-08 00:03:43 +00:00
|
|
|
return RedirectToAction(nameof(Index));
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-08 20:30:13 +00:00
|
|
|
bool AtaExists(int id) => _context.Ata.Any(e => e.Id == id);
|
2019-11-09 19:23:49 +00:00
|
|
|
|
|
|
|
|
public IActionResult Consolidate()
|
|
|
|
|
{
|
|
|
|
|
List<IdHashModel> hashes =
|
|
|
|
|
_context.Ata.Select(m => new IdHashModel(m.Id, Hash.Sha512(m.Identify))).ToList();
|
|
|
|
|
|
|
|
|
|
List<IdHashModel> dups = hashes.GroupBy(x => x.Hash).Where(g => g.Count() > 1).
|
|
|
|
|
Select(x => hashes.FirstOrDefault(y => y.Hash == x.Key)).ToList();
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < dups.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
CommonTypes.Metadata.Ata unique = _context.Ata.First(a => a.Id == dups[i].Id);
|
|
|
|
|
|
|
|
|
|
dups[i].Description = unique.IdentifyDevice?.Model;
|
|
|
|
|
dups[i].Duplicates = hashes.Where(h => h.Hash == dups[i].Hash).Skip(1).Select(x => x.Id).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return View(new IdHashModelForView
|
|
|
|
|
{
|
|
|
|
|
List = dups, Json = JsonConvert.SerializeObject(dups)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost, ActionName("Consolidate"), ValidateAntiForgeryToken]
|
|
|
|
|
public IActionResult ConsolidateConfirmed(string models)
|
|
|
|
|
{
|
|
|
|
|
IdHashModel[] duplicates;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
duplicates = JsonConvert.DeserializeObject<IdHashModel[]>(models);
|
|
|
|
|
}
|
|
|
|
|
catch(JsonSerializationException)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(duplicates is null)
|
|
|
|
|
return BadRequest();
|
|
|
|
|
|
|
|
|
|
foreach(IdHashModel duplicate in duplicates)
|
|
|
|
|
{
|
|
|
|
|
foreach(int duplicateId in duplicate.Duplicates)
|
|
|
|
|
{
|
|
|
|
|
foreach(Device ataDevice in _context.Devices.Where(d => d.ATAId == duplicateId))
|
|
|
|
|
{
|
|
|
|
|
ataDevice.ATAId = duplicate.Id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach(Device atapiDevice in _context.Devices.Where(d => d.ATAPIId == duplicateId))
|
|
|
|
|
{
|
|
|
|
|
atapiDevice.ATAPIId = duplicate.Id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach(UploadedReport ataReport in _context.Reports.Where(d => d.ATAId == duplicateId))
|
|
|
|
|
{
|
|
|
|
|
ataReport.ATAId = duplicate.Id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach(UploadedReport atapiReport in _context.Reports.Where(d => d.ATAPIId == duplicateId))
|
|
|
|
|
{
|
|
|
|
|
atapiReport.ATAPIId = duplicate.Id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_context.Ata.Remove(_context.Ata.First(d => d.Id == duplicateId));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
return RedirectToAction(nameof(Index));
|
|
|
|
|
}
|
2019-11-09 23:49:48 +00:00
|
|
|
|
|
|
|
|
public IActionResult Compare(int id, int rightId)
|
|
|
|
|
{
|
|
|
|
|
var model = new CompareModel();
|
|
|
|
|
|
|
|
|
|
model.LeftId = id;
|
|
|
|
|
model.RightId = rightId;
|
|
|
|
|
|
|
|
|
|
CommonTypes.Metadata.Ata left = _context.Ata.FirstOrDefault(l => l.Id == id);
|
|
|
|
|
CommonTypes.Metadata.Ata right = _context.Ata.FirstOrDefault(r => r.Id == rightId);
|
|
|
|
|
|
|
|
|
|
if(left is null)
|
|
|
|
|
{
|
|
|
|
|
model.ErrorMessage = $"ATA with id {id} has not been found";
|
|
|
|
|
model.HasError = true;
|
|
|
|
|
|
|
|
|
|
return View(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(right is null)
|
|
|
|
|
{
|
|
|
|
|
model.ErrorMessage = $"ATA with id {rightId} has not been found";
|
|
|
|
|
model.HasError = true;
|
|
|
|
|
|
|
|
|
|
return View(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Identify.IdentifyDevice? leftNullable = left.IdentifyDevice;
|
|
|
|
|
Identify.IdentifyDevice? rightNullable = right.IdentifyDevice;
|
|
|
|
|
|
|
|
|
|
if(!leftNullable.HasValue &&
|
|
|
|
|
!rightNullable.HasValue)
|
|
|
|
|
{
|
|
|
|
|
model.AreEqual = true;
|
|
|
|
|
|
|
|
|
|
return View(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(leftNullable.HasValue &&
|
|
|
|
|
!rightNullable.HasValue)
|
|
|
|
|
{
|
|
|
|
|
model.ValueNames.Add("Decoded");
|
|
|
|
|
model.LeftValues.Add("decoded");
|
|
|
|
|
model.RightValues.Add("null");
|
|
|
|
|
|
|
|
|
|
return View(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!leftNullable.HasValue)
|
|
|
|
|
{
|
|
|
|
|
model.ValueNames.Add("Decoded");
|
|
|
|
|
model.LeftValues.Add("null");
|
|
|
|
|
model.RightValues.Add("decoded");
|
|
|
|
|
|
|
|
|
|
return View(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Identify.IdentifyDevice leftValue = left.IdentifyDevice.Value;
|
|
|
|
|
Identify.IdentifyDevice rightValue = right.IdentifyDevice.Value;
|
|
|
|
|
model.ValueNames = new List<string>();
|
|
|
|
|
model.LeftValues = new List<string>();
|
|
|
|
|
model.RightValues = new List<string>();
|
|
|
|
|
|
|
|
|
|
foreach(FieldInfo fieldInfo in leftValue.GetType().GetFields())
|
|
|
|
|
{
|
|
|
|
|
object? lv = fieldInfo.GetValue(leftValue);
|
|
|
|
|
object? rv = fieldInfo.GetValue(rightValue);
|
|
|
|
|
|
|
|
|
|
if(fieldInfo.FieldType.IsArray)
|
|
|
|
|
{
|
|
|
|
|
object[] la = lv as object[];
|
|
|
|
|
object[] ra = rv as object[];
|
|
|
|
|
|
|
|
|
|
switch(la)
|
|
|
|
|
{
|
|
|
|
|
case null when ra is null: continue;
|
|
|
|
|
case null:
|
|
|
|
|
model.ValueNames.Add(fieldInfo.Name);
|
|
|
|
|
model.LeftValues.Add("null");
|
|
|
|
|
model.RightValues.Add("[]");
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(ra is null)
|
|
|
|
|
{
|
|
|
|
|
model.ValueNames.Add(fieldInfo.Name);
|
|
|
|
|
model.LeftValues.Add("[]");
|
|
|
|
|
model.RightValues.Add("null");
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(la.SequenceEqual(ra))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
model.ValueNames.Add(fieldInfo.Name);
|
|
|
|
|
model.LeftValues.Add("[]");
|
|
|
|
|
model.RightValues.Add("[]");
|
|
|
|
|
}
|
|
|
|
|
else if(lv == null &&
|
|
|
|
|
rv == null) { }
|
|
|
|
|
else if(lv != null &&
|
|
|
|
|
rv == null)
|
|
|
|
|
{
|
|
|
|
|
model.ValueNames.Add(fieldInfo.Name);
|
|
|
|
|
model.LeftValues.Add($"{lv}");
|
|
|
|
|
model.RightValues.Add("null");
|
|
|
|
|
}
|
|
|
|
|
else if(lv == null)
|
|
|
|
|
{
|
|
|
|
|
model.ValueNames.Add(fieldInfo.Name);
|
|
|
|
|
model.LeftValues.Add("null");
|
|
|
|
|
model.RightValues.Add($"{rv}");
|
|
|
|
|
}
|
|
|
|
|
else if(!lv.Equals(rv))
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
model.ValueNames.Add(fieldInfo.Name);
|
|
|
|
|
model.LeftValues.Add($"{lv}");
|
|
|
|
|
model.RightValues.Add($"{rv}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
model.AreEqual = model.LeftValues.Count == 0 && model.RightValues.Count == 0;
|
|
|
|
|
|
|
|
|
|
return View(model);
|
|
|
|
|
}
|
2019-11-08 00:03:43 +00:00
|
|
|
}
|
2019-11-08 20:30:13 +00:00
|
|
|
}
|