2019-11-17 18:11:51 +00:00
|
|
|
using System.Collections.Generic;
|
2019-11-08 00:03:43 +00:00
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using DiscImageChef.CommonTypes.Metadata;
|
|
|
|
|
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-17 18:11:51 +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 ScsisController : 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 ScsisController(DicServerContext context) => _context = context;
|
2019-11-08 00:03:43 +00:00
|
|
|
|
|
|
|
|
// GET: Admin/Scsis
|
2019-11-08 20:30:13 +00:00
|
|
|
public async Task<IActionResult> Index() => View(await _context.Scsi.ToListAsync());
|
2019-11-08 00:03:43 +00:00
|
|
|
|
|
|
|
|
// GET: Admin/Scsis/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
|
|
|
Scsi scsi = await _context.Scsi.FirstOrDefaultAsync(m => m.Id == id);
|
|
|
|
|
|
|
|
|
|
if(scsi == null)
|
2019-11-08 00:03:43 +00:00
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return View(scsi);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GET: Admin/Scsis/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
|
|
|
Scsi scsi = await _context.Scsi.FirstOrDefaultAsync(m => m.Id == id);
|
|
|
|
|
|
|
|
|
|
if(scsi == null)
|
2019-11-08 00:03:43 +00:00
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return View(scsi);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// POST: Admin/Scsis/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
|
|
|
Scsi scsi = await _context.Scsi.FindAsync(id);
|
2019-11-08 00:03:43 +00:00
|
|
|
_context.Scsi.Remove(scsi);
|
|
|
|
|
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-17 18:11:51 +00:00
|
|
|
public IActionResult Consolidate()
|
|
|
|
|
{
|
|
|
|
|
List<IdHashModel> hashes = _context.Scsi.Where(m => m.InquiryData != null).
|
|
|
|
|
Select(m => new IdHashModel(m.Id, Hash.Sha512(m.InquiryData))).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++)
|
|
|
|
|
{
|
|
|
|
|
Scsi unique = _context.Scsi.First(a => a.Id == dups[i].Id);
|
|
|
|
|
|
|
|
|
|
dups[i].Description =
|
|
|
|
|
$"{StringHandlers.CToString(unique.Inquiry?.VendorIdentification)} {StringHandlers.CToString(unique.Inquiry?.ProductIdentification)}";
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
Scsi master = _context.Scsi.FirstOrDefault(m => m.Id == duplicate.Id);
|
|
|
|
|
|
|
|
|
|
if(master is null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
foreach(int duplicateId in duplicate.Duplicates)
|
|
|
|
|
{
|
|
|
|
|
Scsi slave = _context.Scsi.FirstOrDefault(m => m.Id == duplicateId);
|
|
|
|
|
|
|
|
|
|
if(slave is null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
foreach(Device scsiDevice in _context.Devices.Where(d => d.SCSI.Id == duplicateId))
|
|
|
|
|
{
|
|
|
|
|
scsiDevice.SCSI = master;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach(UploadedReport scsiReport in _context.Reports.Where(d => d.SCSI.Id == duplicateId))
|
|
|
|
|
{
|
|
|
|
|
scsiReport.SCSI = master;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach(TestedMedia testedMedia in _context.TestedMedia.Where(d => d.ScsiId == duplicateId))
|
|
|
|
|
{
|
|
|
|
|
testedMedia.ScsiId = duplicate.Id;
|
|
|
|
|
_context.Update(testedMedia);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(master.ReadCapabilities is null &&
|
|
|
|
|
slave.ReadCapabilities != null)
|
|
|
|
|
master.ReadCapabilities = slave.ReadCapabilities;
|
|
|
|
|
|
|
|
|
|
_context.Scsi.Remove(slave);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
return RedirectToAction(nameof(Index));
|
|
|
|
|
}
|
2019-11-08 00:03:43 +00:00
|
|
|
}
|
2019-11-08 20:30:13 +00:00
|
|
|
}
|