Files
webdoorcreator/WebDoorCreator.UI/Components/FilesMan/CompoCompare.razor.cs
T
2023-09-11 08:12:41 +02:00

334 lines
11 KiB
C#

using Microsoft.AspNetCore.Components;
using NLog;
using System.Text;
using WebDoorCreator.Data.DbModels;
using WebDoorCreator.Data.DTO;
using WebDoorCreator.Data.Services;
using WebDoorCreator.UI.Data;
namespace WebDoorCreator.UI.Components.FilesMan
{
public partial class CompoCompare
{
protected List<DoorOpTypeModel> tempDOT = new List<DoorOpTypeModel>();
private Dictionary<string, FileDTO> files2Chk = new Dictionary<string, FileDTO>();
private Dictionary<string, FileDTO> searchRecords = new Dictionary<string, FileDTO>();
private Dictionary<string, FileDTO> files2Save = new Dictionary<string, FileDTO>();
private int sendDataMaxVal = 100;
private int sendDataVal = 0;
private string titleMsg = "Scanning directories";
private string scanMsg = "CLICK TO SCAN";
[Parameter]
public CompoSelectFilter actFilter { get; set; } = new CompoSelectFilter();
[Parameter]
public EventCallback<int> updateRecordCount { get; set; }
[Inject]
protected IConfiguration configuration { get; set; } = null!;
protected double expTime { get; set; } = 6;
protected bool isLoading { get; set; } = false;
protected int numSteps { get; set; } = 4;
protected bool show { get; set; } = false;
protected string txtSx { get; set; } = "";
protected string txtDx { get; set; } = "";
[Inject]
protected WebDoorCreatorService WDCService { get; set; } = null!;
private bool _selUns { get; set; } = false;
private int _totalCount { get; set; } = 0;
private int currPage
{
get => actFilter.CurrPage;
set
{
selUns = false;
actFilter.CurrPage = value;
}
}
private int numRecord
{
get => actFilter.NumRec;
set => actFilter.NumRec = value;
}
private string rootPathNew
{
get => configuration.GetValue<string>("CompoBaseDirs:NewCompoDir");
}
private string rootPathOld
{
get => configuration.GetValue<string>("CompoBaseDirs:CurrCompoDir");
}
private bool selUns
{
get => _selUns;
set
{
_selUns = value;
selectAll(_selUns);
}
}
private int totalCount
{
get => _totalCount;
set
{
if (_totalCount != value)
{
_totalCount = value;
updateRecordCount.InvokeAsync(value);
}
}
}
protected string btnCssClass(Core.Enum.fileStatus fStat, bool doAct)
{
string answ = "";
if (fStat == Core.Enum.fileStatus.add && doAct)
{
answ = "btn-primary";
}
else if (fStat == Core.Enum.fileStatus.mod && doAct)
{
answ = "btn-warning";
}
else if (fStat == Core.Enum.fileStatus.rem && doAct)
{
answ = "btn-danger";
}
else
{
answ = "btn-secondary";
}
return answ;
}
protected string btnIcon(Core.Enum.fileStatus fStat)
{
string answ = "";
if (fStat == Core.Enum.fileStatus.add || fStat == Core.Enum.fileStatus.mod)
{
answ = "fa-solid fa-circle-arrow-right";
}
else if (fStat == Core.Enum.fileStatus.rem)
{
answ = "fa-solid fa-square-minus";
}
return answ;
}
protected string btnTxt(Core.Enum.fileStatus fStat)
{
string answ = "";
if (fStat == Core.Enum.fileStatus.add)
{
answ = "A";
}
else if (fStat == Core.Enum.fileStatus.mod)
{
answ = "U";
}
else if (fStat == Core.Enum.fileStatus.rem)
{
answ = "D";
}
return answ;
}
protected async Task changeDoAct(KeyValuePair<string, FileDTO> currObj)
{
await Task.Delay(1);
files2Chk[currObj.Key].action = !files2Chk[currObj.Key].action;
}
protected async Task doSave()
{
await Task.Delay(1);
//var sb = new StringBuilder();
sendDataVal = 0;
var updTask1 = Task.Run(() =>
{
files2Save = searchRecords.Where(x => x.Value.action).ToDictionary(x => x.Key, x => x.Value);
foreach (var item in files2Save)
{
string dir2chk = "";
var fileDirs = item.Key.Split("\\");
foreach (var dir in fileDirs.SkipLast(1))
{
dir2chk += $"\\{dir}";
if (!Directory.Exists($"{rootPathOld}{dir2chk}"))
{
Directory.CreateDirectory($"{rootPathOld}{dir2chk}");
}
}
if (item.Value.status == Core.Enum.fileStatus.add)
{
File.Copy($"{rootPathNew}\\{item.Key}", $"{rootPathOld}\\{item.Key}");
}
else if (item.Value.status == Core.Enum.fileStatus.mod)
{
ReplaceFile($"{rootPathNew}\\{item.Key}", $"{rootPathOld}\\{item.Key}");
}
else if (item.Value.status == Core.Enum.fileStatus.rem)
{
File.Delete($"{rootPathOld}\\{item.Key}");
}
}
}
);
await updTask1;
sendDataVal = 30;
var updTask2 = Task.Run(async () =>
{
await WDCService.ListValuesLuaNgeInsert(rootPathOld);
});
await updTask2;
isLoading = true;
await doScan();
sendDataVal = 90;
}
[Inject]
protected WDCUserService WDCUService { get; set; } = null!;
protected static Logger Log = LogManager.GetCurrentClassLogger();
protected async Task doScan()
{
files2Chk.Clear();
searchRecords.Clear();
Log.Info($"Component scan started by user {WDCUService.userId}");
//await StartLongTimer();
//isSendingData = true;
isLoading = true;
sendDataVal = 0;
var updTask1 = Task.Run(async () =>
await WDCService.populateHws()
);
await updTask1;
searchRecords = new Dictionary<string, FileDTO>();
tempDOT.Clear();
var updTask2 = Task.Run(async () =>
await WDCService.scanSrcDestDir(rootPathOld)
);
var oldFiles = await updTask2;
sendDataVal = 30;
var updTask3 = Task.Run(async () =>
await WDCService.scanSrcDestDir(rootPathNew)
);
var newFiles = await updTask3;
sendDataVal = 60;
//tempDOT = await WDCService.scanAndCompare();
foreach (var item in newFiles)
{
if (oldFiles.ContainsKey(item.Key))
{
if (oldFiles[item.Key].FileMD5 != item.Value.FileMD5)
{
item.Value.status = Core.Enum.fileStatus.mod;
}
oldFiles.Remove(item.Key);
//files2Chk.Add(item.Key, item.Value);
}
else
{
item.Value.status = Core.Enum.fileStatus.add;
}
searchRecords.Add(item.Key, item.Value);
}
sendDataVal = 90;
foreach (var item in oldFiles)
{
item.Value.status = Core.Enum.fileStatus.rem;
searchRecords.Add(item.Key, item.Value);
}
searchRecords = searchRecords.Where(x => x.Value.status > 0).ToDictionary(x => x.Key, x => x.Value);
await ReloadData();
sendDataVal = 100;
isLoading = false;
if (searchRecords.Count == 0)
{
scanMsg = "THE DIRECTORIES HAVE NO DIFFERENCES";
}
Log.Info($"Component scan COMPLETED");
}
protected override async Task OnParametersSetAsync()
{
await ReloadData();
}
protected async Task ReloadData()
{
await Task.Delay(1);
totalCount = searchRecords.Count;
files2Chk = searchRecords
.Skip(numRecord * (currPage - 1))
.Take(numRecord)
.ToDictionary(x => x.Key, x => x.Value);
//await InvokeAsync(() => StateHasChanged());
}
protected void ReplaceFile(string FileToMoveAndDelete, string FileToReplace)
{
File.Replace(FileToMoveAndDelete, FileToReplace, null);
}
protected void selectAll(bool selUnsel)
{
foreach (var item in searchRecords)
{
item.Value.action = selUnsel;
}
}
protected KeyValuePair<string, FileDTO> actFile { get; set; } = new KeyValuePair<string, FileDTO>();
protected async Task showDiff(KeyValuePair<string, FileDTO> currFile, Core.Enum.fileStatus cStatus)
{
await Task.Delay(1);
actFile = currFile;
txtSx = "";
txtDx = "";
// in base al tipo di modifica leggo...
switch (cStatus)
{
case Core.Enum.fileStatus.add:
txtSx = File.ReadAllText($"{rootPathNew}\\{currFile.Key}");
break;
case Core.Enum.fileStatus.mod:
txtSx = File.ReadAllText($"{rootPathNew}\\{currFile.Key}");
txtDx = File.ReadAllText($"{rootPathOld}\\{currFile.Key}");
break;
case Core.Enum.fileStatus.rem:
txtDx = File.ReadAllText($"{rootPathOld}\\{currFile.Key}");
break;
case Core.Enum.fileStatus.none:
default:
break;
}
}
}
}