From 1f4e14b2e973ee7de337fd4866d9a5ceff5cb6d1 Mon Sep 17 00:00:00 2001 From: Lucas Faria Mendes Date: Mon, 30 Mar 2026 10:38:18 -0300 Subject: chore: location --- .../ConfigurationFileCache.cs | 133 +++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Configuration/ConfigurationFileCache.cs (limited to 'Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Configuration/ConfigurationFileCache.cs') diff --git a/Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Configuration/ConfigurationFileCache.cs b/Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Configuration/ConfigurationFileCache.cs new file mode 100644 index 0000000..4864edc --- /dev/null +++ b/Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Configuration/ConfigurationFileCache.cs @@ -0,0 +1,133 @@ +using Gestor.Common.Helpers; +using NHibernate.Cfg; +using System; +using System.ComponentModel; +using System.Data.SqlClient; +using System.IO; +using System.Reflection; +using System.Runtime.Serialization.Formatters.Binary; + +namespace Gestor.Infrastructure.Configuration +{ + public class ConfigurationFileCache + { + private readonly string _cacheFile; + + private readonly Assembly _definitionsAssembly; + + private bool IsConfigurationFileValid + { + get + { + string location; + if (!File.Exists(this._cacheFile)) + { + return false; + } + FileInfo fileInfo = new FileInfo(this._cacheFile); + Assembly assembly = this._definitionsAssembly; + if (assembly != null) + { + location = assembly.Location; + } + else + { + location = null; + } + string str = location; + if (str == null) + { + return false; + } + FileInfo fileInfo1 = new FileInfo(str); + if (fileInfo.Length < (long)5120) + { + return false; + } + return fileInfo.LastWriteTime >= fileInfo1.LastWriteTime; + } + } + + public ConfigurationFileCache(string connectionString) + { + if (connectionString == null) + { + return; + } + this._definitionsAssembly = Assembly.GetExecutingAssembly(); + SqlConnection sqlConnection = new SqlConnection(connectionString); + string str = string.Concat("Data_", sqlConnection.Database, ".cfg"); + sqlConnection.Dispose(); + this._cacheFile = string.Concat(AppDomain.CurrentDomain.BaseDirectory, str); + } + + public void DeleteCacheFile() + { + try + { + if (File.Exists(this._cacheFile)) + { + File.Delete(this._cacheFile); + } + } + catch (Exception exception) + { + } + } + + public NHibernate.Cfg.Configuration LoadConfigurationFromFile() + { + NHibernate.Cfg.Configuration configuration; + if (!this.IsConfigurationFileValid) + { + return null; + } + try + { + using (FileStream fileStream = File.Open(this._cacheFile, FileMode.Open, FileAccess.Read)) + { + using (MemoryStream memoryStream = new MemoryStream(ConfigurationFileCache.ReadFully(fileStream).DecryptBytes())) + { + configuration = (new BinaryFormatter()).Deserialize(memoryStream) as NHibernate.Cfg.Configuration; + } + } + } + catch (Exception exception) + { + this.DeleteCacheFile(); + configuration = null; + } + return configuration; + } + + private static byte[] ReadFully(Stream inputStream) + { + byte[] array; + using (MemoryStream memoryStream = new MemoryStream()) + { + inputStream.CopyTo(memoryStream); + array = memoryStream.ToArray(); + } + return array; + } + + public void SaveConfigurationToFile(NHibernate.Cfg.Configuration configuration) + { + try + { + using (FileStream fileStream = File.Open(this._cacheFile, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)) + { + using (MemoryStream memoryStream = new MemoryStream()) + { + (new BinaryFormatter()).Serialize(memoryStream, configuration); + byte[] numArray = memoryStream.ToArray().EncryptBytes(); + fileStream.Write(numArray, 0, (int)numArray.Length); + } + } + } + catch (Exception exception) + { + } + } + } +} \ No newline at end of file -- cgit v1.2.3