Init
This commit is contained in:
377
Property/Models/Stateless/A_Property.cs
Normal file
377
Property/Models/Stateless/A_Property.cs
Normal file
@ -0,0 +1,377 @@
|
||||
namespace View_by_Distance.Property.Models.Stateless;
|
||||
|
||||
public static class A_Property
|
||||
{
|
||||
|
||||
public static string DateTimeFormat() => "yyyy:MM:dd HH:mm:ss";
|
||||
|
||||
public static (int Season, string seasonName) GetSeason(int dayOfYear)
|
||||
{
|
||||
(int Season, string seasonName) result = dayOfYear switch
|
||||
{
|
||||
< 78 => new(0, "Winter"),
|
||||
< 171 => new(1, "Spring"),
|
||||
< 264 => new(2, "Summer"),
|
||||
< 354 => new(3, "Fall"),
|
||||
_ => new(4, "Winter")
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
public static List<(int g, string sourceDirectory, string[] sourceDirectoryFiles, int r)> GetGroupCollection(string rootDirectory, string searchPattern, List<string> topDirectories, int maxImagesInDirectoryForTopLevelFirstPass = 50, bool reverse = false)
|
||||
{
|
||||
List<(int g, string sourceDirectory, string[] sourceDirectoryFiles, int r)> results = new();
|
||||
string? parentDirectory;
|
||||
string[] subDirectories;
|
||||
string[] sourceDirectoryFiles;
|
||||
List<string[]> fileCollections = new();
|
||||
if (!topDirectories.Any())
|
||||
{
|
||||
topDirectories.Add(rootDirectory);
|
||||
topDirectories.AddRange(from l in Directory.GetDirectories(rootDirectory, "*", SearchOption.TopDirectoryOnly) select Path.GetFullPath(l));
|
||||
}
|
||||
for (int g = 1; g < 5; g++)
|
||||
{
|
||||
if (g == 4)
|
||||
{
|
||||
for (int i = fileCollections.Count - 1; i > -1; i--)
|
||||
{
|
||||
parentDirectory = Path.GetDirectoryName(fileCollections[i][0]);
|
||||
if (string.IsNullOrEmpty(parentDirectory))
|
||||
continue;
|
||||
results.Add(new(g, parentDirectory, fileCollections[i], results.Count));
|
||||
fileCollections.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
else if (g == 2)
|
||||
{
|
||||
fileCollections = (from l in fileCollections orderby l.Length descending select l).ToList();
|
||||
for (int i = fileCollections.Count - 1; i > -1; i--)
|
||||
{
|
||||
if (fileCollections[i].Length > maxImagesInDirectoryForTopLevelFirstPass * g)
|
||||
break;
|
||||
parentDirectory = Path.GetDirectoryName(fileCollections[i][0]);
|
||||
if (string.IsNullOrEmpty(parentDirectory))
|
||||
continue;
|
||||
results.Add(new(g, parentDirectory, fileCollections[i], results.Count));
|
||||
fileCollections.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
else if (g == 3)
|
||||
{
|
||||
subDirectories = Directory.GetDirectories(rootDirectory, "*", SearchOption.AllDirectories);
|
||||
if (reverse)
|
||||
subDirectories = subDirectories.Reverse().ToArray();
|
||||
foreach (string subDirectory in subDirectories)
|
||||
{
|
||||
sourceDirectoryFiles = Directory.GetFiles(subDirectory, "*", SearchOption.TopDirectoryOnly);
|
||||
if (!topDirectories.Contains(subDirectory))
|
||||
results.Add(new(g, subDirectory, sourceDirectoryFiles, results.Count));
|
||||
}
|
||||
}
|
||||
else if (g == 1)
|
||||
{
|
||||
sourceDirectoryFiles = Directory.GetFiles(rootDirectory, searchPattern, SearchOption.TopDirectoryOnly);
|
||||
if (sourceDirectoryFiles.Length > maxImagesInDirectoryForTopLevelFirstPass)
|
||||
fileCollections.Add(sourceDirectoryFiles);
|
||||
else
|
||||
results.Add(new(g, rootDirectory, sourceDirectoryFiles, results.Count));
|
||||
if (reverse)
|
||||
topDirectories.Reverse();
|
||||
subDirectories = topDirectories.ToArray();
|
||||
foreach (string subDirectory in subDirectories)
|
||||
{
|
||||
sourceDirectoryFiles = Directory.GetFiles(subDirectory, searchPattern, SearchOption.TopDirectoryOnly);
|
||||
if (sourceDirectoryFiles.Length > maxImagesInDirectoryForTopLevelFirstPass)
|
||||
fileCollections.Add(sourceDirectoryFiles);
|
||||
else
|
||||
{
|
||||
if (sourceDirectoryFiles.Any() || Directory.GetDirectories(subDirectory, "*", SearchOption.TopDirectoryOnly).Any())
|
||||
results.Add(new(g, subDirectory, sourceDirectoryFiles, results.Count));
|
||||
else if (searchPattern == "*")
|
||||
{
|
||||
sourceDirectoryFiles = Directory.GetFiles(subDirectory, searchPattern, SearchOption.TopDirectoryOnly);
|
||||
foreach (string subFile in sourceDirectoryFiles)
|
||||
File.Delete(subFile);
|
||||
Directory.Delete(subDirectory);
|
||||
}
|
||||
}
|
||||
}
|
||||
fileCollections.Reverse();
|
||||
}
|
||||
else
|
||||
throw new Exception();
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
public static int GetDeterministicHashCode(byte[] value)
|
||||
{
|
||||
int result;
|
||||
unchecked
|
||||
{
|
||||
int hash1 = (5381 << 16) + 5381;
|
||||
int hash2 = hash1;
|
||||
for (int i = 0; i < value.Length; i += 2)
|
||||
{
|
||||
hash1 = ((hash1 << 5) + hash1) ^ value[i];
|
||||
if (i == value.Length - 1)
|
||||
break;
|
||||
hash2 = ((hash2 << 5) + hash2) ^ value[i + 1];
|
||||
}
|
||||
result = hash1 + (hash2 * 1566083941);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static int GetDeterministicHashCode(string value)
|
||||
{
|
||||
int result;
|
||||
unchecked
|
||||
{
|
||||
int hash1 = (5381 << 16) + 5381;
|
||||
int hash2 = hash1;
|
||||
for (int i = 0; i < value.Length; i += 2)
|
||||
{
|
||||
hash1 = ((hash1 << 5) + hash1) ^ value[i];
|
||||
if (i == value.Length - 1)
|
||||
break;
|
||||
hash2 = ((hash2 << 5) + hash2) ^ value[i + 1];
|
||||
}
|
||||
result = hash1 + (hash2 * 1566083941);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static (bool?, string[]) IsWrongYear(string[] segments, string year)
|
||||
{
|
||||
bool? result;
|
||||
string[] results = (
|
||||
from l
|
||||
in segments
|
||||
where l?.Length > 2
|
||||
&& (
|
||||
l[..2] is "19" or "20"
|
||||
|| (l.Length == 5 && l.Substring(1, 2) is "19" or "20" && (l[0] is '~' or '=' or '-' or '^' or '#'))
|
||||
|| (l.Length == 6 && l[..2] is "19" or "20" && l[4] == '.')
|
||||
|| (l.Length == 7 && l.Substring(1, 2) is "19" or "20" && l[5] == '.')
|
||||
)
|
||||
select l
|
||||
).ToArray();
|
||||
string[] matches = (
|
||||
from l
|
||||
in results
|
||||
where l == year
|
||||
|| (l.Length == 5 && l.Substring(1, 4) == year && (l[0] is '~' or '=' or '-' or '^' or '#'))
|
||||
|| (l.Length == 6 && l[..4] == year && l[4] == '.')
|
||||
|| (l.Length == 7 && l.Substring(1, 4) == year && l[5] == '.')
|
||||
select l
|
||||
).ToArray();
|
||||
if (!results.Any())
|
||||
result = null;
|
||||
else
|
||||
result = !matches.Any();
|
||||
return new(result, results);
|
||||
}
|
||||
|
||||
public static List<DateTime> GetDateTimes(DateTime creationTime, DateTime lastWriteTime, DateTime? dateTime, DateTime? dateTimeDigitized, DateTime? dateTimeOriginal, DateTime? gpsDateStamp)
|
||||
{
|
||||
List<DateTime> results = new()
|
||||
{
|
||||
creationTime,
|
||||
lastWriteTime
|
||||
};
|
||||
if (dateTime.HasValue)
|
||||
results.Add(dateTime.Value);
|
||||
if (dateTimeDigitized.HasValue)
|
||||
results.Add(dateTimeDigitized.Value);
|
||||
if (dateTimeOriginal.HasValue)
|
||||
results.Add(dateTimeOriginal.Value);
|
||||
if (gpsDateStamp.HasValue)
|
||||
results.Add(gpsDateStamp.Value);
|
||||
return results;
|
||||
}
|
||||
|
||||
public static DateTime GetDateTime(Models.A_Property? property)
|
||||
{
|
||||
DateTime result;
|
||||
if (property is null)
|
||||
result = DateTime.MinValue;
|
||||
else
|
||||
{
|
||||
List<DateTime> datetimes = new()
|
||||
{
|
||||
property.CreationTime,
|
||||
property.LastWriteTime
|
||||
};
|
||||
if (property.DateTime.HasValue)
|
||||
datetimes.Add(property.DateTime.Value);
|
||||
if (property.DateTimeDigitized.HasValue)
|
||||
datetimes.Add(property.DateTimeDigitized.Value);
|
||||
if (property.DateTimeOriginal.HasValue)
|
||||
datetimes.Add(property.DateTimeOriginal.Value);
|
||||
if (property.GPSDateStamp.HasValue)
|
||||
datetimes.Add(property.GPSDateStamp.Value);
|
||||
result = datetimes.Min();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static List<DateTime> GetDateTimes(Models.A_Property property) => GetDateTimes(property.CreationTime, property.LastWriteTime, property.DateTime, property.DateTimeDigitized, property.DateTimeOriginal, property.GPSDateStamp);
|
||||
|
||||
public static DateTime GetMinimumDateTime(Models.A_Property? property)
|
||||
{
|
||||
DateTime result;
|
||||
List<DateTime> datetimes;
|
||||
if (property is null)
|
||||
result = DateTime.MinValue;
|
||||
else
|
||||
{
|
||||
datetimes = GetDateTimes(property);
|
||||
result = datetimes.Min();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static string[] GetDirectoryRenameCollection(Models.Configuration configuration, string outputResolution, string bMetadata, string cResizeName)
|
||||
{
|
||||
List<string> results = new();
|
||||
string cResizeContentDirectory;
|
||||
string cResizeSingletonDirectory;
|
||||
string bMetadataSingletonDirectory;
|
||||
string aPropertySingletonDirectory;
|
||||
bMetadataSingletonDirectory = IResult.GetResultsDateGroupDirectory(configuration, bMetadata, "{}");
|
||||
if (Directory.Exists(bMetadataSingletonDirectory))
|
||||
results.Add(bMetadataSingletonDirectory);
|
||||
aPropertySingletonDirectory = IResult.GetResultsDateGroupDirectory(configuration, nameof(A_Property), "{}");
|
||||
if (Directory.Exists(aPropertySingletonDirectory))
|
||||
results.Add(aPropertySingletonDirectory);
|
||||
cResizeContentDirectory = Path.Combine(IResult.GetResultsFullGroupDirectory(configuration, cResizeName, outputResolution, includeResizeGroup: true, includeModel: false, includePredictorModel: false), "()");
|
||||
if (Directory.Exists(cResizeContentDirectory))
|
||||
results.Add(cResizeContentDirectory);
|
||||
cResizeSingletonDirectory = Path.Combine(IResult.GetResultsFullGroupDirectory(configuration, cResizeName, outputResolution, includeResizeGroup: true, includeModel: false, includePredictorModel: false), "{}");
|
||||
if (Directory.Exists(cResizeSingletonDirectory))
|
||||
results.Add(cResizeSingletonDirectory);
|
||||
return results.ToArray();
|
||||
}
|
||||
|
||||
public static void SearchForAbandonedFilesFull(string argZero, string rootDirectory, bool onlyJson)
|
||||
{
|
||||
bool check;
|
||||
string[] files;
|
||||
string moveFile;
|
||||
string extension;
|
||||
string? directory;
|
||||
const int last = 6;
|
||||
string searchPattern;
|
||||
string searchDirectory;
|
||||
int lessThan = last + 1;
|
||||
string? parentDirectory;
|
||||
DirectoryInfo directoryInfo;
|
||||
string fileNameWithoutExtension;
|
||||
IEnumerator<FileInfo> enumerator;
|
||||
List<string[]> fileMovePaths = new();
|
||||
List<string> toReviewFiles = new();
|
||||
int rootDirectoryLength = rootDirectory.Length;
|
||||
for (int i = 1; i < lessThan; i++)
|
||||
{
|
||||
files = i switch
|
||||
{
|
||||
1 => Directory.GetFiles(rootDirectory, "*.json", SearchOption.AllDirectories),
|
||||
2 => Directory.GetFiles(rootDirectory, "*.nosj", SearchOption.AllDirectories),
|
||||
3 => Directory.GetFiles(rootDirectory, "*.jpeg", SearchOption.AllDirectories),
|
||||
4 => Directory.GetFiles(rootDirectory, "*.tvs", SearchOption.AllDirectories),
|
||||
5 => Directory.GetFiles(rootDirectory, "*.png", SearchOption.AllDirectories),
|
||||
last => Directory.GetFiles(rootDirectory, "*", SearchOption.AllDirectories),
|
||||
_ => Array.Empty<string>()
|
||||
};
|
||||
foreach (string file in files)
|
||||
{
|
||||
extension = Path.GetExtension(file);
|
||||
if (extension == ".delete")
|
||||
continue;
|
||||
directory = Path.GetDirectoryName(file);
|
||||
if (string.IsNullOrEmpty(directory))
|
||||
continue;
|
||||
fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
|
||||
if (file.EndsWith(" - R.png"))
|
||||
continue;
|
||||
if (fileNameWithoutExtension.Length < 1)
|
||||
continue;
|
||||
if (fileNameWithoutExtension[1..] != string.Concat(" - ", Path.GetFileNameWithoutExtension(directory)))
|
||||
{
|
||||
searchPattern = string.Concat(fileNameWithoutExtension, '*');
|
||||
searchDirectory = string.Concat(argZero, directory[rootDirectoryLength..]);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (fileNameWithoutExtension.Length < 4)
|
||||
continue;
|
||||
searchPattern = string.Concat(fileNameWithoutExtension[4..], '*');
|
||||
parentDirectory = Path.GetDirectoryName(directory);
|
||||
if (string.IsNullOrEmpty(parentDirectory))
|
||||
continue;
|
||||
searchDirectory = string.Concat(argZero, parentDirectory[rootDirectoryLength..]);
|
||||
}
|
||||
directoryInfo = new(searchDirectory);
|
||||
if (!directoryInfo.Exists)
|
||||
check = false;
|
||||
else
|
||||
{
|
||||
enumerator = directoryInfo.EnumerateFiles(searchPattern, SearchOption.TopDirectoryOnly).GetEnumerator();
|
||||
check = enumerator.MoveNext();
|
||||
}
|
||||
if (check)
|
||||
continue;
|
||||
toReviewFiles.Add(file);
|
||||
}
|
||||
if (toReviewFiles.Any())
|
||||
throw new Exception("Need to fix property having the extension before .json extension!");
|
||||
foreach (string toReviewFile in toReviewFiles)
|
||||
{
|
||||
extension = Path.GetExtension(toReviewFile);
|
||||
if (extension == ".delete")
|
||||
continue;
|
||||
directory = Path.GetDirectoryName(toReviewFile);
|
||||
if (string.IsNullOrEmpty(directory))
|
||||
continue;
|
||||
moveFile = Path.Combine(directory, Path.ChangeExtension(toReviewFile, ".delete"));
|
||||
if (i == last)
|
||||
fileMovePaths.Add(new string[] { toReviewFile, moveFile });
|
||||
else if (extension is ".nosj")
|
||||
File.Delete(toReviewFile);
|
||||
else
|
||||
{
|
||||
if (File.Exists(moveFile))
|
||||
File.Delete(moveFile);
|
||||
File.Move(toReviewFile, moveFile);
|
||||
}
|
||||
}
|
||||
toReviewFiles.Clear();
|
||||
if (onlyJson)
|
||||
break;
|
||||
}
|
||||
if (fileMovePaths.Any())
|
||||
throw new Exception(string.Join(',', (from l in fileMovePaths select l[0]).ToArray()));
|
||||
}
|
||||
|
||||
public static string GetDiffRootDirectory(string diffPropertyDirectory)
|
||||
{
|
||||
string result = string.Empty;
|
||||
string results = " - Results";
|
||||
string? checkDirectory = diffPropertyDirectory;
|
||||
for (int i = 0; i < int.MaxValue; i++)
|
||||
{
|
||||
checkDirectory = Path.GetDirectoryName(checkDirectory);
|
||||
if (string.IsNullOrEmpty(checkDirectory))
|
||||
break;
|
||||
if (checkDirectory.EndsWith(results))
|
||||
{
|
||||
result = checkDirectory[..^results.Length];
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
42
Property/Models/Stateless/Configuration.cs
Normal file
42
Property/Models/Stateless/Configuration.cs
Normal file
@ -0,0 +1,42 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Phares.Shared;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace View_by_Distance.Property.Models.Stateless;
|
||||
|
||||
public abstract class Configuration
|
||||
{
|
||||
|
||||
public static Models.Configuration Get(IsEnvironment isEnvironment, IConfigurationRoot configurationRoot, string workingDirectory)
|
||||
{
|
||||
Models.Configuration? result;
|
||||
string environmentName = IsEnvironment.GetEnvironmentName(isEnvironment);
|
||||
string section = string.Concat(environmentName, ":", nameof(Binder.Configuration));
|
||||
IConfigurationSection configurationSection = configurationRoot.GetSection(section);
|
||||
Binder.Configuration configuration = configurationSection.Get<Binder.Configuration>();
|
||||
string json = JsonSerializer.Serialize(configuration, new JsonSerializerOptions() { WriteIndented = true });
|
||||
result = JsonSerializer.Deserialize<Models.Configuration>(json);
|
||||
if (result is null)
|
||||
throw new Exception(json);
|
||||
string jsonThis = result.ToString();
|
||||
if (jsonThis != json)
|
||||
{
|
||||
int? check = null;
|
||||
int min = new int[] { json.Length, jsonThis.Length }.Min();
|
||||
for (int i = 0; i < min; i++)
|
||||
{
|
||||
if (json[i] == jsonThis[i])
|
||||
continue;
|
||||
check = i;
|
||||
break;
|
||||
}
|
||||
if (check is null)
|
||||
throw new Exception();
|
||||
string a = json[..check.Value].Split(',')[^1];
|
||||
string b = json[check.Value..].Split(',')[0];
|
||||
throw new Exception($"{a}{b}");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
24
Property/Models/Stateless/IPath.cs
Normal file
24
Property/Models/Stateless/IPath.cs
Normal file
@ -0,0 +1,24 @@
|
||||
namespace View_by_Distance.Property.Models.Stateless;
|
||||
|
||||
public interface IPath
|
||||
{
|
||||
|
||||
string TestStatic_GetRelativePath(string path, int length);
|
||||
static string GetRelativePath(string path, int length) => XPath.GetRelativePath(path, length);
|
||||
|
||||
bool TestStatic_DeleteEmptyDirectories(string rootDirectory);
|
||||
static bool DeleteEmptyDirectories(string rootDirectory) => XPath.DeleteEmptyDirectories(rootDirectory);
|
||||
|
||||
List<string> TestStatic_GetDirectoryNames(string directory);
|
||||
static List<string> GetDirectoryNames(string directory) => XPath.GetDirectoryNames(directory);
|
||||
|
||||
bool TestStatic_WriteAllText(string path, string contents, bool compareBeforeWrite);
|
||||
static bool WriteAllText(string path, string contents, bool compareBeforeWrite) => XPath.WriteAllText(path, contents, compareBeforeWrite);
|
||||
|
||||
(int level, List<string> directories) TestStatic_Get(string rootDirectory, string sourceDirectory);
|
||||
static (int level, List<string> directories) Get(string rootDirectory, string sourceDirectory) => XPath.Get(rootDirectory, sourceDirectory);
|
||||
|
||||
string TestStatic_GetDirectory(string sourceDirectory, int level, string directoryName);
|
||||
static string GetDirectory(string sourceDirectory, int level, string directoryName) => XPath.GetDirectory(sourceDirectory, level, directoryName);
|
||||
|
||||
}
|
24
Property/Models/Stateless/IResult.cs
Normal file
24
Property/Models/Stateless/IResult.cs
Normal file
@ -0,0 +1,24 @@
|
||||
namespace View_by_Distance.Property.Models.Stateless;
|
||||
|
||||
public interface IResult
|
||||
{
|
||||
|
||||
string TestStatic_GetRelativePath(Models.Configuration configuration, string path);
|
||||
static string GetRelativePath(Models.Configuration configuration, string path) => Result.GetRelativePath(configuration, path);
|
||||
|
||||
string TestStatic_GetResultsGroupDirectory(Models.Configuration configuration, string description);
|
||||
static string GetResultsGroupDirectory(Models.Configuration configuration, string description) => Result.GetResultsGroupDirectory(configuration, description);
|
||||
|
||||
string TestStatic_GetResultsDateGroupDirectory(Models.Configuration configuration, string description);
|
||||
static string GetResultsDateGroupDirectory(Models.Configuration configuration, string description) => Result.GetResultsDateGroupDirectory(configuration, description);
|
||||
|
||||
string TestStatic_GetResultsDateGroupDirectory(Models.Configuration configuration, string description, string jsonGroup);
|
||||
static string GetResultsDateGroupDirectory(Models.Configuration configuration, string description, string jsonGroup) => Result.GetResultsDateGroupDirectory(configuration, description, jsonGroup);
|
||||
|
||||
string TestStatic_GetResultsFullGroupDirectory(Models.Configuration configuration, string description, string outputResolution, bool includeResizeGroup, bool includeModel, bool includePredictorModel);
|
||||
static string GetResultsFullGroupDirectory(Models.Configuration configuration, string description, string outputResolution, bool includeResizeGroup, bool includeModel, bool includePredictorModel) => Result.GetResultsFullGroupDirectory(configuration, description, outputResolution, includeResizeGroup, includeModel, includePredictorModel);
|
||||
|
||||
List<string> TestStatic_GetDirectoryInfoCollection(Models.Configuration configuration, string sourceDirectory, string description, string outputResolution, bool includeResizeGroup, bool includeModel, bool includePredictorModel, string contentDescription, string singletonDescription, string collectionDescription);
|
||||
static List<string> GetDirectoryInfoCollection(Models.Configuration configuration, string sourceDirectory, string description, string outputResolution, bool includeResizeGroup, bool includeModel, bool includePredictorModel, string contentDescription, string singletonDescription, string collectionDescription) => Result.GetDirectoryInfoCollection(configuration, sourceDirectory, description, outputResolution, includeResizeGroup, includeModel, includePredictorModel, contentDescription, singletonDescription, collectionDescription);
|
||||
|
||||
}
|
143
Property/Models/Stateless/Path.cs
Normal file
143
Property/Models/Stateless/Path.cs
Normal file
@ -0,0 +1,143 @@
|
||||
namespace View_by_Distance.Property.Models.Stateless;
|
||||
|
||||
internal class XPath
|
||||
{
|
||||
|
||||
internal static string GetRelativePath(string path, int length)
|
||||
{
|
||||
string result = path[length..].Replace(@"\", "/");
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static bool DeleteEmptyDirectories(string rootDirectory)
|
||||
{
|
||||
bool result;
|
||||
if (!Directory.Exists(rootDirectory))
|
||||
result = false;
|
||||
else
|
||||
{
|
||||
string[] files;
|
||||
string[] directories = Directory.GetDirectories(rootDirectory, "*", SearchOption.TopDirectoryOnly);
|
||||
if (directories.Length > 0)
|
||||
files = Array.Empty<string>();
|
||||
else
|
||||
files = Directory.GetFiles(rootDirectory, "*", SearchOption.AllDirectories);
|
||||
if (directories.Length == 0 && files.Length == 0)
|
||||
{
|
||||
result = true;
|
||||
Directory.Delete(rootDirectory);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = false;
|
||||
foreach (string directory in directories)
|
||||
{
|
||||
result = DeleteEmptyDirectories(directory);
|
||||
if (result)
|
||||
result = DeleteEmptyDirectories(directory);
|
||||
}
|
||||
if (files is null)
|
||||
{
|
||||
directories = Directory.GetDirectories(rootDirectory, "*", SearchOption.TopDirectoryOnly);
|
||||
result = directories.Length == 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static bool WriteAllText(string path, string contents, bool compareBeforeWrite)
|
||||
{
|
||||
bool result;
|
||||
string text;
|
||||
if (!compareBeforeWrite)
|
||||
result = true;
|
||||
else
|
||||
{
|
||||
if (!File.Exists(path))
|
||||
text = string.Empty;
|
||||
else
|
||||
text = File.ReadAllText(path);
|
||||
result = text != contents;
|
||||
}
|
||||
if (result)
|
||||
{
|
||||
if (path.Contains("()"))
|
||||
File.WriteAllText(path, contents);
|
||||
else if (path.Contains("{}") && !path.EndsWith(".json"))
|
||||
File.WriteAllText(path, contents);
|
||||
else if (path.Contains("[]") && !path.EndsWith(".json"))
|
||||
File.WriteAllText(path, contents);
|
||||
else if (path.Contains("{}") && path.EndsWith(".json") && contents[0] == '{')
|
||||
File.WriteAllText(path, contents);
|
||||
else if (path.Contains("[]") && path.EndsWith(".json") && contents[0] == '[')
|
||||
File.WriteAllText(path, contents);
|
||||
else
|
||||
File.WriteAllText(path, contents);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static List<string> GetDirectoryNames(string directory)
|
||||
{
|
||||
List<string> results = new();
|
||||
string? checkDirectory = directory;
|
||||
string? pathRoot = Path.GetPathRoot(directory);
|
||||
string extension = Path.GetExtension(directory);
|
||||
if (string.IsNullOrEmpty(pathRoot))
|
||||
throw new Exception($"{nameof(pathRoot)} is null!");
|
||||
if (Directory.Exists(directory))
|
||||
results.Add(Path.GetFileName(directory));
|
||||
else if ((string.IsNullOrEmpty(extension) || extension.Length > 3) && !File.Exists(directory))
|
||||
results.Add(Path.GetFileName(directory));
|
||||
for (int i = 0; i < int.MaxValue; i++)
|
||||
{
|
||||
checkDirectory = Path.GetDirectoryName(checkDirectory);
|
||||
if (string.IsNullOrEmpty(checkDirectory) || checkDirectory == pathRoot)
|
||||
break;
|
||||
results.Add(Path.GetFileName(checkDirectory));
|
||||
}
|
||||
results.Add(pathRoot);
|
||||
results.Reverse();
|
||||
return results;
|
||||
}
|
||||
|
||||
internal static (int level, List<string> directories) Get(string rootDirectory, string sourceDirectory)
|
||||
{
|
||||
int result = 0;
|
||||
string? directory;
|
||||
string? checkDirectory;
|
||||
List<string> results = new();
|
||||
checkDirectory = sourceDirectory;
|
||||
for (int i = 0; i < int.MaxValue; i++)
|
||||
{
|
||||
result += 1;
|
||||
directory = Path.GetFileName(checkDirectory);
|
||||
if (string.IsNullOrEmpty(directory))
|
||||
break;
|
||||
results.Add(directory);
|
||||
checkDirectory = Path.GetDirectoryName(checkDirectory);
|
||||
if (checkDirectory == rootDirectory)
|
||||
break;
|
||||
}
|
||||
results.Reverse();
|
||||
return new(result, results);
|
||||
}
|
||||
|
||||
internal static string GetDirectory(string sourceDirectory, int level, string directoryName)
|
||||
{
|
||||
string result;
|
||||
string? checkDirectory;
|
||||
checkDirectory = Path.GetDirectoryName(sourceDirectory);
|
||||
for (int i = 0; i < level; i++)
|
||||
checkDirectory = Path.GetDirectoryName(checkDirectory);
|
||||
if (string.IsNullOrEmpty(checkDirectory))
|
||||
throw new Exception();
|
||||
checkDirectory = Path.Combine(checkDirectory, directoryName);
|
||||
if (!Directory.Exists(checkDirectory))
|
||||
_ = Directory.CreateDirectory(checkDirectory);
|
||||
result = checkDirectory;
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
98
Property/Models/Stateless/Result.cs
Normal file
98
Property/Models/Stateless/Result.cs
Normal file
@ -0,0 +1,98 @@
|
||||
namespace View_by_Distance.Property.Models.Stateless;
|
||||
|
||||
internal class Result
|
||||
{
|
||||
|
||||
internal static string GetRelativePath(Models.Configuration configuration, string path)
|
||||
{
|
||||
string result = XPath.GetRelativePath(path, configuration.RootDirectory.Length);
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static string GetResultsGroupDirectory(Models.Configuration configuration, string description)
|
||||
{
|
||||
string result = Path.Combine($"{configuration.RootDirectory} - Results", description.Replace("_", ") "));
|
||||
if (!Directory.Exists(result))
|
||||
_ = Directory.CreateDirectory(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static string GetResultsDateGroupDirectory(Models.Configuration configuration, string description)
|
||||
{
|
||||
string result = Path.Combine(GetResultsGroupDirectory(configuration, description), configuration.DateGroup);
|
||||
if (!Directory.Exists(result))
|
||||
_ = Directory.CreateDirectory(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static string GetResultsDateGroupDirectory(Models.Configuration configuration, string description, string jsonGroup)
|
||||
{
|
||||
string result = Path.Combine(GetResultsDateGroupDirectory(configuration, description), jsonGroup);
|
||||
if (!Directory.Exists(result))
|
||||
_ = Directory.CreateDirectory(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static string GetResultsFullGroupDirectory(Models.Configuration configuration, string description, string outputResolution, bool includeResizeGroup, bool includeModel, bool includePredictorModel)
|
||||
{
|
||||
string result = GetResultsDateGroupDirectory(configuration, description);
|
||||
if (includeResizeGroup)
|
||||
result = Path.Combine(result, outputResolution);
|
||||
if (includeModel && includePredictorModel)
|
||||
{
|
||||
string dateGroupDirectory = string.Concat(outputResolution.Replace(" ", string.Empty), " - ", "_Configuration.ModelName", " - ", "_Configuration.PredictorModelName");
|
||||
result = Path.Combine(result, dateGroupDirectory);
|
||||
}
|
||||
else if (includeModel)
|
||||
throw new Exception();
|
||||
else if (includePredictorModel)
|
||||
throw new Exception();
|
||||
if (!Directory.Exists(result))
|
||||
_ = Directory.CreateDirectory(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static List<string> GetDirectoryInfoCollection(Models.Configuration configuration, string sourceDirectory, string description, string outputResolution, bool includeResizeGroup, bool includeModel, bool includePredictorModel, string contentDescription, string singletonDescription, string collectionDescription)
|
||||
{
|
||||
List<string> results = new();
|
||||
string result = string.Empty;
|
||||
string checkDirectory;
|
||||
string sourceDirectorySegment = GetRelativePath(configuration, sourceDirectory);
|
||||
string dateGroupDirectory = IResult.GetResultsFullGroupDirectory(configuration, description, outputResolution, includeResizeGroup, includeModel, includePredictorModel);
|
||||
if (!string.IsNullOrEmpty(contentDescription))
|
||||
{
|
||||
result = string.Concat(Path.Combine(dateGroupDirectory, "<>"), sourceDirectorySegment);
|
||||
DirectoryInfo contentDirectoryInfo = new(result.Replace("<>", "()"));
|
||||
if (!contentDirectoryInfo.Exists)
|
||||
contentDirectoryInfo.Create();
|
||||
checkDirectory = Path.Combine(dateGroupDirectory, string.Concat("() - ", contentDescription));
|
||||
if (!Directory.Exists(checkDirectory))
|
||||
_ = Directory.CreateDirectory(checkDirectory);
|
||||
}
|
||||
if (!string.IsNullOrEmpty(singletonDescription))
|
||||
{
|
||||
result = string.Concat(Path.Combine(dateGroupDirectory, "<>"), sourceDirectorySegment);
|
||||
DirectoryInfo singletonDirectoryInfo = new(result.Replace("<>", "{}"));
|
||||
if (!singletonDirectoryInfo.Exists)
|
||||
singletonDirectoryInfo.Create();
|
||||
checkDirectory = Path.Combine(dateGroupDirectory, string.Concat("{} - ", singletonDescription));
|
||||
if (!Directory.Exists(checkDirectory))
|
||||
_ = Directory.CreateDirectory(checkDirectory);
|
||||
}
|
||||
if (!string.IsNullOrEmpty(collectionDescription))
|
||||
{
|
||||
result = string.Concat(Path.Combine(dateGroupDirectory, "<>"), sourceDirectorySegment);
|
||||
DirectoryInfo collectionDirectoryInfo = new(result.Replace("<>", "[]"));
|
||||
if (!collectionDirectoryInfo.Exists)
|
||||
collectionDirectoryInfo.Create();
|
||||
checkDirectory = Path.Combine(dateGroupDirectory, string.Concat("[] - ", collectionDescription));
|
||||
if (!Directory.Exists(checkDirectory))
|
||||
_ = Directory.CreateDirectory(checkDirectory);
|
||||
}
|
||||
if (string.IsNullOrEmpty(result))
|
||||
throw new Exception();
|
||||
results.Add(result);
|
||||
return results;
|
||||
}
|
||||
|
||||
}
|
10
Property/Models/Stateless/SerilogExtensionMethods.cs
Normal file
10
Property/Models/Stateless/SerilogExtensionMethods.cs
Normal file
@ -0,0 +1,10 @@
|
||||
namespace View_by_Distance.Property.Models.Stateless;
|
||||
|
||||
internal static class SerilogExtensionMethods
|
||||
{
|
||||
|
||||
internal static void Warn(this Serilog.ILogger log, string messageTemplate) => log.Warning(messageTemplate);
|
||||
|
||||
internal static void Info(this Serilog.ILogger log, string messageTemplate) => log.Information(messageTemplate);
|
||||
|
||||
}
|
Reference in New Issue
Block a user