diff --git a/Adaptation/FileHandlers/MoveMatchingFiles/FileRead.cs b/Adaptation/FileHandlers/MoveMatchingFiles/FileRead.cs index 9e72baa..7ec2cfb 100644 --- a/Adaptation/FileHandlers/MoveMatchingFiles/FileRead.cs +++ b/Adaptation/FileHandlers/MoveMatchingFiles/FileRead.cs @@ -7,6 +7,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text; using System.Text.Json; using System.Threading; @@ -153,9 +154,9 @@ public class FileRead : Shared.FileRead, IFileRead return results; } - private void MoveCollection(DateTime dateTime, List<(string matchingFile, string checkFile)> collection) + private static List<(string, string, string, string, string)> GetCollection(List<(string matchingFile, string checkFile)> collection) { - long preWait; + List<(string, string, string, string, string)> results = new(); string errFile; string checkDirectory; string noWaitDirectory; @@ -165,10 +166,24 @@ public class FileRead : Shared.FileRead, IFileRead checkDirectory = Path.GetDirectoryName(checkFile); if (!Directory.Exists(checkDirectory)) _ = Directory.CreateDirectory(checkDirectory); - File.Move(matchingFile, checkFile); noWaitDirectory = Path.Combine(checkDirectory, "NoWaitDirectory"); + results.Add(new(matchingFile, checkFile, errFile, checkDirectory, noWaitDirectory)); + } + return results; + } + + private void MoveCollection(DateTime dateTime, List<(string matchingFile, string checkFile)> collection) + { + long preWait; + List<(string checkFile, string errFile)> postCollection = new(); + foreach ((string matchingFile, string checkFile, string errFile, string checkDirectory, string noWaitDirectory) in GetCollection(collection)) + { + File.Move(matchingFile, checkFile); if (Directory.Exists(noWaitDirectory)) + { + postCollection.Add(new(checkFile, errFile)); continue; + } if (_FileConnectorConfiguration?.FileHandleWaitTime is null) preWait = DateTime.Now.AddMilliseconds(1234).Ticks; else @@ -186,10 +201,45 @@ public class FileRead : Shared.FileRead, IFileRead if (!File.Exists(checkFile)) break; if (new TimeSpan(DateTime.Now.Ticks - dateTime.Ticks).TotalSeconds > _BreakAfterSeconds) - throw new Exception($"Not all files were consumned after {_BreakAfterSeconds} second(s)!"); + throw new Exception($"Not all files were consumed after {_BreakAfterSeconds} second(s)!"); Thread.Sleep(500); } } + if (postCollection.Any()) + { + Thread.Sleep(500); + StringBuilder stringBuilder = new(); + foreach ((string checkFile, string errFile) in postCollection) + { + if (File.Exists(errFile)) + _ = stringBuilder.AppendLine(File.ReadAllText(errFile)); + if (File.Exists(checkFile)) + _ = stringBuilder.AppendLine($"<{checkFile}> was not consumed by the end!"); + } + if (stringBuilder.Length > 0) + throw new Exception(stringBuilder.ToString()); + } + } + + private static void CreatePointerFile(int numberLength, string parentDirectory, List matchingFiles) + { +#nullable enable + string checkFile; + string writeFile; + string? directoryName; + int parentDirectoryLength = parentDirectory.Length; + foreach (string matchingFile in matchingFiles) + { + directoryName = Path.GetDirectoryName(matchingFile); + if (directoryName is null) + continue; + checkFile = $"{matchingFile[0]}{directoryName.Substring(parentDirectoryLength + numberLength + 1)}"; + writeFile = Path.Combine(parentDirectory, $"{directoryName.Substring(parentDirectory.Length + 1, numberLength)}.txt"); + if (File.Exists(writeFile)) + continue; + File.AppendAllLines(writeFile, new string[] { parentDirectory, matchingFile, directoryName, checkFile }); + } +#nullable disable } private Tuple> GetExtractResult(string reportFullPath, DateTime dateTime) @@ -205,6 +255,9 @@ public class FileRead : Shared.FileRead, IFileRead List matchingFiles = GetMatchingFiles(ticks, reportFullPath, searchDirectories); if (matchingFiles.Count != searchDirectories.Count) throw new Exception($"Didn't find all files after {_BreakAfterSeconds} second(s)!"); + try + { CreatePointerFile(numberLength, parentParentDirectory, matchingFiles); } + catch (Exception) { } List<(string matchingFile, string checkFile)> collection = GetCollection(numberLength, parentParentDirectory, matchingFiles); MoveCollection(dateTime, collection); return results;