using EgwCoreLib.Razor; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Forms; using Microsoft.VisualBasic; using NLog.Fluent; using SMGen.Core; using SMGen.Data; using SMGen.Data.Data; using SMGen.Data.Services; namespace SMGen.Pages { public partial class FilesFix { protected Dictionary eventsFromDb = new Dictionary(); protected Dictionary statesFromDb = new Dictionary(); [Inject] protected SMGDataService SMGDService { get; set; } = null!; protected Dictionary Files { get; set; } = new Dictionary(); protected int maxAllowedFiles { get; set; } = 100; protected string pathDir { get; set; } = ""; protected string pathFile { get; set; } = ""; protected int resetSucc { get; set; } = 0; private async Task LoadFiles(InputFileChangeEventArgs e) { List loadedFiles = new(); long maxFileSize = 1024 * 1024; loadedFiles.Clear(); Files.Clear(); // path cartella root (development) pathDir = Path.Combine("Temp", "unsafe_uploads"); // se la cartella non esistesse la creo if (!Directory.Exists(pathDir)) { Directory.CreateDirectory(pathDir); Log.Info($"Creato directory {pathDir}"); } foreach (var file in e.GetMultipleFiles(maxAllowedFiles)) { try { loadedFiles.Add(file); //assegno un nome file randomico x sicurezza var trustedFileNameForFileStorage = Path.GetRandomFileName(); //path del file da scrivere pathFile = Path.Combine(pathDir, trustedFileNameForFileStorage); // creo file using (FileStream fs = new(pathFile, FileMode.Create)) { // copio il contenuto del file await file.OpenReadStream(maxFileSize).CopyToAsync(fs); // scrivo log Log.Info($"Salvato file temp {pathFile}"); } if (file.Name.Contains(".rul")) { var newFIle = new FilesClass() { tempFileName = pathFile, isOk = false, origFileName = file.Name, calcRunning = false, DLoadFileName = "" }; Files.Add(file.Name, newFIle); } } catch (Exception exc) { Log.Error($"Errore durante salvataggio file temp {file.Name}: {exc}{Environment.NewLine}"); } } if (Files != null && Files.Count > 0) { await SMGDService.FilesLoadRedis(Files); } resetSucc = 0; await InvokeAsync(() => StateHasChanged()); await ReloadData(); } protected override async Task OnInitializedAsync() { await Task.Delay(1); await SMGDService.ExecFlushRedisPattern(Core.Constants.FILES_TO_PROC); } #region Protected Methods protected void ForceReload(int newNum) { numRecord = newNum; } protected void ForceReloadPage(int newNum) { currPage = newNum; } protected SelectSMIn2EvParams currFilter = new SelectSMIn2EvParams(); protected DataPager? pagerRulFix = null!; protected async Task pgResetReq(bool doReset) { if (doReset) { await Task.Delay(1); if (pagerRulFix != null) { pagerRulFix.resetCurrPage(); } } } protected void UpdateTotCount(int newTotCount) { totalCount = newTotCount; } #endregion Protected Methods #region Private Properties private int currPage { get => currFilter.CurrPage; set => currFilter.CurrPage = value; } private int numRecord { get => currFilter.NumRec; set => currFilter.NumRec = value; } private int totalCount { get => currFilter.TotCount; set => currFilter.TotCount = value; } #endregion Private Properties protected async Task ReloadData() { Files = await SMGDService.FilesGetAll(); } } }