576 lines
17 KiB
C#
576 lines
17 KiB
C#
using Microsoft.AspNetCore.Components;
|
|
using NLog;
|
|
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
|
|
{
|
|
#region Public Properties
|
|
|
|
[Parameter]
|
|
public CompoSelectFilter actFilter { get; set; } = new CompoSelectFilter();
|
|
|
|
[Parameter]
|
|
public EventCallback<int> updateRecordCount { get; set; }
|
|
|
|
#endregion Public Properties
|
|
|
|
#region Protected Fields
|
|
|
|
protected static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
protected List<DoorOpTypeModel> tempDOT = new List<DoorOpTypeModel>();
|
|
|
|
#endregion Protected Fields
|
|
|
|
#region Protected Properties
|
|
|
|
protected bool _showAdd { get; set; } = true;
|
|
|
|
protected bool _showDel { get; set; } = false;
|
|
|
|
protected bool _showUpd { get; set; } = true;
|
|
|
|
protected KeyValuePair<string, FileDTO> actFile { get; set; } = new KeyValuePair<string, FileDTO>();
|
|
|
|
[Inject]
|
|
protected IConfiguration configuration { get; set; } = null!;
|
|
|
|
protected double expTime { get; set; } = 6;
|
|
|
|
protected bool isLoading { get; set; } = false;
|
|
protected bool isDrawing { get; set; } = false;
|
|
|
|
protected int numSteps { get; set; } = 4;
|
|
|
|
protected bool show { get; set; } = false;
|
|
|
|
protected bool showAdd
|
|
{
|
|
get => _showAdd;
|
|
set => _showAdd = value;
|
|
}
|
|
|
|
protected bool showBtnCur
|
|
{
|
|
get => NumFileCur > 0;
|
|
}
|
|
|
|
protected bool showBtnNew
|
|
{
|
|
get => NumFileNew > 0;
|
|
}
|
|
|
|
protected bool showDel
|
|
{
|
|
get => _showDel;
|
|
set => _showDel = value;
|
|
}
|
|
|
|
protected bool showUpd
|
|
{
|
|
get => _showUpd;
|
|
set => _showUpd = value;
|
|
}
|
|
|
|
protected string txtDx { get; set; } = "";
|
|
|
|
protected string txtSx { get; set; } = "";
|
|
|
|
[Inject]
|
|
protected WebDoorCreatorService WDCService { get; set; } = null!;
|
|
|
|
[Inject]
|
|
protected WDCUserService WDCUService { get; set; } = null!;
|
|
|
|
#endregion Protected Properties
|
|
|
|
#region Protected Methods
|
|
|
|
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;
|
|
isLoading = true;
|
|
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($"{rootPathCur}{dir2chk}"))
|
|
{
|
|
Directory.CreateDirectory($"{rootPathCur}{dir2chk}");
|
|
}
|
|
}
|
|
|
|
if (item.Value.status == Core.Enum.fileStatus.add)
|
|
{
|
|
File.Copy($"{rootPathNew}\\{item.Key}", $"{rootPathCur}\\{item.Key}");
|
|
}
|
|
else if (item.Value.status == Core.Enum.fileStatus.mod)
|
|
{
|
|
ReplaceFile($"{rootPathNew}\\{item.Key}", $"{rootPathCur}\\{item.Key}");
|
|
}
|
|
else if (item.Value.status == Core.Enum.fileStatus.rem)
|
|
{
|
|
File.Delete($"{rootPathCur}\\{item.Key}");
|
|
}
|
|
}
|
|
}
|
|
);
|
|
await updTask1;
|
|
sendDataVal = 30;
|
|
var updTask2 = Task.Run(async () =>
|
|
{
|
|
await WDCService.ListValuesLuaNgeInsert(rootPathCur);
|
|
});
|
|
await updTask2;
|
|
await doScan(true, true);
|
|
sendDataVal = 90;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Scansione e comparazione directory COMPO
|
|
/// </summary>
|
|
/// <param name="scanCur"></param>
|
|
/// <param name="scanNew"></param>
|
|
/// <returns></returns>
|
|
protected async Task doScan(bool scanCur, bool scanNew)
|
|
{
|
|
files2Chk.Clear();
|
|
searchRecords.Clear();
|
|
ListType = new List<string>();
|
|
Log.Info($"Component scan started by user {WDCUService.userId}");
|
|
isDrawing = true;
|
|
isLoading = true;
|
|
sendDataVal = 0;
|
|
var updTask1 = Task.Run(async () =>
|
|
await WDCService.populateHws()
|
|
);
|
|
await updTask1;
|
|
searchRecords = new Dictionary<string, FileDTO>();
|
|
tempDOT.Clear();
|
|
|
|
// gestione files nuovi
|
|
var newFiles = await getCurFiles(rootPathNew, !scanNew);
|
|
NumFileNew = newFiles.Count;
|
|
sendDataVal = 30;
|
|
|
|
// gestione files cur
|
|
var curFiles = await getCurFiles(rootPathCur, !scanCur);
|
|
NumFileCur = curFiles.Count;
|
|
sendDataVal = 60;
|
|
|
|
foreach (var item in newFiles)
|
|
{
|
|
if (curFiles.ContainsKey(item.Key))
|
|
{
|
|
if (curFiles[item.Key].FileMD5 != item.Value.FileMD5)
|
|
{
|
|
item.Value.status = Core.Enum.fileStatus.mod;
|
|
checkItemType(item);
|
|
}
|
|
curFiles.Remove(item.Key);
|
|
}
|
|
else
|
|
{
|
|
item.Value.status = Core.Enum.fileStatus.add;
|
|
checkItemType(item);
|
|
}
|
|
searchRecords.Add(item.Key, item.Value);
|
|
}
|
|
|
|
sendDataVal = 90;
|
|
foreach (var item in curFiles)
|
|
{
|
|
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);
|
|
stats2show.Add(Core.Enum.fileStatus.add);
|
|
stats2show.Add(Core.Enum.fileStatus.mod);
|
|
await ReloadData();
|
|
sendDataVal = 100;
|
|
isLoading = false;
|
|
isDrawing = false;
|
|
if (searchRecords.Count == 0)
|
|
{
|
|
scanMsg = "THE DIRECTORIES HAVE NO DIFFERENCES";
|
|
}
|
|
Log.Info($"Component scan COMPLETED");
|
|
}
|
|
|
|
private void checkItemType(KeyValuePair<string, FileDTO> item)
|
|
{
|
|
string type = getType(item.Key);
|
|
if (!ListType.Contains(type))
|
|
{
|
|
ListType.Add(type);
|
|
}
|
|
}
|
|
|
|
public static string getType(string key)
|
|
{
|
|
string answ = "";
|
|
if (key.Contains("\\"))
|
|
{
|
|
answ = key.Split("\\")[0];
|
|
}
|
|
return answ;
|
|
}
|
|
public static string getFolder(string key)
|
|
{
|
|
string answ = "";
|
|
if (key.Contains("\\"))
|
|
{
|
|
answ = key.Split("\\")[1];
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
protected string filtBtnAddCss()
|
|
{
|
|
string answ = "";
|
|
if (showAdd)
|
|
{
|
|
answ = "btn-primary";
|
|
}
|
|
else
|
|
{
|
|
answ = "btn-secondary";
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
protected string filtBtnRemCss()
|
|
{
|
|
string answ = "";
|
|
if (showDel)
|
|
{
|
|
answ = "btn-danger";
|
|
}
|
|
else
|
|
{
|
|
answ = "btn-secondary";
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
protected async Task filtBtnSet(Core.Enum.fileStatus fileStat)
|
|
{
|
|
switch (fileStat)
|
|
{
|
|
case Core.Enum.fileStatus.add:
|
|
showAdd = !showAdd;
|
|
if (showAdd && !stats2show.Contains(Core.Enum.fileStatus.add))
|
|
{
|
|
stats2show.Add(Core.Enum.fileStatus.add);
|
|
}
|
|
else if (!showAdd && stats2show.Contains(Core.Enum.fileStatus.add))
|
|
{
|
|
stats2show.Remove(Core.Enum.fileStatus.add);
|
|
}
|
|
break;
|
|
|
|
case Core.Enum.fileStatus.mod:
|
|
showUpd = !showUpd;
|
|
if (showUpd && !stats2show.Contains(Core.Enum.fileStatus.mod))
|
|
{
|
|
stats2show.Add(Core.Enum.fileStatus.mod);
|
|
}
|
|
else if (!showUpd && stats2show.Contains(Core.Enum.fileStatus.mod))
|
|
{
|
|
stats2show.Remove(Core.Enum.fileStatus.mod);
|
|
}
|
|
break;
|
|
|
|
case Core.Enum.fileStatus.rem:
|
|
showDel = !showDel;
|
|
if (showDel && !stats2show.Contains(Core.Enum.fileStatus.rem))
|
|
{
|
|
stats2show.Add(Core.Enum.fileStatus.rem);
|
|
}
|
|
else if (!showDel && stats2show.Contains(Core.Enum.fileStatus.rem))
|
|
{
|
|
stats2show.Remove(Core.Enum.fileStatus.rem);
|
|
}
|
|
break;
|
|
}
|
|
await ReloadData();
|
|
}
|
|
|
|
protected string filtBtnUpdCss()
|
|
{
|
|
string answ = "";
|
|
if (showUpd)
|
|
{
|
|
answ = "btn-warning";
|
|
}
|
|
else
|
|
{
|
|
answ = "btn-secondary";
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
protected override async Task OnParametersSetAsync()
|
|
{
|
|
await ReloadData();
|
|
}
|
|
|
|
protected string TypeSel
|
|
{
|
|
get => _typeSel;
|
|
set
|
|
{
|
|
if (_typeSel != value)
|
|
{
|
|
_typeSel = value;
|
|
ReloadData().ConfigureAwait(false);
|
|
}
|
|
}
|
|
}
|
|
private string _typeSel { get; set; } = "*";
|
|
|
|
protected List<string> ListType { get; set; } = new List<string>();
|
|
|
|
protected async Task ReloadData()
|
|
{
|
|
isDrawing = true;
|
|
await Task.Delay(1);
|
|
|
|
var filtData = searchRecords
|
|
.Where(x => stats2show.Contains(x.Value.status) && (TypeSel == "*" || x.Key.StartsWith(TypeSel)))
|
|
.ToList();
|
|
totalCount = filtData.Count;
|
|
|
|
files2Chk = filtData
|
|
.Skip(numRecord * (currPage - 1))
|
|
.Take(numRecord)
|
|
.ToDictionary(x => x.Key, x => x.Value);
|
|
//await InvokeAsync(() => StateHasChanged());
|
|
isDrawing = false;
|
|
}
|
|
|
|
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 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($"{rootPathCur}\\{currFile.Key}");
|
|
break;
|
|
|
|
case Core.Enum.fileStatus.rem:
|
|
txtDx = File.ReadAllText($"{rootPathCur}\\{currFile.Key}");
|
|
break;
|
|
|
|
case Core.Enum.fileStatus.none:
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Private Fields
|
|
|
|
private Dictionary<string, FileDTO> files2Chk = new Dictionary<string, FileDTO>();
|
|
|
|
private Dictionary<string, FileDTO> files2Save = new Dictionary<string, FileDTO>();
|
|
|
|
private string scanMsg = "CLICK TO SCAN";
|
|
|
|
private Dictionary<string, FileDTO> searchRecords = new Dictionary<string, FileDTO>();
|
|
|
|
private int sendDataMaxVal = 100;
|
|
|
|
private int sendDataVal = 0;
|
|
|
|
private List<Core.Enum.fileStatus> stats2show = new List<Core.Enum.fileStatus>();
|
|
|
|
private string titleMsg = "Scanning directories";
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Properties
|
|
|
|
private bool _selUns { get; set; } = false;
|
|
|
|
private int _totalCount { get; set; } = -1;
|
|
|
|
private int currPage
|
|
{
|
|
get => actFilter.CurrPage;
|
|
set
|
|
{
|
|
selUns = false;
|
|
actFilter.CurrPage = value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Numero files CURRENT
|
|
/// </summary>
|
|
private int NumFileCur { get; set; } = 0;
|
|
|
|
/// <summary>
|
|
/// Numero files NEW
|
|
/// </summary>
|
|
private int NumFileNew { get; set; } = 0;
|
|
|
|
private int numRecord
|
|
{
|
|
get => actFilter.NumRec;
|
|
set => actFilter.NumRec = value;
|
|
}
|
|
|
|
private string rootPathCur
|
|
{
|
|
get => configuration.GetValue<string>("CompoBaseDirs:CurrCompoDir");
|
|
}
|
|
|
|
private string rootPathNew
|
|
{
|
|
get => configuration.GetValue<string>("CompoBaseDirs:NewCompoDir");
|
|
}
|
|
|
|
private bool selUns
|
|
{
|
|
get => _selUns;
|
|
set
|
|
{
|
|
_selUns = value;
|
|
selectAll(_selUns);
|
|
}
|
|
}
|
|
|
|
private int totalCount
|
|
{
|
|
get => _totalCount;
|
|
set
|
|
{
|
|
if (_totalCount != value)
|
|
{
|
|
_totalCount = value;
|
|
updateRecordCount.InvokeAsync(value);
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion Private Properties
|
|
|
|
#region Private Methods
|
|
|
|
/// <summary>
|
|
/// Recupera i dati files current,
|
|
/// </summary>
|
|
/// <param name="rootPath">Path di ricerca</param>
|
|
/// <param name="useCache">Usa cache Redis vs lettura da filesystem</param>
|
|
/// <returns></returns>
|
|
private async Task<Dictionary<string, FileDTO>> getCurFiles(string rootPath, bool useCache)
|
|
{
|
|
var updTask = Task.Run(async () =>
|
|
await WDCService.scanSrcDestDir(rootPath, useCache)
|
|
);
|
|
var listFiles = await updTask;
|
|
return listFiles;
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |