IDirectory first pass
This commit is contained in:
243
Shared/Models/Stateless/Methods/XDirectory.cs
Normal file
243
Shared/Models/Stateless/Methods/XDirectory.cs
Normal file
@ -0,0 +1,243 @@
|
||||
namespace View_by_Distance.Shared.Models.Stateless.Methods;
|
||||
|
||||
internal abstract partial class XDirectory
|
||||
{
|
||||
|
||||
private static int GetCeilingAverage(List<string[]> fileCollection)
|
||||
{
|
||||
List<int> counts = new();
|
||||
foreach (string[] files in fileCollection)
|
||||
counts.Add(files.Length);
|
||||
int average = (int)Math.Ceiling(counts.Average());
|
||||
return average;
|
||||
}
|
||||
|
||||
private static List<string[]> GetFilesCollection(List<string[]> fileCollection, int ceilingAverage)
|
||||
{
|
||||
List<string[]> results = new();
|
||||
foreach (string[] files in fileCollection)
|
||||
{
|
||||
if (files.Length < ceilingAverage)
|
||||
results.Add(files);
|
||||
}
|
||||
foreach (string[] files in fileCollection)
|
||||
{
|
||||
if (files.Length >= ceilingAverage)
|
||||
results.Add(files);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
internal static List<string[]> GetFilesCollection(string directory, string directorySearchFilter, string fileSearchFilter)
|
||||
{
|
||||
List<string[]> results = new();
|
||||
if (!fileSearchFilter.Contains('*'))
|
||||
fileSearchFilter = string.Concat('*', fileSearchFilter);
|
||||
if (!directorySearchFilter.Contains('*'))
|
||||
directorySearchFilter = string.Concat('*', directorySearchFilter);
|
||||
string[] directories = Directory.GetDirectories(directory, directorySearchFilter, SearchOption.TopDirectoryOnly);
|
||||
foreach (string innerDirectory in directories)
|
||||
results.Add(Directory.GetFiles(innerDirectory, fileSearchFilter, SearchOption.AllDirectories));
|
||||
int ceilingAverage = GetCeilingAverage(results);
|
||||
results = GetFilesCollection(results, ceilingAverage);
|
||||
return results;
|
||||
}
|
||||
|
||||
internal static IReadOnlyDictionary<string, List<string>> GetFilesKeyValuePairs(List<string[]> filesCollection)
|
||||
{
|
||||
Dictionary<string, List<string>> results = new();
|
||||
string fileName;
|
||||
List<string>? collection;
|
||||
foreach (string[] files in filesCollection)
|
||||
{
|
||||
foreach (string file in files)
|
||||
{
|
||||
fileName = Path.GetFileName(file);
|
||||
if (!results.TryGetValue(fileName, out collection))
|
||||
{
|
||||
results.Add(fileName, new());
|
||||
if (!results.TryGetValue(fileName, out collection))
|
||||
throw new Exception();
|
||||
}
|
||||
collection.Add(file);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
internal static int LookForAbandoned(List<string[]> jsonFilesCollection, IReadOnlyDictionary<string, List<string>> fileNamesToFiles, string extension)
|
||||
{
|
||||
string fileName;
|
||||
List<string>? collection;
|
||||
int length = extension.Length;
|
||||
List<(string, string)> rename = new();
|
||||
foreach (string[] files in jsonFilesCollection)
|
||||
{
|
||||
foreach (string file in files)
|
||||
{
|
||||
fileName = Path.GetFileName(file);
|
||||
if (fileName.Length < length || !fileName.EndsWith(extension))
|
||||
throw new Exception();
|
||||
if (!fileNamesToFiles.TryGetValue(fileName[..^length], out collection))
|
||||
rename.Add(new(file, string.Concat(file, ".del")));
|
||||
|
||||
}
|
||||
}
|
||||
foreach ((string from, string to) in rename)
|
||||
{
|
||||
if (File.Exists(to))
|
||||
continue;
|
||||
File.Move(from, to);
|
||||
}
|
||||
return rename.Count;
|
||||
}
|
||||
|
||||
private static void IsNotUniqueLoop(string file, List<string> collection)
|
||||
{
|
||||
TimeSpan timeSpan;
|
||||
FileInfo possibleFileInfo;
|
||||
FileInfo fileInfo = new(file);
|
||||
foreach (string possible in collection)
|
||||
{
|
||||
possibleFileInfo = new(possible);
|
||||
if (possibleFileInfo.LastWriteTime == fileInfo.LastWriteTime && possibleFileInfo.Length == fileInfo.Length)
|
||||
continue;
|
||||
timeSpan = new(possibleFileInfo.LastWriteTime.Ticks - fileInfo.LastWriteTime.Ticks);
|
||||
if (timeSpan.TotalMinutes < 2 && possibleFileInfo.Length == fileInfo.Length)
|
||||
{
|
||||
File.SetLastWriteTime(file, new DateTime[] { possibleFileInfo.LastWriteTime, fileInfo.LastWriteTime }.Min());
|
||||
continue;
|
||||
}
|
||||
throw new Exception();
|
||||
}
|
||||
}
|
||||
|
||||
private static string? GetMatch(string file, List<string> collection)
|
||||
{
|
||||
string? result = null;
|
||||
FileInfo possibleFileInfo;
|
||||
List<long> lengths = new();
|
||||
List<string> matches = new();
|
||||
FileInfo fileInfo = new(file);
|
||||
List<DateTime> creationTimes = new();
|
||||
foreach (string possible in collection)
|
||||
{
|
||||
possibleFileInfo = new(possible);
|
||||
lengths.Add(possibleFileInfo.Length);
|
||||
creationTimes.Add(possibleFileInfo.CreationTime);
|
||||
if (possibleFileInfo.CreationTime != fileInfo.LastWriteTime)
|
||||
continue;
|
||||
matches.Add(possible);
|
||||
}
|
||||
if (matches.Count == 1 || (lengths.Distinct().Count() == 1 && creationTimes.Distinct().Count() == 1))
|
||||
result = matches.First();
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static List<FilePair> GetFiles(List<string[]> filesCollection, IReadOnlyDictionary<string, List<string>> fileNamesToFiles, string extension, IReadOnlyDictionary<string, List<string>> compareFileNamesToFiles)
|
||||
{
|
||||
List<FilePair> results = new();
|
||||
string? match;
|
||||
string fileName;
|
||||
bool uniqueFileName;
|
||||
List<string>? collection;
|
||||
foreach (string[] files in filesCollection)
|
||||
{
|
||||
foreach (string file in files)
|
||||
{
|
||||
fileName = Path.GetFileName(file);
|
||||
if (!fileNamesToFiles.TryGetValue(fileName, out collection))
|
||||
throw new Exception();
|
||||
uniqueFileName = collection.Count == 1;
|
||||
if (!uniqueFileName)
|
||||
IsNotUniqueLoop(file, collection);
|
||||
if (!compareFileNamesToFiles.TryGetValue(string.Concat(fileName, extension), out collection))
|
||||
results.Add(new(file, uniqueFileName, new(), null));
|
||||
else
|
||||
{
|
||||
if (!collection.Any())
|
||||
results.Add(new(file, uniqueFileName, collection, null));
|
||||
else if (uniqueFileName && collection.Count == 1)
|
||||
results.Add(new(file, uniqueFileName, collection, collection.First()));
|
||||
else
|
||||
{
|
||||
match = GetMatch(file, collection);
|
||||
results.Add(new(file, uniqueFileName, collection, match));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private static void IsUniqueLoop(string resultAllInOne, string resultAllInOneDirectory, FilePair item, List<(string, string)> rename)
|
||||
{
|
||||
char directory;
|
||||
string fileName;
|
||||
foreach (string path in item.Collection)
|
||||
{
|
||||
if (path.Contains(resultAllInOne))
|
||||
continue;
|
||||
fileName = Path.GetFileName(path);
|
||||
directory = IDirectory.GetDirectory(fileName);
|
||||
rename.Add(new(path, Path.Combine(resultAllInOneDirectory, directory.ToString(), fileName)));
|
||||
}
|
||||
}
|
||||
|
||||
private static void IsNotUniqueLoop(string directory, string resultAllInOne, string jsonGroupDirectory, string extension, FilePair item, List<(string, string)> rename)
|
||||
{
|
||||
int length = directory.Length;
|
||||
foreach (string path in item.Collection)
|
||||
{
|
||||
if (!path.Contains(resultAllInOne))
|
||||
continue;
|
||||
if (item.Match is null || path != item.Match)
|
||||
continue;
|
||||
rename.Add(new(path, string.Concat(jsonGroupDirectory, item.Path[length..], extension)));
|
||||
}
|
||||
}
|
||||
|
||||
internal static int MaybeMove(string directory, string resultAllInOne, List<FilePair> filePairs, string jsonGroupDirectory, string extension)
|
||||
{
|
||||
FileInfo? toFileInfo;
|
||||
FileInfo fromFileInfo;
|
||||
string checkDirectory;
|
||||
List<(string, string)> rename = new();
|
||||
string resultAllInOneDirectory = Path.Combine(jsonGroupDirectory, resultAllInOne);
|
||||
foreach (FilePair item in filePairs)
|
||||
{
|
||||
if (item.IsUnique)
|
||||
IsUniqueLoop(resultAllInOne, resultAllInOneDirectory, item, rename);
|
||||
else
|
||||
IsNotUniqueLoop(directory, resultAllInOne, jsonGroupDirectory, extension, item, rename);
|
||||
}
|
||||
foreach ((string from, string to) in rename)
|
||||
{
|
||||
toFileInfo = null;
|
||||
checkDirectory = to;
|
||||
fromFileInfo = new(from);
|
||||
if (!fromFileInfo.Exists)
|
||||
continue;
|
||||
for (int i = 0; i < int.MaxValue; i++)
|
||||
{
|
||||
toFileInfo = new(checkDirectory);
|
||||
if (toFileInfo.Directory is null)
|
||||
continue;
|
||||
if (!toFileInfo.Directory.Exists)
|
||||
_ = Directory.CreateDirectory(toFileInfo.Directory.FullName);
|
||||
if (checkDirectory.Length > 199)
|
||||
throw new Exception();
|
||||
if (!toFileInfo.Exists)
|
||||
break;
|
||||
else if (fromFileInfo.Length == toFileInfo.Length && fromFileInfo.LastWriteTime == toFileInfo.LastWriteTime)
|
||||
checkDirectory = string.Concat(checkDirectory, ".del");
|
||||
else
|
||||
checkDirectory = string.Concat(checkDirectory, ".i");
|
||||
}
|
||||
File.Move(from, checkDirectory);
|
||||
}
|
||||
_ = IPath.DeleteEmptyDirectories(jsonGroupDirectory);
|
||||
return rename.Count;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user