Download SSL Certificates Sort Subtasks of Markdown files Test BioRad EAF CopyDirectories json to Markdown Sort Day 2024 Q2 GitRemoteRemove Handle directoryInfo.LinkTarget better Remove StartAt Handle directoryInfo.LinkTarget
206 lines
9.6 KiB
C#
206 lines
9.6 KiB
C#
using File_Folder_Helper.Models;
|
|
using Microsoft.Extensions.Logging;
|
|
using System.Collections.ObjectModel;
|
|
using System.Diagnostics;
|
|
using System.Globalization;
|
|
using System.Text.Json;
|
|
|
|
namespace File_Folder_Helper.Day;
|
|
|
|
internal static partial class Helper20240724
|
|
{
|
|
|
|
private record FileConnectorConfigurationSystem(string AlternateTargetFolder,
|
|
string FileAgeThreshold,
|
|
string[] SourceFileFilters,
|
|
string TargetFileLocation);
|
|
|
|
#pragma warning disable IDE0028, IDE0056, IDE0300, IDE0240, IDE0241
|
|
|
|
private static readonly HttpClient _HttpClient = new();
|
|
private static readonly string _StaticFileServer = "localhost:5054";
|
|
private static readonly FileConnectorConfigurationSystem _FileConnectorConfiguration = new(
|
|
"D:/Tmp/Phares/AlternateTargetFolder",
|
|
"000:20:00:01",
|
|
[".txt"],
|
|
"D:/Tmp/Phares/TargetFileLocation");
|
|
|
|
private static DateTime GetFileAgeThresholdDateTime(string fileAgeThreshold)
|
|
{
|
|
DateTime result = DateTime.Now;
|
|
string[] segments = fileAgeThreshold.Split(':');
|
|
for (int i = 0; i < segments.Length; i++)
|
|
{
|
|
result = i switch
|
|
{
|
|
0 => result.AddDays(double.Parse(segments[i]) * -1),
|
|
1 => result.AddHours(double.Parse(segments[i]) * -1),
|
|
2 => result.AddMinutes(double.Parse(segments[i]) * -1),
|
|
3 => result.AddSeconds(double.Parse(segments[i]) * -1),
|
|
_ => throw new Exception(),
|
|
};
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private static string[] GetValidWeeks(DateTime fileAgeThresholdDateTime)
|
|
{
|
|
DateTime dateTime = DateTime.Now;
|
|
Calendar calendar = new CultureInfo("en-US").Calendar;
|
|
string weekOfYear = $"{dateTime:yyyy}_Week_{calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday):00}";
|
|
string lastWeekOfYear = $"{fileAgeThresholdDateTime:yyyy}_Week_{calendar.GetWeekOfYear(fileAgeThresholdDateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday):00}";
|
|
return new string[] { weekOfYear, lastWeekOfYear }.Distinct().ToArray();
|
|
}
|
|
|
|
private static string[] GetValidDays(DateTime fileAgeThresholdDateTime)
|
|
{
|
|
DateTime dateTime = DateTime.Now;
|
|
return new string[] { dateTime.ToString("yyyy-MM-dd"), fileAgeThresholdDateTime.ToString("yyyy-MM-dd") }.Distinct().ToArray();
|
|
}
|
|
|
|
private static ReadOnlyCollection<NginxFileSystem> GetDayNginxFileSystemCollection(DateTime fileAgeThresholdDateTime, string week, string day, string dayUrl, NginxFileSystem[] dayNginxFileSystemCollection)
|
|
{
|
|
List<NginxFileSystem> results = new();
|
|
DateTime dateTime;
|
|
string nginxFormat = "ddd, dd MMM yyyy HH:mm:ss zzz";
|
|
foreach (NginxFileSystem dayNginxFileSystem in dayNginxFileSystemCollection)
|
|
{
|
|
if (!DateTime.TryParseExact(dayNginxFileSystem.MTime.Replace("GMT", "+00:00"), nginxFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime))
|
|
continue;
|
|
if (dateTime < fileAgeThresholdDateTime)
|
|
continue;
|
|
results.Add(new(
|
|
Path.GetFullPath(Path.Combine(_FileConnectorConfiguration.TargetFileLocation, week, day, dayNginxFileSystem.Name)),
|
|
string.Concat(dayUrl, '/', dayNginxFileSystem.Name),
|
|
dateTime.ToString(),
|
|
dayNginxFileSystem.Size));
|
|
}
|
|
return results.AsReadOnly();
|
|
}
|
|
|
|
private static ReadOnlyCollection<NginxFileSystem> GetDayNginxFileSystemCollection(DateTime fileAgeThresholdDateTime)
|
|
{
|
|
#nullable enable
|
|
List<NginxFileSystem> results = new();
|
|
string dayUrl;
|
|
string dayJson;
|
|
string weekJson;
|
|
string checkWeek;
|
|
Task<HttpResponseMessage> task;
|
|
NginxFileSystem[]? dayNginxFileSystemCollection;
|
|
NginxFileSystem[]? weekNginxFileSystemCollection;
|
|
string[] days = GetValidDays(fileAgeThresholdDateTime);
|
|
string[] weeks = GetValidWeeks(fileAgeThresholdDateTime);
|
|
foreach (string week in weeks)
|
|
{
|
|
checkWeek = string.Concat("http://", _StaticFileServer, '/', week);
|
|
task = _HttpClient.GetAsync(checkWeek);
|
|
task.Wait();
|
|
if (!task.Result.IsSuccessStatusCode)
|
|
continue;
|
|
weekJson = _HttpClient.GetStringAsync(checkWeek).Result;
|
|
weekNginxFileSystemCollection = JsonSerializer.Deserialize(weekJson, NginxFileSystemCollectionSourceGenerationContext.Default.NginxFileSystemArray);
|
|
if (weekNginxFileSystemCollection is null)
|
|
continue;
|
|
foreach (NginxFileSystem weekNginxFileSystem in weekNginxFileSystemCollection)
|
|
{
|
|
if (!(from l in days where weekNginxFileSystem.Name == l select false).Any())
|
|
continue;
|
|
dayUrl = string.Concat(checkWeek, '/', weekNginxFileSystem.Name);
|
|
dayJson = _HttpClient.GetStringAsync(dayUrl).Result;
|
|
dayNginxFileSystemCollection = JsonSerializer.Deserialize(dayJson, NginxFileSystemCollectionSourceGenerationContext.Default.NginxFileSystemArray);
|
|
if (dayNginxFileSystemCollection is null)
|
|
continue;
|
|
results.AddRange(GetDayNginxFileSystemCollection(fileAgeThresholdDateTime, week, weekNginxFileSystem.Name, dayUrl, dayNginxFileSystemCollection));
|
|
}
|
|
}
|
|
return results.AsReadOnly();
|
|
#nullable disable
|
|
}
|
|
|
|
private static ReadOnlyCollection<Tuple<DateTime, FileInfo, FileInfo, string>> GetPossible()
|
|
{
|
|
List<Tuple<DateTime, FileInfo, FileInfo, string>> results = new();
|
|
DateTime dateTime;
|
|
FileInfo targetFileInfo;
|
|
FileInfo alternateFileInfo;
|
|
DateTime fileAgeThresholdDateTime = GetFileAgeThresholdDateTime(_FileConnectorConfiguration.FileAgeThreshold);
|
|
ReadOnlyCollection<NginxFileSystem> dayNginxFileSystemCollection = GetDayNginxFileSystemCollection(fileAgeThresholdDateTime);
|
|
foreach (NginxFileSystem nginxFileSystem in dayNginxFileSystemCollection)
|
|
{
|
|
targetFileInfo = new FileInfo(nginxFileSystem.Name);
|
|
if (targetFileInfo.Directory is null)
|
|
continue;
|
|
if (!Directory.Exists(targetFileInfo.Directory.FullName))
|
|
_ = Directory.CreateDirectory(targetFileInfo.Directory.FullName);
|
|
if (!DateTime.TryParse(nginxFileSystem.MTime, out dateTime))
|
|
continue;
|
|
if (targetFileInfo.Exists && targetFileInfo.LastWriteTime == dateTime)
|
|
continue;
|
|
alternateFileInfo = new(Path.Combine(_FileConnectorConfiguration.AlternateTargetFolder, nginxFileSystem.Name));
|
|
results.Add(new(dateTime, targetFileInfo, alternateFileInfo, nginxFileSystem.Type));
|
|
}
|
|
return (from l in results orderby l.Item1 select l).ToList().AsReadOnly();
|
|
}
|
|
|
|
private static void Test()
|
|
{
|
|
#nullable enable
|
|
if (_HttpClient is null)
|
|
throw new Exception();
|
|
if (string.IsNullOrEmpty(_StaticFileServer))
|
|
throw new Exception();
|
|
if (string.IsNullOrEmpty(_StaticFileServer))
|
|
{
|
|
ReadOnlyCollection<Tuple<DateTime, FileInfo, FileInfo, string>> possibleDownload = GetPossible();
|
|
if (possibleDownload.Count > 0)
|
|
{
|
|
string targetFileName = possibleDownload[0].Item4;
|
|
FileInfo targetFileInfo = possibleDownload[0].Item2;
|
|
FileInfo alternateFileInfo = possibleDownload[0].Item3;
|
|
DateTime matchNginxFileSystemDateTime = possibleDownload[0].Item1;
|
|
// if (alternateFileInfo.Exists)
|
|
// File.Delete(alternateFileInfo.FullName);
|
|
if (targetFileInfo.Exists)
|
|
File.Delete(targetFileInfo.FullName);
|
|
string targetJson = _HttpClient.GetStringAsync(targetFileName).Result;
|
|
File.WriteAllText(targetFileInfo.FullName, targetJson);
|
|
targetFileInfo.LastWriteTime = matchNginxFileSystemDateTime;
|
|
// File.Copy(targetFileInfo.FullName, alternateFileInfo.FullName);
|
|
File.AppendAllText(alternateFileInfo.FullName, targetJson);
|
|
}
|
|
}
|
|
#nullable disable
|
|
}
|
|
|
|
internal static void CopyDirectories(ILogger<Worker> logger, List<string> args)
|
|
{
|
|
Test();
|
|
string[] files;
|
|
Process process;
|
|
string checkDirectory;
|
|
string filter = args[3];
|
|
string replaceWith = args[4];
|
|
string searchPattern = args[2];
|
|
string sourceDirectory = Path.GetFullPath(args[0]);
|
|
string[] foundDirectories = Directory.GetDirectories(sourceDirectory, searchPattern, SearchOption.AllDirectories);
|
|
logger.LogInformation($"Found {foundDirectories.Length} directories");
|
|
foreach (string foundDirectory in foundDirectories)
|
|
{
|
|
if (!foundDirectory.Contains(filter))
|
|
continue;
|
|
logger.LogDebug(foundDirectory);
|
|
checkDirectory = foundDirectory.Replace(filter, replaceWith);
|
|
if (Directory.Exists(checkDirectory))
|
|
{
|
|
files = Directory.GetFiles(checkDirectory, "*", SearchOption.AllDirectories);
|
|
if (files.Length > 0)
|
|
continue;
|
|
Directory.Delete(checkDirectory);
|
|
}
|
|
process = Process.Start("cmd.exe", $"/c xCopy \"{foundDirectory}\" \"{checkDirectory}\" /S /E /I /H /Y");
|
|
process.WaitForExit();
|
|
}
|
|
}
|
|
|
|
} |