HelperNuget
This commit is contained in:
@ -223,6 +223,7 @@ public class DCP
|
||||
level += 1;
|
||||
foreach (dynamic entry in expandoObject)
|
||||
{
|
||||
#pragma warning disable IL2026, IL3050
|
||||
if (entry.Value is ExpandoObject)
|
||||
TextResolveEntry(result, entry.Value, level, string.Concat(super, " : ", entry.Key), i, group);
|
||||
else
|
||||
@ -262,6 +263,7 @@ public class DCP
|
||||
i = null;
|
||||
}
|
||||
}
|
||||
#pragma warning restore IL2026, IL3050
|
||||
}
|
||||
level -= 1;
|
||||
if (level == 0)
|
||||
@ -273,6 +275,7 @@ public class DCP
|
||||
StringBuilder result = new();
|
||||
if (result.Length > 0) //Skipping because System.Text.Json changed the way Expando works
|
||||
{
|
||||
#pragma warning disable IL2026, IL3050
|
||||
string title = string.Concat(Path.GetFileName(edaObjectFile), " - ", common.Source);
|
||||
dynamic? expandoObject = JsonSerializer.Deserialize<ExpandoObject>(json);
|
||||
_ = result.AppendLine(title);
|
||||
@ -288,6 +291,7 @@ public class DCP
|
||||
_ = result.AppendLine(edaObjectFile);
|
||||
_ = result.AppendLine();
|
||||
TextResolveEntry(result, expandoObject, 0, string.Empty, null, group: true);
|
||||
#pragma warning restore IL2026, IL3050
|
||||
}
|
||||
return result.ToString();
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ using System.Text;
|
||||
|
||||
namespace File_Watcher.Helpers;
|
||||
|
||||
internal static partial class HelperCompass
|
||||
internal class HelperCompass
|
||||
{
|
||||
|
||||
private static bool _FirstRun = true;
|
||||
@ -114,7 +114,7 @@ internal static partial class HelperCompass
|
||||
if (!Directory.Exists(directory))
|
||||
_ = Directory.CreateDirectory(directory);
|
||||
files = GetFiles(appSettings, directory);
|
||||
logger.LogDebug("Found {Files}", files);
|
||||
logger.LogDebug("Found {Files} file(s)", files.Count);
|
||||
CopyFiles(appSettings, files);
|
||||
}
|
||||
return true;
|
||||
|
@ -35,8 +35,10 @@ internal static partial class HelperEDADatabase
|
||||
{
|
||||
Stream stream = ToStream(@this.Trim());
|
||||
XmlReader? reader = XmlReader.Create(stream, new XmlReaderSettings() { ConformanceLevel = ConformanceLevel.Document });
|
||||
#pragma warning disable IL2026, IL2090
|
||||
XmlSerializer xmlSerializer = new(typeof(T), typeof(T).GetNestedTypes());
|
||||
result = xmlSerializer.Deserialize(reader);
|
||||
#pragma warning restore IL2026, IL2090
|
||||
stream.Dispose();
|
||||
}
|
||||
catch (Exception)
|
||||
@ -131,7 +133,9 @@ internal static partial class HelperEDADatabase
|
||||
continue;
|
||||
// cSpell:enable
|
||||
common.Update(configuration);
|
||||
#pragma warning disable IL2026, IL3050
|
||||
json = JsonSerializer.Serialize(configuration, configuration.GetType(), jsonSerializerOptions);
|
||||
#pragma warning restore IL2026, IL3050
|
||||
if (common?.UnitName is null)
|
||||
continue;
|
||||
fileName = string.Concat(edaObjectFile.Replace(replace, "Partial"), ".csv");
|
||||
@ -211,7 +215,9 @@ internal static partial class HelperEDADatabase
|
||||
List<object> row;
|
||||
StringBuilder sql = new();
|
||||
string objectTypeDirectory;
|
||||
#pragma warning disable IL3050
|
||||
Array objectTypes = Enum.GetValues(typeof(ModuleInstanceTypeName));
|
||||
#pragma warning restore IL3050
|
||||
Dictionary<string, Dictionary<ModuleInstanceTypeName, List<List<object>>>> rows = [];
|
||||
string decrypted = RijndaelEncryption.Decrypt(appSettings.EDADatabaseConfiguration.Password, appSettings.Company);
|
||||
string connectionString = $"Data Source={appSettings.EDADatabaseConfiguration.TNS}; User Id={appSettings.EDADatabaseConfiguration.UserName}; Password={decrypted};";
|
||||
|
152
Helpers/HelperNuget.cs
Normal file
152
Helpers/HelperNuget.cs
Normal file
@ -0,0 +1,152 @@
|
||||
using File_Watcher.Models;
|
||||
using System.IO.Compression;
|
||||
|
||||
namespace File_Watcher.Helpers;
|
||||
|
||||
internal class HelperNuget
|
||||
{
|
||||
|
||||
private static DateTimeOffset? GetDateTimeOffset(string keyFileExtension, FileInfo fileInfo, FileInfo extractKeyFileInfo)
|
||||
{
|
||||
DateTimeOffset? dateTimeOffset = null;
|
||||
using ZipArchive zip = ZipFile.Open(fileInfo.FullName, ZipArchiveMode.Read);
|
||||
foreach (ZipArchiveEntry zipArchiveEntry in zip.Entries)
|
||||
{
|
||||
if (!zipArchiveEntry.Name.EndsWith(keyFileExtension))
|
||||
continue;
|
||||
dateTimeOffset = zipArchiveEntry.LastWriteTime;
|
||||
if (fileInfo.FullName[0] != '\\')
|
||||
{
|
||||
zipArchiveEntry.ExtractToFile(extractKeyFileInfo.FullName);
|
||||
File.SetCreationTime(extractKeyFileInfo.FullName, fileInfo.CreationTime);
|
||||
File.SetLastWriteTime(extractKeyFileInfo.FullName, dateTimeOffset.Value.LocalDateTime);
|
||||
}
|
||||
break;
|
||||
}
|
||||
return dateTimeOffset;
|
||||
}
|
||||
|
||||
private static bool ExtractKeyFileAndSetDateFromZipEntry(ILogger<Worker> logger, string[] zipFiles, string keyFileExtension, string keyFileExtensionB, string keyFileExtensionC, bool renameToLower)
|
||||
{
|
||||
bool result = false;
|
||||
string[] files;
|
||||
string checkFile;
|
||||
string? lowerName;
|
||||
FileInfo fileInfo;
|
||||
FileInfo extractKeyFileInfo;
|
||||
DateTimeOffset? dateTimeOffset;
|
||||
foreach (string zipFile in zipFiles)
|
||||
{
|
||||
fileInfo = new(zipFile);
|
||||
if (fileInfo.DirectoryName is null)
|
||||
throw new NullReferenceException(nameof(fileInfo.DirectoryName));
|
||||
lowerName = !renameToLower ? null : Path.Combine(fileInfo.DirectoryName, fileInfo.Name.ToLower());
|
||||
if (renameToLower && lowerName is not null && lowerName != fileInfo.FullName)
|
||||
{
|
||||
files = Directory.GetFiles(fileInfo.DirectoryName, $"{Path.GetFileNameWithoutExtension(fileInfo.Name)}*", SearchOption.TopDirectoryOnly);
|
||||
foreach (string file in files)
|
||||
File.Move(file, Path.Combine(fileInfo.DirectoryName, Path.GetFileName(file).ToLower()));
|
||||
fileInfo = new(lowerName);
|
||||
if (fileInfo.DirectoryName is null)
|
||||
throw new NullReferenceException(nameof(fileInfo.DirectoryName));
|
||||
}
|
||||
extractKeyFileInfo = new(Path.Combine(fileInfo.DirectoryName, $"{Path.GetFileNameWithoutExtension(fileInfo.Name)}{keyFileExtension}"));
|
||||
if (extractKeyFileInfo.Exists)
|
||||
{
|
||||
if (extractKeyFileInfo.CreationTime.ToString("yyyy-MM-dd") == fileInfo.CreationTime.ToString("yyyy-MM-dd") && extractKeyFileInfo.LastWriteTime.ToString("yyyy-MM-dd") == fileInfo.LastWriteTime.ToString("yyyy-MM-dd"))
|
||||
continue;
|
||||
File.Delete(extractKeyFileInfo.FullName);
|
||||
}
|
||||
try
|
||||
{
|
||||
dateTimeOffset = GetDateTimeOffset(keyFileExtension, fileInfo, extractKeyFileInfo);
|
||||
if (dateTimeOffset is null)
|
||||
continue;
|
||||
if (fileInfo.LastWriteTime != dateTimeOffset.Value.LocalDateTime)
|
||||
{
|
||||
File.SetLastWriteTime(fileInfo.FullName, dateTimeOffset.Value.LocalDateTime);
|
||||
if (!result)
|
||||
result = true;
|
||||
}
|
||||
if (string.IsNullOrEmpty(keyFileExtensionB))
|
||||
continue;
|
||||
files = Directory.GetFiles(fileInfo.DirectoryName, keyFileExtensionB, SearchOption.TopDirectoryOnly);
|
||||
foreach (string file in files)
|
||||
{
|
||||
fileInfo = new(file);
|
||||
if (fileInfo.LastWriteTime != dateTimeOffset.Value.LocalDateTime)
|
||||
{
|
||||
File.SetLastWriteTime(fileInfo.FullName, dateTimeOffset.Value.LocalDateTime);
|
||||
if (!result)
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
if (string.IsNullOrEmpty(keyFileExtensionC))
|
||||
continue;
|
||||
if (fileInfo.DirectoryName is null)
|
||||
throw new NullReferenceException(nameof(fileInfo.DirectoryName));
|
||||
files = Directory.GetFiles(fileInfo.DirectoryName, keyFileExtensionC, SearchOption.TopDirectoryOnly);
|
||||
foreach (string file in files)
|
||||
{
|
||||
fileInfo = new(file);
|
||||
if (fileInfo.LastWriteTime != dateTimeOffset.Value.LocalDateTime)
|
||||
{
|
||||
File.SetLastWriteTime(fileInfo.FullName, dateTimeOffset.Value.LocalDateTime);
|
||||
if (!result)
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
logger.LogInformation("<{zipFile}> is invalid!", zipFile);
|
||||
checkFile = string.Concat(zipFile, ".err");
|
||||
for (int e = 0; e < short.MaxValue; e++)
|
||||
{
|
||||
if (!File.Exists(checkFile))
|
||||
break;
|
||||
checkFile = string.Concat(checkFile, e);
|
||||
}
|
||||
try
|
||||
{ File.Move(zipFile, checkFile); }
|
||||
catch (Exception) { logger.LogInformation("<{zipFile}> couldn't be moved!", zipFile); }
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static int CopyFiles(NugetConfiguration nugetConfiguration, string[] files)
|
||||
{
|
||||
int results = 0;
|
||||
FileInfo fileInfo;
|
||||
FileInfo checkFileInfo;
|
||||
foreach (string file in files)
|
||||
{
|
||||
fileInfo = new(file);
|
||||
checkFileInfo = new(Path.Combine(nugetConfiguration.Destination, fileInfo.Name));
|
||||
if (!checkFileInfo.Exists || fileInfo.LastWriteTime != checkFileInfo.LastWriteTime || fileInfo.Length != checkFileInfo.Length)
|
||||
{
|
||||
File.Copy(fileInfo.FullName, checkFileInfo.FullName, overwrite: true);
|
||||
results++;
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
internal static bool Sync(AppSettings appSettings, ILogger<Worker> logger)
|
||||
{
|
||||
NugetConfiguration nugetConfiguration = appSettings.NugetConfiguration;
|
||||
if (!Directory.Exists(nugetConfiguration.Source))
|
||||
_ = Directory.CreateDirectory(nugetConfiguration.Source);
|
||||
if (!Directory.Exists(nugetConfiguration.Destination))
|
||||
_ = Directory.CreateDirectory(nugetConfiguration.Destination);
|
||||
string[] files = Directory.GetFiles(nugetConfiguration.Source, nugetConfiguration.SearchPattern, SearchOption.AllDirectories);
|
||||
logger.LogInformation("Found {Files} file(s)", files.Length);
|
||||
_ = ExtractKeyFileAndSetDateFromZipEntry(logger, files, nugetConfiguration.KeyFileExtension, nugetConfiguration.KeyFileExtensionB, nugetConfiguration.KeyFileExtensionC, nugetConfiguration.RenameToLower);
|
||||
logger.LogInformation("{Files} file(s) verified", files.Length);
|
||||
int filesCopied = CopyFiles(nugetConfiguration, files);
|
||||
logger.LogInformation("Copied {FilesCopied} file(s)", filesCopied);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user