From 4f4cd7bf2f74928867258a7d77153868ff4bb294 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Mon, 20 Sep 2021 19:21:42 +0200 Subject: [PATCH] Ok singolo download --- MP.AppAuth/Controllers/AppAuthController.cs | 1 - MP.AppAuth/Models/UpdMan.cs | 2 +- MP.AppAuth/UpdateMan.cs | 270 ++++++++++++++++++++ MP.Land/Components/HomeLink.razor | 2 +- MP.Land/Components/SingleDownload.razor | 14 + MP.Land/Components/SingleDownload.razor.cs | 111 ++++++++ MP.Land/Components/UserCard.razor | 4 +- MP.Land/MP.Land.csproj | 2 +- MP.Land/Pages/Index.razor | 2 +- MP.Land/Pages/Index.razor.cs | 9 - MP.Land/Pages/UpdateManager.razor | 48 ++++ MP.Land/Pages/UpdateManager.razor.cs | 64 +++++ MP.Land/Resources/ChangeLog.html | 2 +- MP.Land/Resources/VersNum.txt | 2 +- MP.Land/Resources/manifest.xml | 2 +- MP.Land/Shared/NavMenu.razor | 4 +- MP.Land/appsettings.json | 2 + MP.Land/wwwroot/img/LogoBluGreen.png | Bin 0 -> 87309 bytes MP.Land/wwwroot/img/LogoMapoFull.png | Bin 0 -> 157981 bytes 19 files changed, 520 insertions(+), 21 deletions(-) create mode 100644 MP.AppAuth/UpdateMan.cs create mode 100644 MP.Land/Components/SingleDownload.razor create mode 100644 MP.Land/Components/SingleDownload.razor.cs create mode 100644 MP.Land/Pages/UpdateManager.razor create mode 100644 MP.Land/Pages/UpdateManager.razor.cs create mode 100644 MP.Land/wwwroot/img/LogoBluGreen.png create mode 100644 MP.Land/wwwroot/img/LogoMapoFull.png diff --git a/MP.AppAuth/Controllers/AppAuthController.cs b/MP.AppAuth/Controllers/AppAuthController.cs index 3c2897a9..316d3743 100644 --- a/MP.AppAuth/Controllers/AppAuthController.cs +++ b/MP.AppAuth/Controllers/AppAuthController.cs @@ -96,7 +96,6 @@ namespace MP.AppAuth.Controllers dbResult = dbCtx .DbSetUpdMan - //.Where(x => !string.IsNullOrEmpty(x.AppName)) .ToList(); return dbResult; diff --git a/MP.AppAuth/Models/UpdMan.cs b/MP.AppAuth/Models/UpdMan.cs index ff1efe8b..d1496155 100644 --- a/MP.AppAuth/Models/UpdMan.cs +++ b/MP.AppAuth/Models/UpdMan.cs @@ -16,7 +16,7 @@ namespace MP.AppAuth.Models public string AppName { get; set; } public string AppUrl { get; set; } - public bool? IsAuth { get; set; } + public bool IsAuth { get; set; } = false; public string LicenseKey { get; set; } public string LocalRepo { get; set; } public string ManifestUrl { get; set; } diff --git a/MP.AppAuth/UpdateMan.cs b/MP.AppAuth/UpdateMan.cs new file mode 100644 index 00000000..06b39a98 --- /dev/null +++ b/MP.AppAuth/UpdateMan.cs @@ -0,0 +1,270 @@ +using NLog; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using System.Xml; + +namespace MP.AppAuth +{ + /// + /// Object of this class gives you all the details about the update useful in handling the update logic yourself. + /// + public class UpdateInfoEventArgs : EventArgs + { + #region Public Properties + + /// + /// URL of the webpage specifying changes in the new update. + /// + public string ChangelogURL { get; set; } + + /// + /// Returns newest version of the application available to download. + /// + public Version CurrentVersion { get; set; } + + /// + /// Download URL of the update file. + /// + public string DownloadURL { get; set; } + + /// + /// Returns version of the application currently installed on the user's PC. + /// + public Version InstalledVersion { get; set; } + + /// + /// If new update is available then returns true otherwise false. + /// + public bool IsUpdateAvailable { get; set; } + + /// + /// Shows if the update is required or optional. + /// + public bool Mandatory { get; set; } + + #endregion Public Properties + } + + /// + /// Gestione update applicazioni + /// + public class UpdateMan + { + #region Private Fields + + private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); + + #endregion Private Fields + + #region Protected Fields + + protected bool doAuth = false; + + #endregion Protected Fields + + #region Public Fields + + /// + /// Init classe + /// + public static UpdateMan obj = new UpdateMan(); + + #endregion Public Fields + + #region Public Constructors + + /// + /// Init classe + /// + public UpdateMan() + { } + + /// + /// Init classe + /// + /// + /// + public UpdateMan(string user, string pwd) + { + userName = user; + passwd = pwd; + doAuth = !string.IsNullOrEmpty($"{userName}{passwd}"); + } + + #endregion Public Constructors + + #region Protected Properties + + protected string passwd { get; set; } = ""; + protected string userName { get; set; } = ""; + + #endregion Protected Properties + + #region Public Methods + + /// + /// Effettua download ultima versione applicativo + /// + /// + /// + /// + public long downloadLatest(string remoteUrl, string localRepo, string packName) + { + long size = 0; + // verifico directory + if (!Directory.Exists(localRepo)) + { + Directory.CreateDirectory(localRepo); + } + string localFile = ""; + string localLast = ""; + string DownloadURL = ""; + try + { + // in primis scarico update info... + UpdateInfoEventArgs updateData = getUpdateInfo(remoteUrl); + if (updateData.IsUpdateAvailable) + { + DownloadURL = updateData.DownloadURL; + localFile = string.Format(@"{0}\{1}_Build_{2}.zip", localRepo, packName, updateData.CurrentVersion); + localLast = string.Format(@"{0}\{1}.zip", localRepo, packName); + // scarica file e salva in directory locale... + using (var http = new HttpClient()) + { + //var client = new WebClient(); + //bool doAuth = !string.IsNullOrEmpty($"{userName}{passwd}"); + if (doAuth) + { + //client.Credentials = new System.Net.NetworkCredential(userName, passwd); + http.DefaultRequestHeaders.Accept.Clear(); + var byteArray = System.Text.ASCIIEncoding.UTF8.GetBytes($"{userName}:{passwd}"); + http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); + } + //client.DownloadFile(updateData.DownloadURL, localFile); + + // url da chiamare + http.BaseAddress = new Uri(DownloadURL); + // lettura + var data = http.GetAsync(DownloadURL).Result; + using (var myReader = data.Content.ReadAsStreamAsync()) + { + //var myReader = data.Content.ReadAsStreamAsync(); + if (data.StatusCode != HttpStatusCode.OK) + { + Log.Info($"Errore in chiamata getUpdateInfo per URL {remoteUrl}: status code: {data.StatusCode}"); + } + else + { + using (Stream outStream = File.OpenWrite(localFile)) + { + myReader.Result.CopyTo(outStream); + } + // ora lo copio come latest... + File.Copy(localFile, localLast, true); + // indico come fatto! + var thisFile = new FileInfo(localLast); + size = thisFile.Length; + } + } + } + } + else + { + localFile = string.Format(@"{0}\Error.log", localRepo); + // scrivo file in area target... + using (StreamWriter sw = File.AppendText(localFile)) + { + sw.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} ATTENZIONE! Installer NON disponibile | {remoteUrl}"); + } + } + } + catch (Exception exc) + { + localFile = string.Format(@"{0}\Error.log", localRepo); + // scrivo file in area target... + using (StreamWriter sw = File.AppendText(localFile)) + { + sw.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} Errore in fase di download installers{Environment.NewLine}XML: {remoteUrl}{Environment.NewLine}URL: {DownloadURL}"); + sw.WriteLine(exc.ToString()); + } + } + return size; + } + + /// + /// Recupera l'ultima versione disponibile + /// + /// + /// + public UpdateInfoEventArgs getUpdateInfo(string remoteUrl) + { + //bool doAuth = !string.IsNullOrEmpty($"{userName}{passwd}"); + + UpdateInfoEventArgs args = new UpdateInfoEventArgs(); + Version CurrentVersion; + bool Mandatory; + XmlDocument recXml = new XmlDocument(); + try + { + // recupero dati + using (var http = new HttpClient()) + { + if (doAuth) + { + http.DefaultRequestHeaders.Accept.Clear(); + var byteArray = System.Text.ASCIIEncoding.UTF8.GetBytes($"{userName}:{passwd}"); + http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); + } + // url da chiamare + http.BaseAddress = new Uri(remoteUrl); + // lettura + var data = http.GetAsync(remoteUrl).Result; + var myReader = data.Content.ReadAsStreamAsync(); + if (data.StatusCode != HttpStatusCode.OK) + { + Log.Info($"Errore in chiamata getUpdateInfo per URL {remoteUrl}: status code: {data.StatusCode}"); + } + else + { + recXml.Load(myReader.Result); + + XmlNodeList recData = recXml.SelectNodes("item"); + if (recData != null) + { + foreach (XmlNode item in recData) + { + XmlNode appCastVersion = item.SelectSingleNode("version"); + Version.TryParse(appCastVersion?.InnerText, out CurrentVersion); + args.CurrentVersion = CurrentVersion; + XmlNode appCastChangeLog = item.SelectSingleNode("changelog"); + args.ChangelogURL = appCastChangeLog?.InnerText; + XmlNode appCastUrl = item.SelectSingleNode("url"); + args.DownloadURL = appCastUrl?.InnerText; + XmlNode mandatory = item.SelectSingleNode("mandatory"); + Boolean.TryParse(mandatory?.InnerText, out Mandatory); + args.Mandatory = Mandatory; + args.IsUpdateAvailable = true; + } + } + } + } + } + catch (Exception exc) + { + Log.Info($"Eccezione in getUpdateInfo:{Environment.NewLine}{exc}"); + // metto versione = 0 + errore... + args.IsUpdateAvailable = false; + args.CurrentVersion = new Version("0.0.0.0"); + } + return args; + } + + #endregion Public Methods + } +} \ No newline at end of file diff --git a/MP.Land/Components/HomeLink.razor b/MP.Land/Components/HomeLink.razor index e8ca1aa0..653cb0fa 100644 --- a/MP.Land/Components/HomeLink.razor +++ b/MP.Land/Components/HomeLink.razor @@ -67,7 +67,7 @@ protected bool authOk() { - bool answ = !string.IsNullOrEmpty(CurrItem.LicenseKey) && Convert.ToBoolean(CurrItem.IsAuth); + bool answ = !string.IsNullOrEmpty(CurrItem.LicenseKey); return answ; } diff --git a/MP.Land/Components/SingleDownload.razor b/MP.Land/Components/SingleDownload.razor new file mode 100644 index 00000000..4530ffe6 --- /dev/null +++ b/MP.Land/Components/SingleDownload.razor @@ -0,0 +1,14 @@ +
+ @if (authOk()) + { +
+

@CurrItem.AppName 

+
+
+ +
+
+ @outMessage +
+ } +
\ No newline at end of file diff --git a/MP.Land/Components/SingleDownload.razor.cs b/MP.Land/Components/SingleDownload.razor.cs new file mode 100644 index 00000000..58975b3d --- /dev/null +++ b/MP.Land/Components/SingleDownload.razor.cs @@ -0,0 +1,111 @@ +using Microsoft.AspNetCore.Components; +using MP.AppAuth; +using MP.AppAuth.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.Extensions.Configuration; +using MP.Land.Data; +using System.Diagnostics; + +namespace MP.Land.Components +{ + public partial class SingleDownload + { + #region Protected Fields + + protected string outMessage = ""; + protected UpdateInfoEventArgs updArgs = new UpdateInfoEventArgs(); + protected UpdateMan updateManAuth = new UpdateMan("SWDownloader", "viaD@nte16"); + + #endregion Protected Fields + + #region Protected Properties + + [Inject] + protected IConfiguration Configuration { get; set; } + + [Inject] + protected AppAuthService DataService { get; set; } + + #endregion Protected Properties + + #region Public Properties + + [Parameter] + public UpdMan CurrItem { get; set; } + + #endregion Public Properties + + #region Protected Methods + + protected bool authOk() + { + bool answ = !string.IsNullOrEmpty(CurrItem.LicenseKey); + return answ; + } + + /// + /// effettua download del pacchetto indicato + /// + protected void DownloadPack() + { + long size = 0; + Stopwatch stopWatch = new Stopwatch(); + stopWatch.Start(); + if (CurrItem.IsAuth) + { + size = updateManAuth.downloadLatest(CurrItem.ManifestUrl, localPath(CurrItem.LocalRepo), CurrItem.PackName); + } + else + { + size = UpdateMan.obj.downloadLatest(CurrItem.ManifestUrl, localPath(CurrItem.LocalRepo), CurrItem.PackName); + } + stopWatch.Stop(); + TimeSpan ts = stopWatch.Elapsed; + + if (size > 0) + { + outMessage = $"Scaricati {(double)size / (1024 * 1024):N2} Mb in {ts.TotalSeconds:N2} s"; + } + else + { + outMessage = $"Download non riuscito per {CurrItem.AppName}"; + } + } + + protected string fullUrl(string relUrl) + { + return $"{Configuration["BaseUrl"]}{relUrl}"; + } + + protected string localPath(string localRepo) + { + return @$"{Configuration["downloadPath"]}\{localRepo}\{Configuration["appVers"]}"; ; + } + + protected override void OnInitialized() + { + // Recupero info ADM... + if (CurrItem.IsAuth) + { + updArgs = updateManAuth.getUpdateInfo(CurrItem.ManifestUrl); + } + else + { + updArgs = UpdateMan.obj.getUpdateInfo(CurrItem.ManifestUrl); + } + } + + protected MarkupString traduci(string lemma) + { + MarkupString answ; + string rawHtml = DataService.Traduci(lemma, "IT"); + answ = new MarkupString(rawHtml); + return answ; + } + + #endregion Protected Methods + } +} \ No newline at end of file diff --git a/MP.Land/Components/UserCard.razor b/MP.Land/Components/UserCard.razor index 6e0b7852..247d9bbf 100644 --- a/MP.Land/Components/UserCard.razor +++ b/MP.Land/Components/UserCard.razor @@ -8,10 +8,10 @@
- EgalWare + EgalWare
- @* EgalWare*@ + @* EgalWare*@

@Environment

diff --git a/MP.Land/MP.Land.csproj b/MP.Land/MP.Land.csproj index 7f2be65a..85d4b197 100644 --- a/MP.Land/MP.Land.csproj +++ b/MP.Land/MP.Land.csproj @@ -3,7 +3,7 @@ net5.0 MP.Land - 1.1.2109.2016 + 1.1.2109.2019 diff --git a/MP.Land/Pages/Index.razor b/MP.Land/Pages/Index.razor index 6c3b2895..54c0195e 100644 --- a/MP.Land/Pages/Index.razor +++ b/MP.Land/Pages/Index.razor @@ -17,7 +17,7 @@
diff --git a/MP.Land/Pages/Index.razor.cs b/MP.Land/Pages/Index.razor.cs index a5c69119..89eb18b1 100644 --- a/MP.Land/Pages/Index.razor.cs +++ b/MP.Land/Pages/Index.razor.cs @@ -24,12 +24,6 @@ namespace MP.Land.Pages #endregion Protected Fields - #region Private Properties - - private bool isLoading { get; set; } = false; - - #endregion Private Properties - #region Protected Properties [Inject] @@ -56,14 +50,11 @@ namespace MP.Land.Pages protected async Task ReloadAllData() { - isLoading = true; - //MacList = await DataService.MacchineGetAll(); await ReloadData(); } protected async Task ReloadData() { - isLoading = true; // importante altrimenti NON mostra update UI await Task.Delay(1); ListRecords = await DataService.UpdManList(); diff --git a/MP.Land/Pages/UpdateManager.razor b/MP.Land/Pages/UpdateManager.razor new file mode 100644 index 00000000..9fd8b77b --- /dev/null +++ b/MP.Land/Pages/UpdateManager.razor @@ -0,0 +1,48 @@ +@page "/UpdateManager" + +@using MP.Land.Data +@using MP.Land.Components + +
+
+
+ +
+
+

Aggiornamento Moduli

+
+ Ultime release applicazioni +
+

Scaricare uno o più pacchetti selezionando dall'elenco riportato.

+
+
+
+ ALL Packages +
+ +
+
+
+
+
+ @if (ListRecords == null) + { + + } + else if (totalCount == 0) + { +
Nessun record trovato
+ } + else + { +
+ @foreach (var item in ListRecords) + { +
+ +
+ } +
+ } +
+
\ No newline at end of file diff --git a/MP.Land/Pages/UpdateManager.razor.cs b/MP.Land/Pages/UpdateManager.razor.cs new file mode 100644 index 00000000..0d06a403 --- /dev/null +++ b/MP.Land/Pages/UpdateManager.razor.cs @@ -0,0 +1,64 @@ +using Microsoft.AspNetCore.Components; +using MP.Land.Data; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MP.Land.Pages +{ + public partial class UpdateManager + { + #region Private Fields + + private List ListRecords; + + #endregion Private Fields + + #region Protected Fields + + protected int totalCount = 0; + + #endregion Protected Fields + + #region Protected Properties + + [Inject] + protected MessageService AppMService { get; set; } + + [Inject] + protected AppAuthService DataService { get; set; } + + #endregion Protected Properties + + #region Protected Methods + + protected override void OnInitialized() + { + AppMService.ShowSearch = false; + AppMService.PageName = "Update Manager"; + AppMService.PageIcon = "fas fa-download pr-2"; + } + + protected override async Task OnInitializedAsync() + { + await ReloadAllData(); + } + + protected async Task ReloadAllData() + { + await ReloadData(); + } + + protected async Task ReloadData() + { + // importante altrimenti NON mostra update UI + await Task.Delay(1); + ListRecords = await DataService.UpdManList(); + totalCount = ListRecords.Count(); + await Task.Delay(1); + } + + #endregion Protected Methods + } +} \ No newline at end of file diff --git a/MP.Land/Resources/ChangeLog.html b/MP.Land/Resources/ChangeLog.html index 324bb6d0..5b062486 100644 --- a/MP.Land/Resources/ChangeLog.html +++ b/MP.Land/Resources/ChangeLog.html @@ -1,6 +1,6 @@ Modulo gestione Programmi MAPO -

Versione: 1.1.2109.2016

+

Versione: 1.1.2109.2019


Note di rilascio: