Files
view-by-distance-mklink-con…/Shared/Models/Stateless/Methods/FilePair.cs
Mike Phares 30d8a270f9 Removed Obsolete A_Property Methods
Changed GetDimensions to handle a stream at the end and one exit

Switched to using Action? over IDlibDotNet for Tick method

Switched to using AsReadOnly over new()

Moved Meta Base to Shared
2025-06-30 16:42:34 -07:00

149 lines
6.8 KiB
C#

using System.Collections.ObjectModel;
using View_by_Distance.Shared.Models.Properties;
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class FilePair
{
internal static ReadOnlyCollection<Models.FilePair> GetFilePairs(IPropertyConfiguration propertyConfiguration, string directorySearchFilter, string extension, string jsonGroupDirectory, ReadOnlyCollection<ReadOnlyCollection<FilePath>> filePathsCollection)
{
List<Models.FilePair>? results = null;
int renamed;
const bool useCeilingAverage = true;
ReadOnlyCollection<string[]>? jsonFilesCollection = null;
ReadOnlyDictionary<int, ReadOnlyCollection<FilePath>> idToFilePaths = FilePath.GetKeyValuePairs(filePathsCollection);
IReadOnlyDictionary<int, ReadOnlyCollection<FilePath>>? compareFileNamesToFiles = null;
for (int i = 0; i < idToFilePaths.Count; i++)
{
renamed = 0;
jsonFilesCollection = XDirectory.GetFilesCollection(jsonGroupDirectory, directorySearchFilter, extension, useCeilingAverage);
renamed += LookForAbandoned(propertyConfiguration, jsonFilesCollection, idToFilePaths, extension);
if (renamed > 0)
continue;
compareFileNamesToFiles = GetKeyValuePairs(propertyConfiguration, jsonFilesCollection);
results = XDirectory.GetFiles(propertyConfiguration, filePathsCollection, idToFilePaths, compareFileNamesToFiles);
renamed += XDirectory.MaybeMove(propertyConfiguration, results, jsonGroupDirectory, extension);
if (renamed == 0)
break;
if (i > 10)
throw new NotImplementedException();
}
if (results is null || jsonFilesCollection is null || compareFileNamesToFiles is null)
throw new NullReferenceException(nameof(results));
return results.AsReadOnly();
}
private static int LookForAbandoned(IPropertyConfiguration propertyConfiguration, ReadOnlyCollection<string[]> jsonFilesCollection, IReadOnlyDictionary<int, ReadOnlyCollection<FilePath>> fileNamesToFiles, string extension)
{
int result;
bool check;
bool moved = false;
List<string> renameCollection = [];
foreach (string[] files in jsonFilesCollection)
{
if (files.Length == 0)
continue;
check = AnyMoved(propertyConfiguration, fileNamesToFiles, extension, renameCollection, files);
if (!moved && check)
moved = true;
}
if (renameCollection.Count > 0)
XDirectory.MoveFiles(renameCollection, "{}", "{abd}");
result = renameCollection.Count;
if (moved && result == 0)
result = 1;
return result;
}
private static bool AnyMoved(IPropertyConfiguration propertyConfiguration, IReadOnlyDictionary<int, ReadOnlyCollection<FilePath>> fileNamesToFiles, string extension, List<string> renameCollection, string[] files)
{
bool result = false;
string checkFile;
string directory;
FilePath filePath;
string fileNameWith;
string checkDirectory;
string directoryName;
Models.FileHolder fileHolder;
List<string> directoryNames = [];
ReadOnlyCollection<FilePath>? collection;
foreach (string file in files)
{
if (files.Any(l => l.StartsWith(propertyConfiguration.RootDirectory)))
continue;
if (!file.EndsWith(extension))
throw new Exception();
fileHolder = IFileHolder.Get(file);
if (!fileHolder.Exists)
continue;
filePath = FilePath.Get(propertyConfiguration, fileHolder, index: null);
if (filePath.Id is null)
continue;
if (!fileNamesToFiles.TryGetValue(filePath.Id.Value, out collection))
renameCollection.Add(file);
else
{
directoryNames.Clear();
directoryName = Path.GetFileName(filePath.DirectoryFullPath) ?? throw new Exception();
foreach (FilePath f in collection)
directoryNames.Add(Path.GetFileName(f.DirectoryFullPath) ?? throw new Exception());
if (directoryNames.Count == 0 || directoryNames.Distinct().Count() != 1)
continue;
if (directoryName != directoryNames[0])
{
directory = Path.GetDirectoryName(filePath.DirectoryFullPath) ?? throw new Exception();
checkDirectory = Path.Combine(directory, directoryNames[0]);
if (!Directory.Exists(checkDirectory))
_ = Directory.CreateDirectory(checkDirectory);
fileNameWith = collection.Count > 1 ? filePath.Name : $"{collection[0].Name}{extension}";
checkFile = Path.Combine(checkDirectory, fileNameWith);
if (!result)
result = true;
if (!File.Exists(checkFile))
File.Move(file, checkFile);
else
{
if (new FileInfo(file).LastWriteTime > new FileInfo(checkFile).LastWriteTime)
File.Delete(file);
else
File.Move(file, checkFile, true);
}
}
}
}
return result;
}
private static ReadOnlyDictionary<int, ReadOnlyCollection<FilePath>> GetKeyValuePairs(IPropertyConfiguration propertyConfiguration, ReadOnlyCollection<string[]> filesCollection)
{
Dictionary<int, ReadOnlyCollection<FilePath>> results = [];
FilePath filePath;
List<FilePath>? collection;
Models.FileHolder fileHolder;
Dictionary<int, List<FilePath>> keyValuePairs = [];
foreach (string[] files in filesCollection)
{
foreach (string file in files)
{
fileHolder = IFileHolder.Get(file);
if (!fileHolder.Exists)
continue;
filePath = FilePath.Get(propertyConfiguration, fileHolder, index: null);
if (filePath.Id is null)
continue;
if (!keyValuePairs.TryGetValue(filePath.Id.Value, out collection))
{
keyValuePairs.Add(filePath.Id.Value, []);
if (!keyValuePairs.TryGetValue(filePath.Id.Value, out collection))
throw new Exception();
}
collection.Add(filePath);
}
}
foreach (KeyValuePair<int, List<FilePath>> keyValuePair in keyValuePairs)
results.Add(keyValuePair.Key, keyValuePair.Value.AsReadOnly());
return new(results);
}
}