From 674ca83ba9243a9e95a7568c797668dab6aee26a Mon Sep 17 00:00:00 2001 From: Lucas Faria Mendes Date: Mon, 30 Mar 2026 10:35:25 -0300 Subject: feat: upload files --- .../ViewModels/Ferramentas/DownloadViewModel.cs | 550 +++++++++++++++++++++ 1 file changed, 550 insertions(+) create mode 100644 Gestor.Application/ViewModels/Ferramentas/DownloadViewModel.cs (limited to 'Gestor.Application/ViewModels/Ferramentas/DownloadViewModel.cs') diff --git a/Gestor.Application/ViewModels/Ferramentas/DownloadViewModel.cs b/Gestor.Application/ViewModels/Ferramentas/DownloadViewModel.cs new file mode 100644 index 0000000..66da49e --- /dev/null +++ b/Gestor.Application/ViewModels/Ferramentas/DownloadViewModel.cs @@ -0,0 +1,550 @@ +using Gestor.Application.Helpers; +using Gestor.Application.ViewModels.Generic; +using Gestor.Common.Validation; +using Gestor.Model.API; +using Gestor.Model.Domain.Common; +using Gestor.Model.Domain.Generic; +using Gestor.Model.Domain.Seguros; +using IWshRuntimeLibrary; +using System; +using System.Collections.Generic; +using System.Collections.Specialized; +using System.ComponentModel; +using System.Diagnostics; +using System.Globalization; +using System.IO; +using System.IO.Compression; +using System.Linq; +using System.Net; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Text.RegularExpressions; +using System.Threading; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Media; + +namespace Gestor.Application.ViewModels.Ferramentas +{ + public class DownloadViewModel : BaseSegurosViewModel + { + private const string _path = "c:\\AggerSeguros\\"; + + private string _head; + + private string _total; + + private string _progresso; + + private decimal _porcentagem; + + private bool _processando = true; + + private Geometry _maximizeRestore = Geometry.Parse((string)System.Windows.Application.Current.Resources["Maximize"]); + + private string CurrentVersion + { + get; + set; + } + + public string Head + { + get + { + return this._head; + } + set + { + this._head = value; + base.OnPropertyChanged("Head"); + } + } + + public Geometry MaximizeRestore + { + get + { + return this._maximizeRestore; + } + set + { + this._maximizeRestore = value; + base.OnPropertyChanged("MaximizeRestore"); + } + } + + private Gestor.Model.API.Parameters Parameters + { + get; + set; + } + + public decimal Porcentagem + { + get + { + return this._porcentagem; + } + set + { + this._porcentagem = value; + this.Progresso = string.Format("{0}% de {1} MB", value, this.Total); + base.OnPropertyChanged("Porcentagem"); + } + } + + public bool Processando + { + get + { + return this._processando; + } + set + { + this._processando = value; + base.OnPropertyChanged("Processando"); + } + } + + public string Progresso + { + get + { + return this._progresso; + } + set + { + this._progresso = value; + base.OnPropertyChanged("Progresso"); + } + } + + public string Total + { + get + { + return this._total; + } + set + { + this._total = value; + base.OnPropertyChanged("Total"); + } + } + + public DownloadViewModel(Gestor.Model.API.Parameters parameters) + { + this.Parameters = parameters; + base.IsEnabled = false; + this.Head = string.Concat("ATUALIZAÇÃO ", this.Parameters.get_Directory().ToUpper()); + } + + public async Task Atualizar() + { + System.Version version; + string str; + FileVersionInfo versionInfo; + str = (this.Parameters.get_Beta() ? "Beta" : "Released"); + string str1 = str; + int type = this.Parameters.get_Type(); + if (type == 7) + { + try + { + Gestor.Model.API.Version version1 = await Gestor.Application.Helpers.Connection.Get(string.Format("Update/{0}/{1}", str1, this.Parameters.get_Type()), true, false); + if (File.Exists("c:\\AggerSeguros\\Agger.Gestor.exe")) + { + versionInfo = FileVersionInfo.GetVersionInfo("c:\\AggerSeguros\\Agger.Gestor.exe"); + } + else + { + versionInfo = null; + } + FileVersionInfo fileVersionInfo = versionInfo; + if (fileVersionInfo == null || !System.Version.TryParse(fileVersionInfo.FileVersion, out version) || !(version >= System.Version.Parse(version1.get_VersaoAplicacao()))) + { + await this.AtualizarGestor(version1, true); + } + else + { + Funcoes.Destroy(); + str1 = null; + return; + } + } + catch (Exception exception) + { + Funcoes.Destroy(); + } + } + else if (type != 88) + { + if (type == 99) + { + Gestor.Model.API.Version version2 = await Gestor.Application.Helpers.Connection.Get(string.Format("Update/Released/{0}", this.Parameters.get_Type()), true, false); + await this.Atualizar(version2); + } + else + { + this.CurrentVersion = await this.GetCurrentVersion(this.Parameters.get_Directory()); + Gestor.Model.API.Version version3 = await Gestor.Application.Helpers.Connection.Get(string.Format("Update/{0}/{1}", str1, this.Parameters.get_Type()), true, false); + if (!this.VerificarVersao(version3)) + { + await this.Atualizar(version3); + } + else + { + this.Open(); + } + } + } + else if (File.Exists("c:\\AggerSeguros\\Aggilizador.Application.exe")) + { + this.Parameters.set_Application("Aggilizador.Application.exe"); + this.Open(); + } + else + { + this.Parameters.set_Arguments(""); + await this.AtualizarAggilizador(); + } + str1 = null; + } + + public async Task Atualizar(Gestor.Model.API.Version version) + { + string str; + string str1; + if (this.CheckFreeSpace()) + { + string str2 = string.Concat("c:\\AggerSeguros\\", version.get_Name()); + if (Directory.Exists(str2)) + { + Directory.Delete(str2, true); + } + while (Directory.Exists(str2)) + { + Thread.Sleep(500); + } + Directory.CreateDirectory(str2); + using (WebClient webClient = new WebClient()) + { + webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(this.DownloadFileComplete); + webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(this.DownloadProgressCallback); + this.CurrentVersion = version.get_Name(); + str = (this.Parameters.get_Type() == 99 ? string.Concat(version.get_Uri(), "/", version.get_Name(), ".exe") : string.Concat(version.get_Uri(), "/", version.get_Name(), ".zip")); + string str3 = str; + str1 = (this.Parameters.get_Type() == 99 ? string.Concat(str2, ".exe") : string.Concat(str2, "//", version.get_Name(), ".zip")); + string str4 = str1; + Stream stream = webClient.OpenRead(str3); + double num = Math.Round((double)((float)long.Parse(webClient.ResponseHeaders["Content-Length"]) / 1024f / 1024f), 2); + this.Total = num.ToString(CultureInfo.InvariantCulture); + if (stream != null) + { + stream.Dispose(); + } + else + { + } + await webClient.DownloadFileTaskAsync(str3, str4); + webClient.Dispose(); + } + webClient = null; + } + else + { + LogError logError = new LogError(); + logError.set_IdFornecedor(ApplicationHelper.IdFornecedor); + logError.set_Fornecedor(Recursos.Empresa.get_Nome()); + logError.set_UsuarioLogado(Recursos.Usuario.get_Nome()); + logError.set_IdUsuarioLogado(Recursos.Usuario.get_Id()); + logError.set_Versao(ApplicationHelper.Versao.ToString()); + logError.set_Data(Funcoes.GetNetworkTime()); + logError.set_IdErro(1001); + logError.set_Erro(string.Concat(Gestor.Common.Validation.ValidationHelper.GetDescription((TipoErro)1001), " - ", version.get_Name())); + logError.set_HResult(0); + logError.set_HelpLink(""); + logError.set_Message(""); + logError.set_Source(""); + logError.set_StackTrace(""); + logError.set_Maquina(Environment.MachineName); + logError.set_UsuarioMaquina(Environment.UserName); + Erro.RegistrarErro(logError, true); + } + } + + public async Task AtualizarAggilizador() + { + if (this.CheckFreeSpace()) + { + using (WebClient webClient = new WebClient()) + { + webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(this.DownloadFileComplete); + webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(this.DownloadProgressCallback); + string str = "https://update.aggilizador.com.br/stable/Instalador.exe"; + string str1 = string.Concat("c:\\AggerSeguros\\", this.Parameters.get_Application()); + Stream stream = webClient.OpenRead(str); + double num = Math.Round((double)((float)long.Parse(webClient.ResponseHeaders["Content-Length"]) / 1024f / 1024f), 2); + this.Total = num.ToString(CultureInfo.InvariantCulture); + if (stream != null) + { + stream.Dispose(); + } + else + { + } + await webClient.DownloadFileTaskAsync(str, str1); + webClient.Dispose(); + } + webClient = null; + } + else + { + LogError logError = new LogError(); + logError.set_IdFornecedor(ApplicationHelper.IdFornecedor); + logError.set_Fornecedor(Recursos.Empresa.get_Nome()); + logError.set_UsuarioLogado(Recursos.Usuario.get_Nome()); + logError.set_IdUsuarioLogado(Recursos.Usuario.get_Id()); + logError.set_Versao(ApplicationHelper.Versao.ToString()); + logError.set_Data(Funcoes.GetNetworkTime()); + logError.set_IdErro(1001); + logError.set_Erro(string.Concat(Gestor.Common.Validation.ValidationHelper.GetDescription((TipoErro)1001), " - AGGILIZADOR")); + logError.set_HResult(0); + logError.set_HelpLink(""); + logError.set_Message(""); + logError.set_Source(""); + logError.set_StackTrace(""); + logError.set_Maquina(Environment.MachineName); + logError.set_UsuarioMaquina(Environment.UserName); + Erro.RegistrarErro(logError, true); + } + } + + public async Task AtualizarGestor(Gestor.Model.API.Version version, bool criarAtalho = false) + { + string str = "c:\\AggerSeguros\\"; + using (WebClient webClient = new WebClient()) + { + webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(this.DownloadFileCompleteGestor); + webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(this.DownloadProgressCallback); + this.CurrentVersion = version.get_Name(); + string str1 = string.Concat(version.get_Uri(), "/", version.get_Name(), ".zip"); + string str2 = string.Concat(str, version.get_Name(), ".zip"); + Stream stream = webClient.OpenRead(str1); + double num = Math.Round((double)((float)long.Parse(webClient.ResponseHeaders["Content-Length"]) / 1024f / 1024f), 2); + this.Total = num.ToString(CultureInfo.InvariantCulture); + if (stream != null) + { + stream.Dispose(); + } + else + { + } + await webClient.DownloadFileTaskAsync(str1, str2); + webClient.Dispose(); + } + webClient = null; + if (criarAtalho) + { + this.CriarAtalho(); + } + } + + private bool CheckFreeSpace() + { + DriveInfo driveInfo = new DriveInfo("C"); + if (!driveInfo.IsReady) + { + return false; + } + return driveInfo.AvailableFreeSpace > (long)314572800; + } + + private void CriarAtalho() + { + WshShell variable = (WshShell)Activator.CreateInstance(Marshal.GetTypeFromCLSID(new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8"))); + string str = "c:\\AggerSeguros\\"; + string str1 = "Agger.Gestor.exe"; + string str2 = "Agger Gestor"; + try + { + IWshShortcut variable1 = variable.CreateShortcut(string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "\\Agger Gestor.lnk")) as IWshShortcut; + if (variable1 != null) + { + variable1.TargetPath = string.Concat(str, str1); + variable1.WindowStyle = 1; + variable1.Description = str2; + variable1.WorkingDirectory = str; + variable1.IconLocation = string.Concat(str, str1, ",0"); + variable1.Save(); + } + } + catch (Exception exception) + { + } + try + { + IWshShortcut variable2 = variable.CreateShortcut(string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.Programs), "\\Agger Gestor.lnk")) as IWshShortcut; + if (variable2 != null) + { + variable2.TargetPath = string.Concat(str, str1); + variable2.WindowStyle = 1; + variable2.Description = str2; + variable2.WorkingDirectory = str; + variable2.IconLocation = string.Concat(str, str1, ",0"); + variable2.Save(); + } + } + catch (Exception exception1) + { + } + try + { + IWshShortcut variable3 = variable.CreateShortcut(string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "\\Agger Gestor.lnk")) as IWshShortcut; + if (variable3 != null) + { + variable3.TargetPath = string.Concat(str, str1); + variable3.WindowStyle = 1; + variable3.Description = str2; + variable3.WorkingDirectory = str; + variable3.IconLocation = string.Concat(str, str1, ",0"); + variable3.Save(); + } + } + catch (Exception exception2) + { + } + try + { + IWshShortcut variable4 = variable.CreateShortcut(string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu), "\\Agger Gestor.lnk")) as IWshShortcut; + if (variable4 != null) + { + variable4.TargetPath = string.Concat(str, str1); + variable4.WindowStyle = 1; + variable4.Description = str2; + variable4.WorkingDirectory = str; + variable4.IconLocation = string.Concat(str, str1, ",0"); + variable4.Save(); + } + } + catch (Exception exception3) + { + } + } + + private void DownloadFileComplete(object sender, AsyncCompletedEventArgs e) + { + this.Processando = true; + this.Porcentagem = decimal.Zero; + this.Extract(); + this.Open(); + } + + private void DownloadFileCompleteGestor(object sender, AsyncCompletedEventArgs e) + { + this.Processando = true; + this.Porcentagem = decimal.Zero; + this.ExtractGestor(); + Funcoes.Destroy(); + } + + private void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e) + { + this.Processando = false; + this.Porcentagem = e.ProgressPercentage; + } + + public void Extract() + { + if (this.Parameters.get_Type() == 99 || this.Parameters.get_Type() == 88) + { + return; + } + string str = string.Concat("c:\\AggerSeguros\\", this.CurrentVersion); + string str1 = string.Concat(str, "\\", this.CurrentVersion, ".zip"); + string.Concat(str, "\\", this.Parameters.get_Application()); + ZipFile.ExtractToDirectory(str1, str); + File.Delete(str1); + } + + public void ExtractGestor() + { + string str = string.Concat("c:\\AggerSeguros\\", this.CurrentVersion, ".zip"); + ZipArchive zipArchive = ZipFile.OpenRead(str); + try + { + foreach (ZipArchiveEntry entry in zipArchive.get_Entries()) + { + string fullPath = Path.GetFullPath(Path.Combine("c:\\AggerSeguros\\", entry.get_FullName())); + fullPath.StartsWith("c:\\AggerSeguros\\", StringComparison.OrdinalIgnoreCase); + if (entry.get_Name() != "") + { + entry.ExtractToFile(fullPath, true); + } + else + { + Directory.CreateDirectory(Path.GetDirectoryName(fullPath)); + } + } + zipArchive.Dispose(); + File.Delete(str); + } + catch (Exception exception) + { + } + } + + private async Task GetCurrentVersion(string name) + { + string str1 = await Task.Run(() => { + string[] directories = Directory.GetDirectories("c:\\AggerSeguros\\"); + List nums = new List(); + string[] strArrays = directories; + for (int i = 0; i < (int)strArrays.Length; i++) + { + string str = strArrays[i]; + if (str.IndexOf(name, StringComparison.Ordinal) > -1) + { + System.Text.RegularExpressions.Match match = (new Regex("(^[A-Za-z]+)(.)([A-Za-z]+)(\\d+)", RegexOptions.IgnoreCase)).Match(str.Replace("c:\\AggerSeguros\\", "")); + if (match.Success) + { + nums.Add(long.Parse(match.Groups[4].Value)); + } + } + } + nums.Reverse(); + if (nums.Count == 0) + { + return ""; + } + return string.Format("{0}{1}", name, nums.First()); + }); + return str1; + } + + public void Open() + { + if (!this.Parameters.get_Run()) + { + Funcoes.Destroy(); + return; + } + string str = Path.Combine((this.Parameters.get_Type() == 99 || this.Parameters.get_Type() == 88 ? "c:\\AggerSeguros\\" : string.Concat("c:\\AggerSeguros\\", this.CurrentVersion)), this.Parameters.get_Application()); + try + { + Process.Start(str, this.Parameters.get_Arguments()); + } + catch + { + } + Funcoes.Destroy(); + } + + private bool VerificarVersao(Gestor.Model.API.Version versao) + { + bool flag = File.Exists(string.Concat("c:\\AggerSeguros\\\\", this.CurrentVersion, "\\", this.Parameters.get_Application())); + return (versao.get_Name() == this.CurrentVersion) & flag; + } + } +} \ No newline at end of file -- cgit v1.2.3