HelperEDADatabase
HelperEAFProgramData HelperCompass HelperInfinityQS HelperSerial HelperTCP dotnet_analyzer_diagnostic
This commit is contained in:
118
Helpers/HelperCompass.cs
Normal file
118
Helpers/HelperCompass.cs
Normal file
@ -0,0 +1,118 @@
|
||||
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<Worker> 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<string> GetFiles(AppSettings appSettings, string directory)
|
||||
{
|
||||
ReadOnlyCollection<string> 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<string> files)
|
||||
{
|
||||
string checkFile;
|
||||
foreach (string file in files)
|
||||
{
|
||||
checkFile = Path.Combine(appSettings.CompassConfiguration.Destination, Path.GetFileName(file));
|
||||
if (File.Exists(checkFile))
|
||||
continue;
|
||||
File.Copy(file, checkFile);
|
||||
}
|
||||
}
|
||||
|
||||
internal static bool CopyFile(AppSettings appSettings, ILogger<Worker> logger)
|
||||
{
|
||||
if (_FirstRun)
|
||||
{
|
||||
_FirstRun = false;
|
||||
MapDrives(appSettings, logger);
|
||||
logger.LogCritical("MapDrives Done");
|
||||
}
|
||||
string directory;
|
||||
ReadOnlyCollection<string> 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;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user