diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 4d6c983..58d4b91 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -22,7 +22,7 @@ "-p", "${workspaceFolder}/File-Watcher.csproj", "set", - "asdf", + "_UserSecretsId", "6516d19d6569" ], "problemMatcher": "$msCompile" diff --git a/Helpers/HelperNuget.cs b/Helpers/HelperNuget.cs index 2ae4754..48b1965 100644 --- a/Helpers/HelperNuget.cs +++ b/Helpers/HelperNuget.cs @@ -26,22 +26,68 @@ internal class HelperNuget return dateTimeOffset; } - private static bool ExtractKeyFileAndSetDateFromZipEntry(ILogger logger, string[] zipFiles, string keyFileExtension, string keyFileExtensionB, string keyFileExtensionC, bool renameToLower) + private static void SetLastWriteTimes(ILogger logger, NugetConfiguration nugetConfiguration, FileInfo fileInfo, DateTimeOffset dateTimeOffset) + { + try + { + string[] files; + if (fileInfo.LastWriteTime != dateTimeOffset.LocalDateTime) + File.SetLastWriteTime(fileInfo.FullName, dateTimeOffset.LocalDateTime); + if (!string.IsNullOrEmpty(nugetConfiguration.KeyFileExtensionB)) + { + if (fileInfo.DirectoryName is null) + throw new NullReferenceException(nameof(fileInfo.DirectoryName)); + files = Directory.GetFiles(fileInfo.DirectoryName, nugetConfiguration.KeyFileExtensionB, SearchOption.TopDirectoryOnly); + foreach (string file in files) + { + fileInfo = new(file); + if (fileInfo.LastWriteTime != dateTimeOffset.LocalDateTime) + File.SetLastWriteTime(fileInfo.FullName, dateTimeOffset.LocalDateTime); + } + } + if (!string.IsNullOrEmpty(nugetConfiguration.KeyFileExtensionC)) + { + if (fileInfo.DirectoryName is null) + throw new NullReferenceException(nameof(fileInfo.DirectoryName)); + files = Directory.GetFiles(fileInfo.DirectoryName, nugetConfiguration.KeyFileExtensionC, SearchOption.TopDirectoryOnly); + foreach (string file in files) + { + fileInfo = new(file); + if (fileInfo.LastWriteTime != dateTimeOffset.LocalDateTime) + File.SetLastWriteTime(fileInfo.FullName, dateTimeOffset.LocalDateTime); + } + } + } + catch (Exception) + { + string checkFile; + logger.LogInformation("<{fileInfo.FullName}> is invalid!", fileInfo.FullName); + checkFile = string.Concat(fileInfo.FullName, ".err"); + for (int e = 0; e < short.MaxValue; e++) + { + if (!File.Exists(checkFile)) + break; + checkFile = string.Concat(checkFile, e); + } + try + { File.Move(fileInfo.FullName, checkFile); } + catch (Exception) { logger.LogInformation("<{fileInfo.FullName}> couldn't be moved!", fileInfo.FullName); } + } + } + + private static void ExtractKeyFileAndSetDateFromZipEntry(ILogger logger, NugetConfiguration nugetConfiguration, string[] zipFiles) { - bool result = false; string[] files; - string checkFile; - string? lowerName; FileInfo fileInfo; + string? lowerName; 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) + lowerName = !nugetConfiguration.RenameToLower ? null : Path.Combine(fileInfo.DirectoryName, fileInfo.Name.ToLower()); + if (nugetConfiguration.RenameToLower && lowerName is not null && lowerName != fileInfo.FullName) { files = Directory.GetFiles(fileInfo.DirectoryName, $"{Path.GetFileNameWithoutExtension(fileInfo.Name)}*", SearchOption.TopDirectoryOnly); foreach (string file in files) @@ -50,69 +96,18 @@ internal class HelperNuget if (fileInfo.DirectoryName is null) throw new NullReferenceException(nameof(fileInfo.DirectoryName)); } - extractKeyFileInfo = new(Path.Combine(fileInfo.DirectoryName, $"{Path.GetFileNameWithoutExtension(fileInfo.Name)}{keyFileExtension}")); + extractKeyFileInfo = new(Path.Combine(fileInfo.DirectoryName, $"{Path.GetFileNameWithoutExtension(fileInfo.Name)}{nugetConfiguration.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); } - } + DateTimeOffset? dateTimeOffset = GetDateTimeOffset(nugetConfiguration.KeyFileExtension, fileInfo, extractKeyFileInfo); + if (dateTimeOffset is null) + continue; + SetLastWriteTimes(logger, nugetConfiguration, fileInfo, dateTimeOffset.Value); } - return result; } private static int CopyFiles(NugetConfiguration nugetConfiguration, string[] files) @@ -129,6 +124,12 @@ internal class HelperNuget File.Copy(fileInfo.FullName, checkFileInfo.FullName, overwrite: true); results++; } + if (string.IsNullOrEmpty(nugetConfiguration.KeyFileExtension)) + continue; + fileInfo = new(file); + checkFileInfo = new(Path.Combine(nugetConfiguration.Destination, Path.ChangeExtension(fileInfo.Name, nugetConfiguration.KeyFileExtension))); + if (!checkFileInfo.Exists || fileInfo.LastWriteTime != checkFileInfo.LastWriteTime || fileInfo.Length != checkFileInfo.Length) + File.Copy(fileInfo.FullName, checkFileInfo.FullName, overwrite: true); } return results; } @@ -142,7 +143,7 @@ internal class HelperNuget _ = 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); + ExtractKeyFileAndSetDateFromZipEntry(logger, nugetConfiguration, files); logger.LogInformation("{Files} file(s) verified", files.Length); int filesCopied = CopyFiles(nugetConfiguration, files); logger.LogInformation("Copied {FilesCopied} file(s)", filesCopied); diff --git a/Models/RijndaelEncryption.cs b/Helpers/RijndaelEncryption.cs similarity index 93% rename from Models/RijndaelEncryption.cs rename to Helpers/RijndaelEncryption.cs index 113bfef..e28b6c9 100644 --- a/Models/RijndaelEncryption.cs +++ b/Helpers/RijndaelEncryption.cs @@ -2,10 +2,10 @@ using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; -namespace File_Watcher.Models; +namespace File_Watcher.Helpers; public static class RijndaelEncryption -{ +{ // cSpell:disable /// /// Change the input key GUID when you use this code in your own program. @@ -25,7 +25,7 @@ public static class RijndaelEncryption string result; if (string.IsNullOrEmpty(text)) throw new ArgumentNullException(nameof(text)); -#pragma warning disable +#pragma warning disable SYSLIB0022 RijndaelManaged aesAlg = NewRijndaelManaged(salt); #pragma warning restore ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); @@ -46,7 +46,7 @@ public static class RijndaelEncryption { bool result; base64String = base64String.Trim(); -#pragma warning disable +#pragma warning restore result = (base64String.Length % 4 == 0) && Regex.IsMatch(base64String, @"^[a-zA-Z0-9\+/]*={0,3}$", RegexOptions.None); #pragma warning restore return result; @@ -65,7 +65,7 @@ public static class RijndaelEncryption if (!IsBase64String(cipherText)) throw new Exception("The cipherText input parameter is not base64 encoded"); string text; -#pragma warning disable +#pragma warning disable SYSLIB0022 RijndaelManaged aesAlg = NewRijndaelManaged(salt); #pragma warning restore ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); @@ -84,11 +84,10 @@ public static class RijndaelEncryption /// /// The password salt /// -#pragma warning disable +#pragma warning disable SYSLIB0022, SYSLIB0041, CA5379 private static RijndaelManaged NewRijndaelManaged(string salt) { - if (salt == null) - throw new ArgumentNullException(nameof(salt)); + ArgumentNullException.ThrowIfNull(salt); byte[] saltBytes = Encoding.ASCII.GetBytes(salt); Rfc2898DeriveBytes key = new(_InputKey, saltBytes); RijndaelManaged aesAlg = new(); diff --git a/Worker.cs b/Worker.cs index 45eda4f..e947f33 100644 --- a/Worker.cs +++ b/Worker.cs @@ -52,7 +52,7 @@ public partial class Worker : BackgroundService private async Task Body(CancellationToken cancellationToken) { if (!_IsWindowsService) - throw new EvaluateException("Set break point and skip!"); + throw new EvaluateException($"Set break point and skip to run {_AppSettings.Helper}!"); if (!_IsWindowsService) { for (int i = 0; i < int.MaxValue; i++)