Container bug fix

This commit is contained in:
2022-08-29 10:59:41 -07:00
parent c1d30b5bbc
commit 659e0e39bf
14 changed files with 140 additions and 80 deletions

View File

@ -144,18 +144,26 @@ public class Container
return results;
}
private static List<Shared.Models.Container> GetContainers(Configuration configuration, string aPropertySingletonDirectory, List<(int, string, FileHolder[], int)> fileHolderGroupCollection, List<(int, string, List<(string, Shared.Models.Property?)>, int)> collectionFromJson)
private static Shared.Models.Container[] GetContainers(Configuration configuration, bool firstRun, string aPropertySingletonDirectory, List<(int, string, FileHolder[], int)> fileHolderGroupCollection, List<(int, string, List<(string, Shared.Models.Property?)>, int)> collectionFromJson)
{
List<Shared.Models.Container> results = new();
Shared.Models.Container[] results;
int length;
string key;
int additional;
string inferred;
string fileName;
string extension;
List<Item> items;
string[] existing;
string keyWithJson;
string relativePath;
string? directoryName;
string extensionLowered;
FileHolder keyFileHolder;
Shared.Models.Container container;
string? fileNameWithoutExtension;
bool isValidImageFormatExtension;
Shared.Models.Container container;
List<string> keySourceDirectories;
Dictionary<string, Shared.Models.Container> keyValuePairs = new();
Dictionary<string, (string SourceDirectory, FileHolder FileHolder)> fileHolderKeyValuePairs = new();
length = configuration.RootDirectory.Length;
foreach ((int g, string sourceDirectory, FileHolder[] sourceDirectoryFileHolderCollection, int r) in fileHolderGroupCollection)
@ -163,8 +171,21 @@ public class Container
foreach (FileHolder sourceDirectoryFileHolder in sourceDirectoryFileHolderCollection)
{
relativePath = Shared.Models.Stateless.Methods.IPath.GetRelativePath(sourceDirectoryFileHolder.FullName, length);
key = string.Concat(relativePath, ".json");
fileHolderKeyValuePairs.Add(key, new(sourceDirectory, sourceDirectoryFileHolder));
fileHolderKeyValuePairs.Add(relativePath, new(sourceDirectory, sourceDirectoryFileHolder));
extension = Path.GetExtension(sourceDirectoryFileHolder.FullName);
extensionLowered = Path.GetExtension(sourceDirectoryFileHolder.FullName).ToLower();
if (extension != extensionLowered)
{
directoryName = Path.GetDirectoryName(sourceDirectoryFileHolder.FullName);
if (string.IsNullOrEmpty(directoryName))
continue;
fileNameWithoutExtension = Path.GetFileNameWithoutExtension(sourceDirectoryFileHolder.FullName);
if (string.IsNullOrEmpty(fileNameWithoutExtension))
continue;
fileName = Path.Combine(directoryName, $"{fileNameWithoutExtension}{extensionLowered}");
relativePath = Shared.Models.Stateless.Methods.IPath.GetRelativePath(fileName, length);
fileHolderKeyValuePairs.Add(relativePath, new(sourceDirectory, sourceDirectoryFileHolder));
}
}
}
length = aPropertySingletonDirectory.Length;
@ -176,9 +197,9 @@ public class Container
keySourceDirectories = new();
foreach ((string sourceDirectoryFile, Shared.Models.Property? property) in collection)
{
key = Shared.Models.Stateless.Methods.IPath.GetRelativePath(sourceDirectoryFile, length);
relativePath = key[..^5];
if (!fileHolderKeyValuePairs.ContainsKey(key))
keyWithJson = Shared.Models.Stateless.Methods.IPath.GetRelativePath(sourceDirectoryFile, length);
relativePath = keyWithJson[..^5];
if (!fileHolderKeyValuePairs.ContainsKey(relativePath))
{
inferred = string.Concat(configuration.RootDirectory, relativePath);
keyFileHolder = new(inferred);
@ -190,9 +211,9 @@ public class Container
}
else
{
keyFileHolder = fileHolderKeyValuePairs[key].FileHolder;
keySourceDirectories.Add(fileHolderKeyValuePairs[key].SourceDirectory);
if (!fileHolderKeyValuePairs.Remove(key))
keyFileHolder = fileHolderKeyValuePairs[relativePath].FileHolder;
keySourceDirectories.Add(fileHolderKeyValuePairs[relativePath].SourceDirectory);
if (!fileHolderKeyValuePairs.Remove(relativePath))
throw new Exception();
if (keyFileHolder.ExtensionLowered is ".json")
continue;
@ -210,7 +231,7 @@ public class Container
if (keySourceDirectories.Distinct().Count() != 1)
continue;
container = new(g, r, items, keySourceDirectories[0]);
results.Add(container);
keyValuePairs.Add(keySourceDirectories[0], container);
}
}
length = configuration.RootDirectory.Length;
@ -220,10 +241,9 @@ public class Container
foreach (FileHolder sourceDirectoryFileHolder in sourceDirectoryFileHolderCollection)
{
relativePath = Shared.Models.Stateless.Methods.IPath.GetRelativePath(sourceDirectoryFileHolder.FullName, length);
key = string.Concat(relativePath, ".json");
if (!fileHolderKeyValuePairs.ContainsKey(key))
if (!fileHolderKeyValuePairs.ContainsKey(relativePath))
continue;
if (!fileHolderKeyValuePairs.Remove(key))
if (!fileHolderKeyValuePairs.Remove(relativePath))
throw new Exception();
if (sourceDirectoryFileHolder.ExtensionLowered is ".json")
continue;
@ -232,19 +252,38 @@ public class Container
}
if (items.Any())
{
container = new(g, r, items, sourceDirectory);
results.Add(container);
if (!keyValuePairs.ContainsKey(sourceDirectory))
{
container = new(g, r, items, sourceDirectory);
keyValuePairs.Add(sourceDirectory, container);
}
else
{
additional = 0;
container = keyValuePairs[sourceDirectory];
existing = (from l in container.Items select l.ImageFileHolder?.FullName).ToArray();
foreach (Item item in items)
{
if (item.ImageFileHolder is null || existing.Contains(item.ImageFileHolder.FullName))
continue;
additional += 1;
items.Add(item);
}
container = new(g, r + additional, items, sourceDirectory);
keyValuePairs[sourceDirectory] = container;
}
}
}
if (fileHolderKeyValuePairs.Any())
if (!firstRun && fileHolderKeyValuePairs.Any())
throw new Exception();
results = (from l in results orderby l.G, l.R select l).ToList();
KeyValuePair<string, Shared.Models.Container>[] sortedKeyValuePairs = (from l in keyValuePairs orderby l.Value.G, l.Value.R select l).ToArray();
results = (from l in sortedKeyValuePairs select l.Value).ToArray();
return results;
}
public static List<Shared.Models.Container> GetContainers(Configuration configuration, A_Property propertyLogic)
public static Shared.Models.Container[] GetContainers(Configuration configuration, bool firstRun, A_Property propertyLogic)
{
List<Shared.Models.Container> results;
Shared.Models.Container[] results;
string searchPattern = "*";
List<string> topDirectories = new();
List<(int g, string sourceDirectory, string[] sourceDirectoryFiles, int r)> jsonCollection;
@ -254,7 +293,7 @@ public class Container
jsonCollection = GetJsonGroupCollection(configuration, propertyLogic, aPropertySingletonDirectory);
fileHolderGroupCollection = GetFileHolderGroupCollection(configuration, propertyLogic, searchPattern, topDirectories);
collectionFromJson = GetCollection(aPropertySingletonDirectory, jsonCollection);
results = GetContainers(configuration, aPropertySingletonDirectory, fileHolderGroupCollection, collectionFromJson);
results = GetContainers(configuration, firstRun, aPropertySingletonDirectory, fileHolderGroupCollection, collectionFromJson);
return results;
}