using Microsoft.AspNetCore.Components; using System.Drawing; using WebDoorCreator.Data.DbModels; using WebDoorCreator.Data.DTO; using WebDoorCreator.Data.Services; namespace WebDoorCreator.UI.Components.FilesMan { public partial class CompoCompare { protected List tempDOT = new List(); protected double expTime { get; set; } = 6; protected int numSteps { get; set; } = 4; private Dictionary files2Chk = new Dictionary(); private int currVal = 100; private int expTimeMSec = 10000; private int maxVal = 100; private int nextVal = 100; private Random rnd = new Random(); private string titleMsg = "Scanning directories"; #if false protected async Task StartLongTimer() { double stdWait = expTime / numSteps; int nextWait = 1000; int stepVal = maxVal / numSteps; // imposto i valori x progress.. maxVal = 100; expTimeMSec = (int)(stdWait * 1000); //nextVal = stepVal; //currVal = 0; for (int currStep = 1; currStep <= numSteps; currStep++) { // aggiorno valori currVal = (currStep - 1) * stepVal; nextVal = currStep * stepVal; // se max mi fermo... if (nextVal > maxVal) { nextVal = maxVal; } await InvokeAsync(StateHasChanged); // simulo ritardo importante (da 2 a 3 volte nextWait = (int)(rnd.Next(2000, 3000) * stdWait); // attendo step successivi... await Task.Delay(nextWait); } await Task.Delay(1); currVal = maxVal; await InvokeAsync(StateHasChanged); } #endif [Inject] protected IConfiguration configuration { get; set; } = null!; [Inject] protected WebDoorCreatorService WDCService { get; set; } = null!; protected string txt1 { get; set; } = ""; protected string txt2 { get; set; } = ""; protected bool isLoading { get; set; } = false; private string rootPathNew { get => configuration.GetValue("CompoBaseDirs:NewCompoDir"); } private bool _selUns { get; set; } = false; private bool selUns { get => _selUns; set { _selUns = value; selectAll(_selUns); } } private string rootPathOld { get => configuration.GetValue("CompoBaseDirs:CurrCompoDir"); } protected string btnCssClass(Core.Enum.fileStatus fStat, bool doAct) { string answ = ""; if (fStat == Core.Enum.fileStatus.add && doAct) { answ = "btn-success"; } 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 void selectAll(bool selUnsel) { foreach (var item in files2Chk) { item.Value.action = selUnsel; } } private int sendDataVal = 0; private int sendDataNextVal = 0; private int sendDataMaxVal = 100; protected async Task doScan() { isLoading = true; //await StartLongTimer(); //isSendingData = true; sendDataVal = 0; sendDataNextVal = 90; files2Chk = new Dictionary(); tempDOT.Clear(); await WDCService.populateHws(); var oldFiles = await WDCService.scanSrcDestDir(rootPathOld); var newFiles = await WDCService.scanSrcDestDir(rootPathNew); //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; } files2Chk.Add(item.Key, item.Value); } foreach (var item in oldFiles) { item.Value.status = Core.Enum.fileStatus.rem; files2Chk.Add(item.Key, item.Value); } files2Chk = files2Chk.Where(x => x.Value.status > 0).ToDictionary(x => x.Key, x => x.Value); sendDataVal = 100; sendDataNextVal = 100; isLoading = false; } protected async Task changeDoAct(KeyValuePair currObj) { await Task.Delay(1); files2Chk[currObj.Key].action = !files2Chk[currObj.Key].action; } protected async Task doSave() { await Task.Delay(1); foreach (var item in files2Chk) { 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) { //await ReplaceFile($"{rootPathNew}{item.Key}", $"{rootPathOld}{item.Key}", $"R:\\WebDoor\\bck\\{item.Key}"); } else if (item.Value.status == Core.Enum.fileStatus.rem) { File.Delete($"{rootPathOld}\\{item.Key}"); } } } protected async Task ReplaceFile(string FileToMoveAndDelete, string FileToReplace, string BackupOfFileToReplace) { await Task.Delay(1); File.Replace(FileToMoveAndDelete, FileToReplace, BackupOfFileToReplace, false); } protected async Task showDiff(string relPath) { await Task.Delay(1); //show = !show; await setTxt1(relPath); await setTxt2(relPath); } protected async Task setTxt1(string fileRelPath) { await Task.Delay(1); txt1 = File.ReadAllText($"{rootPathNew}\\{fileRelPath}"); } protected async Task setTxt2(string fileRelPath) { await Task.Delay(1); txt2 = File.ReadAllText($"{rootPathOld}\\{fileRelPath}"); } protected bool show { get; set; } = false; } }