270 lines
10 KiB
C#
270 lines
10 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// Object of this class gives you all the details about the update useful in handling the update logic yourself.
|
|
/// </summary>
|
|
public class UpdateInfoEventArgs : EventArgs
|
|
{
|
|
#region Public Properties
|
|
|
|
/// <summary>
|
|
/// URL of the webpage specifying changes in the new update.
|
|
/// </summary>
|
|
public string ChangelogURL { get; set; }
|
|
|
|
/// <summary>
|
|
/// Returns newest version of the application available to download.
|
|
/// </summary>
|
|
public Version CurrentVersion { get; set; }
|
|
|
|
/// <summary>
|
|
/// Download URL of the update file.
|
|
/// </summary>
|
|
public string DownloadURL { get; set; }
|
|
|
|
/// <summary>
|
|
/// Returns version of the application currently installed on the user's PC.
|
|
/// </summary>
|
|
public Version InstalledVersion { get; set; }
|
|
|
|
/// <summary>
|
|
/// If new update is available then returns true otherwise false.
|
|
/// </summary>
|
|
public bool IsUpdateAvailable { get; set; }
|
|
|
|
/// <summary>
|
|
/// Shows if the update is required or optional.
|
|
/// </summary>
|
|
public bool Mandatory { get; set; }
|
|
|
|
#endregion Public Properties
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gestione update applicazioni
|
|
/// </summary>
|
|
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
|
|
|
|
/// <summary>
|
|
/// Init classe
|
|
/// </summary>
|
|
public static UpdateMan obj = new UpdateMan();
|
|
|
|
#endregion Public Fields
|
|
|
|
#region Public Constructors
|
|
|
|
/// <summary>
|
|
/// Init classe
|
|
/// </summary>
|
|
public UpdateMan()
|
|
{ }
|
|
|
|
/// <summary>
|
|
/// Init classe
|
|
/// </summary>
|
|
/// <param name="user"></param>
|
|
/// <param name="pwd"></param>
|
|
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
|
|
|
|
/// <summary>
|
|
/// Effettua download ultima versione applicativo
|
|
/// </summary>
|
|
/// <param name="remoteUrl"></param>
|
|
/// <param name="localRepo"></param>
|
|
/// <param name="packName"></param>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera l'ultima versione disponibile
|
|
/// </summary>
|
|
/// <param name="remoteUrl"></param>
|
|
/// <returns></returns>
|
|
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
|
|
}
|
|
} |