using Microsoft.AspNetCore.Components; 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 tempDOT = new List(); private Dictionary files2Chk = new Dictionary(); private Dictionary searchRecords = new Dictionary(); private int sendDataMaxVal = 100; private int sendDataNextVal = 0; private int sendDataVal = 0; private string titleMsg = "Scanning directories"; [Parameter] public CompoSelectFilter actFilter { get; set; } = new CompoSelectFilter(); [Parameter] public EventCallback 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 txt1 { get; set; } = ""; protected string txt2 { 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("CompoBaseDirs:NewCompoDir"); } private string rootPathOld { get => configuration.GetValue("CompoBaseDirs:CurrCompoDir"); } private string rootPathBck { get => configuration.GetValue("CompoBaseDirs:BckCompoDir"); } 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-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 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); //var sb = new StringBuilder(); string dir2chk = ""; foreach (var item in files2Chk) { 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) { await ReplaceFile($"{rootPathNew}\\{item.Key}", $"{rootPathOld}\\{item.Key}"); } else if (item.Value.status == Core.Enum.fileStatus.rem) { File.Delete($"{rootPathOld}\\{item.Key}"); } } } protected async Task doScan() { isLoading = true; //await StartLongTimer(); //isSendingData = true; sendDataVal = 0; sendDataNextVal = 90; searchRecords = 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; } searchRecords.Add(item.Key, item.Value); } 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; sendDataNextVal = 100; isLoading = false; } 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 async Task ReplaceFile(string FileToMoveAndDelete, string FileToReplace) { await Task.Delay(1); File.Replace(FileToMoveAndDelete, FileToReplace, null); } protected void selectAll(bool selUnsel) { foreach (var item in files2Chk) { item.Value.action = selUnsel; } } 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 KeyValuePair actFile { get; set; } = new KeyValuePair(); protected async Task showDiff(KeyValuePair currFile) { await Task.Delay(1); actFile = currFile; //show = !show; await setTxt1(currFile.Key); await setTxt2(currFile.Key); } } }