377 lines
16 KiB
C#
377 lines
16 KiB
C#
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;
|
|
}
|
|
|
|
} |