From 0ed470c6104f12d7696d138587ef06311f9b4fd6 Mon Sep 17 00:00:00 2001 From: "zaccaria.majid" Date: Tue, 25 Jul 2023 11:54:11 +0200 Subject: [PATCH] ok download file richiesti --- SMGen/Components/FilesList.razor | 33 +++++++ SMGen/Components/FilesList.razor.cs | 27 ++++++ SMGen/Pages/Download.cshtml | 5 + SMGen/Pages/Download.cshtml.cs | 53 +++++++++++ SMGen/Pages/SMIn2Event.razor | 10 +- SMGen/Pages/SMIn2Event.razor.cs | 143 ++++++++++++++++------------ SMGen/appsettings.json | 2 +- 7 files changed, 203 insertions(+), 70 deletions(-) create mode 100644 SMGen/Components/FilesList.razor create mode 100644 SMGen/Components/FilesList.razor.cs create mode 100644 SMGen/Pages/Download.cshtml create mode 100644 SMGen/Pages/Download.cshtml.cs diff --git a/SMGen/Components/FilesList.razor b/SMGen/Components/FilesList.razor new file mode 100644 index 0000000..609ed6c --- /dev/null +++ b/SMGen/Components/FilesList.razor @@ -0,0 +1,33 @@ +
+ + + + + + + + + @foreach (var item in FilesStatus) + { + + + @if (item.Value) + { + + + + } + else + { + + } + + } + +
Rul fileStatus
@item.Key + + +  Download Data + @Files2Download[item.Key] +
+
\ No newline at end of file diff --git a/SMGen/Components/FilesList.razor.cs b/SMGen/Components/FilesList.razor.cs new file mode 100644 index 0000000..a8f029f --- /dev/null +++ b/SMGen/Components/FilesList.razor.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Components; +using System.Net.Http; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Components.Authorization; +using Microsoft.AspNetCore.Components.Forms; +using Microsoft.AspNetCore.Components.Routing; +using Microsoft.AspNetCore.Components.Web; +using Microsoft.AspNetCore.Components.Web.Virtualization; +using Microsoft.JSInterop; +using SMGen; +using SMGen.Shared; +using SMGen.Components; + +namespace SMGen.Components +{ + public partial class FilesList + { + [Parameter] + public Dictionary FilesStatus { get; set; } = new Dictionary(); + [Parameter] + public Dictionary Files2Download { get; set; } = new Dictionary(); + } +} \ No newline at end of file diff --git a/SMGen/Pages/Download.cshtml b/SMGen/Pages/Download.cshtml new file mode 100644 index 0000000..833eb1f --- /dev/null +++ b/SMGen/Pages/Download.cshtml @@ -0,0 +1,5 @@ +@page "/Download" + +@model SMGen.Pages.DownloadModel +@{ +} \ No newline at end of file diff --git a/SMGen/Pages/Download.cshtml.cs b/SMGen/Pages/Download.cshtml.cs new file mode 100644 index 0000000..f6ea050 --- /dev/null +++ b/SMGen/Pages/Download.cshtml.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.RazorPages; + +namespace SMGen.Pages +{ + /// + /// Gestione donwnload file csv + /// + /// da valutare eventualmente xlsx con https://github.com/closedxml/closedxml + /// + public class DownloadModel : PageModel + { + #region Private Fields + + private readonly IWebHostEnvironment _env; + + #endregion Private Fields + + #region Public Constructors + + public DownloadModel(IWebHostEnvironment env) + { + _env = env; + } + + #endregion Public Constructors + + #region Public Methods + + public IActionResult OnGet() + { + string fileName = ""; + if (Request.Query.ContainsKey("fileName")) + { + fileName = Request.Query["fileName"]; + } + var filePath = Path.Combine(_env.ContentRootPath, fileName); + //var filePath = Path.Combine(_env.WebRootPath, "..\\temp\\", fileName); + + byte[] fileBytes = System.IO.File.ReadAllBytes(filePath); + + return File(fileBytes, "application/force-download", fileName.Split("\\").Last()); + } + + #endregion Public Methods + } +} \ No newline at end of file diff --git a/SMGen/Pages/SMIn2Event.razor b/SMGen/Pages/SMIn2Event.razor index 8b3d057..12bd768 100644 --- a/SMGen/Pages/SMIn2Event.razor +++ b/SMGen/Pages/SMIn2Event.razor @@ -26,12 +26,4 @@ - -
- @foreach (var item in files2Read) - { - @item.Key -
- } -
- + diff --git a/SMGen/Pages/SMIn2Event.razor.cs b/SMGen/Pages/SMIn2Event.razor.cs index 30f0696..4426f9d 100644 --- a/SMGen/Pages/SMIn2Event.razor.cs +++ b/SMGen/Pages/SMIn2Event.razor.cs @@ -42,6 +42,8 @@ namespace SMGen.Pages protected string sz_file2Read { get; set; } = ""; protected string sz_state_machine_name { get; set; } = ""; protected string n_state_machine_index { get; set; } = ""; + protected string pathFile{ get; set; } = ""; + protected string pathDir{ get; set; } = ""; protected int n_states { get; set; } = 0; protected int n_bits { get; set; } = 0; protected bool b_rules_definition { get; set; } = false; @@ -53,6 +55,8 @@ namespace SMGen.Pages protected List Rules { get; set; } = new List(); protected Dictionary files2Read { get; set; } = new Dictionary(); + protected Dictionary filesStatus { get; set; } = new Dictionary(); + protected Dictionary files2Download { get; set; } = new Dictionary(); //// va dopo //private StreamWriter objWriter { get; set; }; private async Task LoadFiles(InputFileChangeEventArgs e) @@ -61,6 +65,8 @@ namespace SMGen.Pages long maxFileSize = 1024 * 15; int maxAllowedFiles = 150; loadedFiles.Clear(); + filesStatus.Clear(); + files2Read.Clear(); foreach (var file in e.GetMultipleFiles(maxAllowedFiles)) { @@ -72,12 +78,12 @@ namespace SMGen.Pages var trustedFileNameForFileStorage = Path.GetRandomFileName(); //path del file da scrivere - var pathFile = Path.Combine(env.ContentRootPath, + pathFile = Path.Combine(env.ContentRootPath, env.EnvironmentName, "unsafe_uploads", trustedFileNameForFileStorage); //path cartella root (development) - var pathDir = Path.Combine(env.ContentRootPath, + pathDir = Path.Combine(env.ContentRootPath, env.EnvironmentName, "unsafe_uploads"); //se la cartella non esiste, creo @@ -87,7 +93,7 @@ namespace SMGen.Pages } else { - //deleteOldFiles(pathDir); + } //creo file @@ -98,6 +104,7 @@ namespace SMGen.Pages if (file.Name.Contains(".rul")) { files2Read.Add(file.Name, pathFile); + filesStatus.Add(file.Name, false); } } catch (Exception ex) @@ -188,7 +195,7 @@ namespace SMGen.Pages var isOk = checkDirExist(procRootDir); if (isOk) { - evaluate_transitions($"{procRootDir}\\{item.Key.Split(".")[0]}.csv"); + evaluate_transitions($"{procRootDir}\\{item.Key.Split(".")[0]}.csv", item.Key); } break; @@ -213,6 +220,7 @@ namespace SMGen.Pages } } } + } protected bool checkDirExist(string dir) @@ -239,7 +247,7 @@ namespace SMGen.Pages return answ; } - protected void evaluate_transitions(string sz_file2Write) + protected void evaluate_transitions(string sz_file2Write, string origFileName) { using (StreamWriter sw = new StreamWriter(sz_file2Write)) { @@ -256,80 +264,95 @@ namespace SMGen.Pages bool[] b_bit = new bool[20]; bool b_invert = false; //ciclo negli stati - for (var i = 0; i <= n_states - 1; i++) + try { - sz_actual_state = States.ToArray()[i]; - - //ciclo negli ingressi - for (n_input = 0; n_input <= (Math.Pow(2, n_bits) - 1); n_input++) + for (var i = 0; i <= n_states - 1; i++) { - n_mask = 1; - for (var n = 0; n <= n_bits - 1; n++) - { - b_bit[n] = Convert.ToBoolean(n_input & n_mask); - n_mask = n_mask << 1; - } + sz_actual_state = States.ToArray()[i]; - var exitFor = false; - - foreach (var act_rule in Rules) + //ciclo negli ingressi + for (n_input = 0; n_input <= (Math.Pow(2, n_bits) - 1); n_input++) { - n_bit = -1; - if ((act_rule.state == "ALL_STATES") || (act_rule.state == sz_actual_state)) + n_mask = 1; + for (var n = 0; n <= n_bits - 1; n++) { - //DA RESETTARE A FALSE (errore da principianti...) - b_invert = false; + b_bit[n] = Convert.ToBoolean(n_input & n_mask); + n_mask = n_mask << 1; + } - sz_actual_bit = act_rule.expression; + var exitFor = false; - //controllo se la riga contiene l'istruzione per negare il bit - if (sz_actual_bit.Trim().StartsWith("NOT")) + foreach (var act_rule in Rules) + { + n_bit = -1; + if ((act_rule.state == "ALL_STATES") || (act_rule.state == sz_actual_state)) { - //inverto il bit - b_invert = true; - sz_actual_bit = sz_actual_bit.Replace("NOT ", "").Trim(); - } - //cerca il nome del bit e ne trova l' indice da 0 - n_bit = Bits.FindIndex(x => x == sz_actual_bit); + //DA RESETTARE A FALSE (errore da principianti...) + b_invert = false; + sz_actual_bit = act_rule.expression; - if (n_bit == -1) - { - exitFor = true; - } - - if (!exitFor) - { - if (((!b_invert) && b_bit[n_bit]) || (b_invert && (!b_bit[n_bit]))) + //controllo se la riga contiene l'istruzione per negare il bit + if (sz_actual_bit.Trim().StartsWith("NOT")) { - sz_line = ""; + //inverto il bit + b_invert = true; + sz_actual_bit = sz_actual_bit.Replace("NOT ", "").Trim(); + } + //cerca il nome del bit e ne trova l' indice da 0 + n_bit = Bits.FindIndex(x => x == sz_actual_bit); - //eseguo solo se diverso dallo stato corrente - if (States.IndexOf(act_rule.next_state) != i) - { - sz_line = $"{n_state_machine_index}" + - $";" + - $"{i}" + - $";" + - $"{n_input}" + - $";" + - $"{Events_to_send.IndexOf(act_rule.event_to_send)}" + - $";" + - $"{States.IndexOf(act_rule.next_state)}"; - - sw.WriteLine(sz_line); - } - //else - //{ - // sz_line = $"----{n_state_machine_index};{i};{n_input};{Events_to_send.IndexOf(act_rule.event_to_send)};{States.IndexOf(act_rule.next_state)}"; - //} + if (n_bit == -1) + { exitFor = true; } + + if (!exitFor) + { + if (((!b_invert) && b_bit[n_bit]) || (b_invert && (!b_bit[n_bit]))) + { + sz_line = ""; + + //eseguo solo se diverso dallo stato corrente + if (States.IndexOf(act_rule.next_state) != i) + { + + sz_line = $"{n_state_machine_index}" + + $";" + + $"{i}" + + $";" + + $"{n_input}" + + $";" + + $"{Events_to_send.IndexOf(act_rule.event_to_send)}" + + $";" + + $"{States.IndexOf(act_rule.next_state)}"; + + sw.WriteLine(sz_line); + } + //else + //{ + // sz_line = $"----{n_state_machine_index};{i};{n_input};{Events_to_send.IndexOf(act_rule.event_to_send)};{States.IndexOf(act_rule.next_state)}"; + //} + exitFor = true; + } + } } } } } + filesStatus[origFileName] = true; + files2Download.Add(origFileName, sz_file2Write); + //var files2Del = Directory.GetFiles(sz_file2Read); + + //foreach (var item in files2Del) + //{ + // File.Delete(item); + //} + } + catch + { + filesStatus[origFileName] = false; } } } diff --git a/SMGen/appsettings.json b/SMGen/appsettings.json index f1b9572..3c885dd 100644 --- a/SMGen/appsettings.json +++ b/SMGen/appsettings.json @@ -6,7 +6,7 @@ } }, "ServerConf": { - "ProcCsvRootPath": "C:\\Temp\\Rules\\PROCESSED" + "ProcCsvRootPath": "Temp\\Rules\\PROCESSED" }, "AllowedHosts": "*" }