using File_Watcher.Models; using System.Collections.ObjectModel; using System.Diagnostics; using System.Text; namespace File_Watcher.Helpers; internal static partial class HelperCompass { private static bool _FirstRun = true; private static void MapDrives(AppSettings appSettings, ILogger logger) { Process? process; string arguments; string decrypted; string? pathRoot; string[] segments; string standardError; string standardOutput; string fileName = "net"; StringBuilder stringBuilder = new(); if (appSettings.DriveConfiguration.Use is not null && appSettings.DriveConfiguration.Use.Value) { pathRoot = Path.GetPathRoot(appSettings.DriveConfiguration.Share); if (!string.IsNullOrEmpty(pathRoot) && !Directory.Exists(pathRoot)) { if (string.IsNullOrEmpty(appSettings.DriveConfiguration.Password)) decrypted = string.Empty; else decrypted = RijndaelEncryption.Decrypt(appSettings.DriveConfiguration.Password, appSettings.Company); arguments = $"use {appSettings.DriveConfiguration.Letter}: \"{appSettings.DriveConfiguration.Share}\" /p:yes /user:{appSettings.DriveConfiguration.User} {decrypted}"; _ = stringBuilder.Clear(); segments = arguments.Split(' '); for (int j = 0; j < segments.Length - 1; j++) _ = stringBuilder.Append(segments[j]).Append(' '); logger.LogInformation("// {output}", stringBuilder); ProcessStartInfo processStartInfo = new(fileName, arguments) { RedirectStandardError = true, RedirectStandardOutput = true, UseShellExecute = false }; try { process = Process.Start(processStartInfo); if (process is not null) { for (int j = 1; j < 45; j++) { _ = process.WaitForExit(1000); if (process.HasExited) break; } if (!process.HasExited) logger.LogError("// Never exited!"); else { standardError = process.StandardError.ReadToEnd(); standardOutput = process.StandardOutput.ReadToEnd(); logger.LogInformation("// {error}{line}{line}// {output}", standardError, Environment.NewLine, Environment.NewLine, standardOutput); } } } catch (Exception ex) { logger.LogError(ex, "Error:"); } } } } private static ReadOnlyCollection GetFiles(AppSettings appSettings, string directory) { ReadOnlyCollection results; if (!Directory.Exists(directory)) _ = Directory.CreateDirectory(directory); results = new(Directory.GetFiles(directory, appSettings.CompassConfiguration.Pattern, SearchOption.TopDirectoryOnly)); return results; } private static void CopyFiles(AppSettings appSettings, ReadOnlyCollection files) { string checkFile; string triggerFile; foreach (string file in files) { checkFile = Path.Combine(appSettings.CompassConfiguration.Destination, Path.GetFileName(file)); triggerFile = $"{checkFile}{appSettings.CompassConfiguration.TriggerAppendage}"; if (File.Exists(checkFile)) continue; if (File.Exists(triggerFile)) continue; File.Copy(file, checkFile); File.WriteAllText(triggerFile, string.Empty); } } internal static bool CopyFile(AppSettings appSettings, ILogger logger) { if (_FirstRun) { _FirstRun = false; MapDrives(appSettings, logger); logger.LogCritical("MapDrives Done"); } string directory; ReadOnlyCollection files; DateTime[] dateTimes = [DateTime.Now, DateTime.Now.AddHours(-appSettings.CompassConfiguration.HoursBack)]; foreach (DateTime dateTime in dateTimes) { directory = Path.Combine(appSettings.CompassConfiguration.Source, dateTime.ToString(appSettings.CompassConfiguration.YearPattern), dateTime.ToString(appSettings.CompassConfiguration.MonthPattern)); if (!Directory.Exists(directory)) _ = Directory.CreateDirectory(directory); files = GetFiles(appSettings, directory); logger.LogDebug("Found {Files}", files); CopyFiles(appSettings, files); } return true; } }