780 lines
42 KiB
C#
780 lines
42 KiB
C#
using ShellProgressBar;
|
|
using System.Diagnostics;
|
|
using System.Drawing;
|
|
using System.Drawing.Imaging;
|
|
using System.Globalization;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using View_by_Distance.Property.Models.Stateless;
|
|
using View_by_Distance.Shared.Models.Stateless;
|
|
|
|
namespace View_by_Distance.Property.Models;
|
|
|
|
public class PropertyLogic
|
|
{
|
|
|
|
protected readonly Dictionary<int, int[]> _IndicesFromNew;
|
|
protected readonly Dictionary<int, int[]> _IndicesFromOld;
|
|
|
|
public List<string> AngleBracketCollection { get; }
|
|
public Dictionary<int, int[]> IndicesFromNew => _IndicesFromNew;
|
|
public Dictionary<int, int[]> IndicesFromOld => _IndicesFromOld;
|
|
|
|
private readonly Serilog.ILogger? _Log;
|
|
private readonly string[] _VerifyToSeason;
|
|
private readonly int _MaxDegreeOfParallelism;
|
|
private readonly ASCIIEncoding _ASCIIEncoding;
|
|
private readonly Configuration _Configuration;
|
|
private readonly List<string> _ExceptionsDirectories;
|
|
private readonly JsonSerializerOptions _WriteIndentedJsonSerializerOptions;
|
|
|
|
public PropertyLogic(int maxDegreeOfParallelism, Configuration configuration, string[] verifyToSeason)
|
|
{
|
|
_Configuration = configuration;
|
|
_ExceptionsDirectories = new();
|
|
_VerifyToSeason = verifyToSeason;
|
|
_ASCIIEncoding = new ASCIIEncoding();
|
|
AngleBracketCollection = new List<string>();
|
|
_Log = Serilog.Log.ForContext<A_Property>();
|
|
_MaxDegreeOfParallelism = maxDegreeOfParallelism;
|
|
_WriteIndentedJsonSerializerOptions = new JsonSerializerOptions { WriteIndented = true };
|
|
if (verifyToSeason is null)
|
|
throw new Exception();
|
|
string json;
|
|
string fullPath;
|
|
Dictionary<int, int[]>? indicesFromOld;
|
|
List<KeyValuePair<int, int[]>>? collection;
|
|
Dictionary<int, int[]> indicesFromNew = new();
|
|
string? rootDirectoryParent = Path.GetDirectoryName(configuration.RootDirectory);
|
|
if (string.IsNullOrEmpty(rootDirectoryParent))
|
|
throw new Exception($"{nameof(rootDirectoryParent)} is null!");
|
|
string keyValuePairsJsonFile = Path.Combine(rootDirectoryParent, "keyValuePairs-637864726339738801.json");
|
|
if (!File.Exists(keyValuePairsJsonFile))
|
|
indicesFromOld = new();
|
|
else
|
|
{
|
|
json = File.ReadAllText(keyValuePairsJsonFile);
|
|
indicesFromOld = JsonSerializer.Deserialize<Dictionary<int, int[]>>(json);
|
|
if (indicesFromOld is null)
|
|
throw new Exception($"{nameof(indicesFromOld)} is null!");
|
|
}
|
|
foreach (string propertyContentCollectionFile in configuration.PropertyContentCollectionFiles)
|
|
{
|
|
fullPath = Path.GetFullPath(string.Concat(rootDirectoryParent, propertyContentCollectionFile));
|
|
if (fullPath.Contains(configuration.RootDirectory))
|
|
continue;
|
|
if (!File.Exists(fullPath))
|
|
continue;
|
|
json = File.ReadAllText(fullPath);
|
|
collection = JsonSerializer.Deserialize<List<KeyValuePair<int, int[]>>>(json);
|
|
if (collection is null)
|
|
throw new Exception($"{nameof(collection)} is null!");
|
|
foreach (KeyValuePair<int, int[]> keyValuePair in collection)
|
|
{
|
|
if (indicesFromNew.ContainsKey(keyValuePair.Key))
|
|
continue;
|
|
indicesFromNew.Add(keyValuePair.Key, keyValuePair.Value);
|
|
}
|
|
}
|
|
_IndicesFromNew = indicesFromNew;
|
|
_IndicesFromOld = indicesFromOld;
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true });
|
|
return result;
|
|
}
|
|
|
|
private long LogDelta(long ticks, string methodName)
|
|
{
|
|
long result;
|
|
if (_Log is null)
|
|
throw new Exception($"{nameof(_Log)} is null!");
|
|
double delta = new TimeSpan(DateTime.Now.Ticks - ticks).TotalMilliseconds;
|
|
_Log.Debug($"{methodName} took {Math.Floor(delta)} millisecond(s)");
|
|
result = DateTime.Now.Ticks;
|
|
return result;
|
|
}
|
|
|
|
public static List<DateTime> GetMetadataDateTimesByPattern(string dateTimeFormat, string filteredSourceDirectoryFile)
|
|
{
|
|
List<DateTime> results = new();
|
|
try
|
|
{
|
|
DateTime checkDateTime;
|
|
DateTime kristy = new(1976, 3, 8);
|
|
IReadOnlyList<MetadataExtractor.Directory> directories = MetadataExtractor.ImageMetadataReader.ReadMetadata(filteredSourceDirectoryFile);
|
|
foreach (MetadataExtractor.Directory directory in directories)
|
|
{
|
|
foreach (MetadataExtractor.Tag tag in directory.Tags)
|
|
{
|
|
if (string.IsNullOrEmpty(tag.Description) || tag.Description.Length != dateTimeFormat.Length)
|
|
continue;
|
|
if (!DateTime.TryParseExact(tag.Description, dateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out checkDateTime))
|
|
continue;
|
|
if (checkDateTime < kristy)
|
|
continue;
|
|
results.Add(checkDateTime);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception) { }
|
|
return results;
|
|
}
|
|
|
|
#pragma warning disable CA1416
|
|
|
|
private A_Property GetImageProperty(string angleBracket, string filteredSourceDirectoryFile, bool populateId, bool isIgnoreExtension, bool isValidImageFormatExtension, bool isValidMetadataExtensions, int? id, List<int> indices)
|
|
{
|
|
A_Property result;
|
|
if (_Log is null)
|
|
throw new Exception($"{nameof(_Log)} is null!");
|
|
if (_Configuration.WriteBitmapDataBytes is null)
|
|
throw new Exception($"{nameof(_Configuration.WriteBitmapDataBytes)} is null!");
|
|
long ticks;
|
|
byte[] bytes;
|
|
string value;
|
|
int encodingHash;
|
|
int? width = null;
|
|
int? height = null;
|
|
string dateTimeFormat;
|
|
DateTime checkDateTime;
|
|
DateTime? dateTime = null;
|
|
PropertyItem? propertyItem;
|
|
string make = string.Empty;
|
|
string model = string.Empty;
|
|
DateTime? gpsDateStamp = null;
|
|
DateTime? dateTimeOriginal = null;
|
|
string orientation = string.Empty;
|
|
DateTime? dateTimeDigitized = null;
|
|
FileInfo fileInfo = new(filteredSourceDirectoryFile);
|
|
long fileInfoLength = fileInfo.Length;
|
|
DateTime creationTime = fileInfo.CreationTime;
|
|
DateTime lastWriteTime = fileInfo.LastWriteTime;
|
|
if (isValidMetadataExtensions)
|
|
{
|
|
dateTimeFormat = "ddd MMM dd HH:mm:ss yyyy";
|
|
List<DateTime> dateTimes = GetMetadataDateTimesByPattern(dateTimeFormat, filteredSourceDirectoryFile);
|
|
if (dateTimes.Any())
|
|
dateTimeOriginal = dateTimes.Min();
|
|
}
|
|
else if (!isIgnoreExtension && isValidImageFormatExtension)
|
|
{
|
|
try
|
|
{
|
|
using Image image = Image.FromFile(filteredSourceDirectoryFile);
|
|
if (populateId && (id is null || !indices.Any()))
|
|
{
|
|
using Bitmap bitmap = new(image);
|
|
Rectangle rectangle = new(0, 0, image.Width, image.Height);
|
|
BitmapData bitmapData = bitmap.LockBits(rectangle, ImageLockMode.ReadOnly, bitmap.PixelFormat);
|
|
IntPtr intPtr = bitmapData.Scan0;
|
|
int length = bitmapData.Stride * bitmap.Height;
|
|
bytes = new byte[length];
|
|
Marshal.Copy(intPtr, bytes, 0, length);
|
|
bitmap.UnlockBits(bitmapData);
|
|
if (id is null)
|
|
{
|
|
ticks = DateTime.Now.Ticks;
|
|
id = Stateless.A_Property.GetDeterministicHashCode(bytes);
|
|
if (_MaxDegreeOfParallelism < 2)
|
|
ticks = LogDelta(ticks, nameof(Stateless.A_Property.GetDeterministicHashCode));
|
|
}
|
|
if (_Configuration.WriteBitmapDataBytes.Value)
|
|
{
|
|
FileInfo contentFileInfo = new(Path.Combine(angleBracket.Replace("<>", "()"), Path.GetFileName(filteredSourceDirectoryFile)));
|
|
File.WriteAllBytes(Path.ChangeExtension(contentFileInfo.FullName, string.Empty), bytes);
|
|
}
|
|
if (_IndicesFromNew.ContainsKey(id.Value) && _IndicesFromNew[id.Value].Any())
|
|
indices.AddRange(_IndicesFromNew[id.Value]);
|
|
else
|
|
{
|
|
ticks = DateTime.Now.Ticks;
|
|
string encoding = Encoding.Default.GetString(bytes);
|
|
if (_MaxDegreeOfParallelism < 2)
|
|
ticks = LogDelta(ticks, nameof(Encoding.Default.GetString));
|
|
encodingHash = Stateless.A_Property.GetDeterministicHashCode(encoding);
|
|
if (_MaxDegreeOfParallelism < 2)
|
|
ticks = LogDelta(ticks, nameof(Stateless.A_Property.GetDeterministicHashCode));
|
|
if (!_IndicesFromOld.ContainsKey(encodingHash))
|
|
indices.Add(encodingHash);
|
|
else
|
|
indices.AddRange(_IndicesFromOld[encodingHash]);
|
|
}
|
|
}
|
|
width = image.Width;
|
|
height = image.Height;
|
|
dateTimeFormat = Stateless.A_Property.DateTimeFormat();
|
|
if (image.PropertyIdList.Contains((int)IExif.Tags.DateTime))
|
|
{
|
|
propertyItem = image.GetPropertyItem((int)IExif.Tags.DateTime);
|
|
if (propertyItem?.Value is not null)
|
|
{
|
|
value = _ASCIIEncoding.GetString(propertyItem.Value, 0, propertyItem.Len - 1);
|
|
if (value.Length > dateTimeFormat.Length)
|
|
value = value[..dateTimeFormat.Length];
|
|
if (value.Length == dateTimeFormat.Length && DateTime.TryParseExact(value, dateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out checkDateTime))
|
|
dateTime = checkDateTime;
|
|
}
|
|
}
|
|
if (image.PropertyIdList.Contains((int)IExif.Tags.DateTimeDigitized))
|
|
{
|
|
propertyItem = image.GetPropertyItem((int)IExif.Tags.DateTimeDigitized);
|
|
if (propertyItem?.Value is not null)
|
|
{
|
|
value = _ASCIIEncoding.GetString(propertyItem.Value, 0, propertyItem.Len - 1);
|
|
if (value.Length > dateTimeFormat.Length)
|
|
value = value[..dateTimeFormat.Length];
|
|
if (value.Length == dateTimeFormat.Length && DateTime.TryParseExact(value, dateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out checkDateTime))
|
|
dateTimeDigitized = checkDateTime;
|
|
}
|
|
}
|
|
if (image.PropertyIdList.Contains((int)IExif.Tags.DateTimeOriginal))
|
|
{
|
|
propertyItem = image.GetPropertyItem((int)IExif.Tags.DateTimeOriginal);
|
|
if (propertyItem?.Value is not null)
|
|
{
|
|
value = _ASCIIEncoding.GetString(propertyItem.Value, 0, propertyItem.Len - 1);
|
|
if (value.Length > dateTimeFormat.Length)
|
|
value = value[..dateTimeFormat.Length];
|
|
if (value.Length == dateTimeFormat.Length && DateTime.TryParseExact(value, dateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out checkDateTime))
|
|
dateTimeOriginal = checkDateTime;
|
|
}
|
|
}
|
|
if (image.PropertyIdList.Contains((int)IExif.Tags.GPSDateStamp))
|
|
{
|
|
propertyItem = image.GetPropertyItem((int)IExif.Tags.GPSDateStamp);
|
|
if (propertyItem?.Value is not null)
|
|
{
|
|
value = _ASCIIEncoding.GetString(propertyItem.Value, 0, propertyItem.Len - 1);
|
|
if (value.Length > dateTimeFormat.Length)
|
|
value = value[..dateTimeFormat.Length];
|
|
if (value.Length == dateTimeFormat.Length && DateTime.TryParseExact(value, dateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out checkDateTime))
|
|
gpsDateStamp = checkDateTime;
|
|
}
|
|
}
|
|
if (image.PropertyIdList.Contains((int)IExif.Tags.Make))
|
|
{
|
|
propertyItem = image.GetPropertyItem((int)IExif.Tags.Make);
|
|
if (propertyItem?.Value is not null)
|
|
{
|
|
value = _ASCIIEncoding.GetString(propertyItem.Value, 0, propertyItem.Len - 1);
|
|
make = value;
|
|
}
|
|
}
|
|
if (image.PropertyIdList.Contains((int)IExif.Tags.Model))
|
|
{
|
|
propertyItem = image.GetPropertyItem((int)IExif.Tags.Model);
|
|
if (propertyItem?.Value is not null)
|
|
{
|
|
value = _ASCIIEncoding.GetString(propertyItem.Value, 0, propertyItem.Len - 1);
|
|
model = value;
|
|
}
|
|
}
|
|
if (image.PropertyIdList.Contains((int)IExif.Tags.Orientation))
|
|
{
|
|
propertyItem = image.GetPropertyItem((int)IExif.Tags.Orientation);
|
|
if (propertyItem?.Value is not null)
|
|
{
|
|
value = BitConverter.ToInt16(propertyItem.Value, 0).ToString();
|
|
orientation = value;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
_Log.Info(string.Concat(new StackFrame().GetMethod()?.Name, " <", filteredSourceDirectoryFile, ">"));
|
|
}
|
|
}
|
|
else
|
|
dateTimeOriginal = null;
|
|
result = new(creationTime, dateTime, dateTimeDigitized, dateTimeOriginal, fileInfoLength, gpsDateStamp, height, id, indices.ToArray(), lastWriteTime, make, model, orientation, width);
|
|
return result;
|
|
}
|
|
|
|
#pragma warning restore CA1416
|
|
|
|
private A_Property GetProperty(List<Tuple<string, DateTime>> filteredSourceDirectoryFileTuples, List<string> parseExceptions, bool firstPass, string angleBracket, string filteredSourceDirectoryFile, FileInfo fileInfo, bool isIgnoreExtension, bool isValidImageFormatExtension, bool isValidMetadataExtensions)
|
|
{
|
|
A_Property? result;
|
|
if (_Configuration.ForcePropertyLastWriteTimeToCreationTime is null)
|
|
throw new Exception($"{nameof(_Configuration.ForcePropertyLastWriteTimeToCreationTime)} is null!");
|
|
if (_Configuration.PopulatePropertyId is null)
|
|
throw new Exception($"{nameof(_Configuration.PopulatePropertyId)} is null!");
|
|
if (_Configuration.PropertiesChangedForProperty is null)
|
|
throw new Exception($"{nameof(_Configuration.PropertiesChangedForProperty)} is null!");
|
|
string json;
|
|
int? id = null;
|
|
List<int> indices = new();
|
|
bool hasWrongYearProperty = false;
|
|
string[] changesFrom = new string[] { "B_Metadata" };
|
|
bool populateId = !firstPass && _Configuration.PopulatePropertyId.Value;
|
|
List<DateTime> dateTimes = (from l in filteredSourceDirectoryFileTuples where changesFrom.Contains(l.Item1) select l.Item2).ToList();
|
|
if (!fileInfo.Exists)
|
|
{
|
|
if (fileInfo.Directory?.Parent is null)
|
|
throw new Exception();
|
|
string parentCheck = Path.Combine(fileInfo.Directory.Parent.FullName, fileInfo.Name);
|
|
if (File.Exists(parentCheck))
|
|
{
|
|
File.Move(parentCheck, fileInfo.FullName);
|
|
fileInfo.Refresh();
|
|
}
|
|
}
|
|
if (_Configuration.ForcePropertyLastWriteTimeToCreationTime.Value && !fileInfo.Exists && File.Exists(Path.ChangeExtension(fileInfo.FullName, ".delete")))
|
|
{
|
|
File.Move(Path.ChangeExtension(fileInfo.FullName, ".delete"), fileInfo.FullName);
|
|
fileInfo.Refresh();
|
|
}
|
|
if (_Configuration.ForcePropertyLastWriteTimeToCreationTime.Value && fileInfo.Exists && fileInfo.LastWriteTime != fileInfo.CreationTime)
|
|
{
|
|
File.SetLastWriteTime(fileInfo.FullName, fileInfo.CreationTime);
|
|
fileInfo.Refresh();
|
|
}
|
|
if (_Configuration.PropertiesChangedForProperty.Value)
|
|
result = null;
|
|
else if (!fileInfo.Exists)
|
|
result = null;
|
|
else if (dateTimes.Any() && dateTimes.Max() > fileInfo.LastWriteTime)
|
|
result = null;
|
|
else
|
|
{
|
|
json = File.ReadAllText(fileInfo.FullName);
|
|
try
|
|
{
|
|
bool check = true;
|
|
A_Property? property = JsonSerializer.Deserialize<A_Property>(json);
|
|
if (!isIgnoreExtension && isValidImageFormatExtension && ((populateId && property?.Id is null) || property?.Width is null || property?.Height is null))
|
|
{
|
|
check = false;
|
|
id = property?.Id;
|
|
if (property is not null && property.Indices.Any())
|
|
indices = property.Indices.ToList();
|
|
property = GetImageProperty(angleBracket, filteredSourceDirectoryFile, populateId, isIgnoreExtension, isValidImageFormatExtension, isValidMetadataExtensions, id, indices);
|
|
}
|
|
if (!isIgnoreExtension && isValidImageFormatExtension && populateId && property is not null && !property.Indices.Any())
|
|
{
|
|
check = false;
|
|
id = property?.Id;
|
|
if (property is not null && property.Indices.Any())
|
|
indices = property.Indices.ToList();
|
|
property = GetImageProperty(angleBracket, filteredSourceDirectoryFile, populateId, isIgnoreExtension, isValidImageFormatExtension, isValidMetadataExtensions, id, indices);
|
|
}
|
|
if (!isIgnoreExtension && isValidImageFormatExtension && populateId && property is not null && property.LastWriteTime != new FileInfo(filteredSourceDirectoryFile).LastWriteTime)
|
|
{
|
|
check = false;
|
|
id = null;
|
|
indices.Clear();
|
|
property = GetImageProperty(angleBracket, filteredSourceDirectoryFile, populateId, isIgnoreExtension, isValidImageFormatExtension, isValidMetadataExtensions, id, indices);
|
|
}
|
|
if (!isIgnoreExtension && isValidImageFormatExtension && property?.Width is not null && property?.Height is not null && property.Width.Value == property.Height.Value && File.Exists(filteredSourceDirectoryFile))
|
|
{
|
|
check = false;
|
|
id = property?.Id;
|
|
if (property is not null && property.Indices.Any())
|
|
indices = property.Indices.ToList();
|
|
property = GetImageProperty(angleBracket, filteredSourceDirectoryFile, populateId, isIgnoreExtension, isValidImageFormatExtension, isValidMetadataExtensions, id, indices);
|
|
if (property?.Width is not null && property?.Height is not null && property.Width.Value != property.Height.Value)
|
|
throw new Exception("Was square!");
|
|
}
|
|
if (json.Contains("WrongYear"))
|
|
{
|
|
id = property?.Id;
|
|
hasWrongYearProperty = true;
|
|
}
|
|
if (property is null)
|
|
throw new Exception();
|
|
if (!check)
|
|
result = null;
|
|
else
|
|
{
|
|
result = property;
|
|
filteredSourceDirectoryFileTuples.Add(new Tuple<string, DateTime>(nameof(A_Property), fileInfo.LastWriteTime));
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
result = null;
|
|
parseExceptions.Add(nameof(A_Property));
|
|
}
|
|
}
|
|
if (result is null)
|
|
{
|
|
result = GetImageProperty(angleBracket, filteredSourceDirectoryFile, populateId, isIgnoreExtension, isValidImageFormatExtension, isValidMetadataExtensions, id, indices);
|
|
json = JsonSerializer.Serialize(result, _WriteIndentedJsonSerializerOptions);
|
|
if (populateId && IPath.WriteAllText(fileInfo.FullName, json, compareBeforeWrite: true))
|
|
{
|
|
if (!_Configuration.ForcePropertyLastWriteTimeToCreationTime.Value && (!fileInfo.Exists || fileInfo.LastWriteTime == fileInfo.CreationTime))
|
|
filteredSourceDirectoryFileTuples.Add(new Tuple<string, DateTime>(nameof(A_Property), DateTime.Now));
|
|
else
|
|
{
|
|
File.SetLastWriteTime(fileInfo.FullName, fileInfo.CreationTime);
|
|
fileInfo.Refresh();
|
|
filteredSourceDirectoryFileTuples.Add(new Tuple<string, DateTime>(nameof(A_Property), fileInfo.CreationTime));
|
|
}
|
|
}
|
|
}
|
|
else if (hasWrongYearProperty)
|
|
{
|
|
json = JsonSerializer.Serialize(result, _WriteIndentedJsonSerializerOptions);
|
|
if (IPath.WriteAllText(fileInfo.FullName, json, compareBeforeWrite: true))
|
|
{
|
|
File.SetLastWriteTime(fileInfo.FullName, fileInfo.CreationTime);
|
|
fileInfo.Refresh();
|
|
filteredSourceDirectoryFileTuples.Add(new Tuple<string, DateTime>(nameof(A_Property), fileInfo.CreationTime));
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private bool AnyFilesMoved(Group group)
|
|
{
|
|
bool result = false;
|
|
if (_Log is null)
|
|
throw new Exception($"{nameof(_Log)} is null!");
|
|
int season;
|
|
string[] matches;
|
|
string deleteFile;
|
|
bool? isWrongYear;
|
|
string seasonName;
|
|
DateTime dateTime;
|
|
A_Property? property;
|
|
string destinationFile;
|
|
DateTime minimumDateTime;
|
|
FileInfo? propertyFileInfo;
|
|
string destinationDirectory;
|
|
string[] sourceDirectorySegments;
|
|
string filteredSourceDirectoryFile;
|
|
FileInfo filteredSourceDirectoryFileInfo;
|
|
DateTime directoryMaximumOfMinimumDateTime = DateTime.MinValue;
|
|
for (int i = 0; i < group.FilteredSourceDirectoryFiles.Length; i++)
|
|
{
|
|
if (!group.ValidImageFormatExtentionCollection[i])
|
|
continue;
|
|
property = group.PropertyCollection[i];
|
|
if (property is null)
|
|
continue;
|
|
minimumDateTime = Stateless.A_Property.GetMinimumDateTime(property);
|
|
if (minimumDateTime > directoryMaximumOfMinimumDateTime)
|
|
directoryMaximumOfMinimumDateTime = minimumDateTime;
|
|
filteredSourceDirectoryFile = group.FilteredSourceDirectoryFiles[i];
|
|
filteredSourceDirectoryFileInfo = new(filteredSourceDirectoryFile);
|
|
if (minimumDateTime != filteredSourceDirectoryFileInfo.CreationTime)
|
|
{
|
|
(isWrongYear, matches) = property.IsWrongYear(filteredSourceDirectoryFile, minimumDateTime);
|
|
if (isWrongYear is null || !isWrongYear.Value)
|
|
dateTime = minimumDateTime;
|
|
else
|
|
{
|
|
if (isWrongYear.HasValue && isWrongYear.Value)
|
|
_Log.Information($"Wrong Year? <{filteredSourceDirectoryFile}>");
|
|
if (!matches.Any())
|
|
continue;
|
|
if (!DateTime.TryParseExact(matches[0], "yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime))
|
|
continue;
|
|
}
|
|
try
|
|
{ File.SetCreationTime(filteredSourceDirectoryFile, dateTime); }
|
|
catch (Exception)
|
|
{ }
|
|
}
|
|
if (!_VerifyToSeason.Contains(group.SourceDirectory))
|
|
continue;
|
|
propertyFileInfo = group.PropertyFileInfoCollection[i];
|
|
if (propertyFileInfo is null)
|
|
continue;
|
|
sourceDirectorySegments = Path.GetFileName(group.SourceDirectory).Split(' ');
|
|
(season, seasonName) = Stateless.A_Property.GetSeason(minimumDateTime.DayOfYear);
|
|
if (sourceDirectorySegments[0] == "zzz")
|
|
destinationDirectory = Path.Combine(_Configuration.RootDirectory, $"zzz ={minimumDateTime:yyyy}.{season} {seasonName} {string.Join(' ', sourceDirectorySegments.Skip(3))}");
|
|
else if (sourceDirectorySegments.Length > 2)
|
|
destinationDirectory = Path.Combine(_Configuration.RootDirectory, $"={minimumDateTime:yyyy}.{season} {seasonName} {string.Join(' ', sourceDirectorySegments.Skip(2))}");
|
|
else
|
|
destinationDirectory = Path.Combine(_Configuration.RootDirectory, $"={minimumDateTime:yyyy}.{season} {seasonName}");
|
|
if (destinationDirectory == group.SourceDirectory)
|
|
continue;
|
|
if (!result)
|
|
result = true;
|
|
if (!Directory.Exists(destinationDirectory))
|
|
_ = Directory.CreateDirectory(destinationDirectory);
|
|
destinationFile = Path.Combine(destinationDirectory, filteredSourceDirectoryFileInfo.Name);
|
|
if (File.Exists(destinationFile))
|
|
{
|
|
if (destinationFile.EndsWith(".jpg", ignoreCase: true, CultureInfo.CurrentCulture))
|
|
destinationFile = Path.Combine(destinationDirectory, Path.ChangeExtension(filteredSourceDirectoryFileInfo.Name, ".jpeg"));
|
|
else if (destinationFile.EndsWith(".jpeg", ignoreCase: true, CultureInfo.CurrentCulture))
|
|
destinationFile = Path.Combine(destinationDirectory, Path.ChangeExtension(filteredSourceDirectoryFileInfo.Name, ".jpg"));
|
|
}
|
|
if (File.Exists(destinationFile))
|
|
{
|
|
_Log.Information($"*** source <{filteredSourceDirectoryFile}>");
|
|
_Log.Information($"*** destination <{destinationFile}>");
|
|
if (propertyFileInfo.Exists)
|
|
{
|
|
deleteFile = Path.ChangeExtension(propertyFileInfo.FullName, ".delete");
|
|
if (File.Exists(deleteFile))
|
|
File.Delete(deleteFile);
|
|
File.Move(propertyFileInfo.FullName, deleteFile);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
File.Move(filteredSourceDirectoryFile, destinationFile);
|
|
if (propertyFileInfo.Exists)
|
|
{
|
|
deleteFile = Path.ChangeExtension(propertyFileInfo.FullName, ".delete");
|
|
if (File.Exists(deleteFile))
|
|
File.Delete(deleteFile);
|
|
File.Move(propertyFileInfo.FullName, deleteFile);
|
|
}
|
|
}
|
|
}
|
|
if (directoryMaximumOfMinimumDateTime != DateTime.MinValue)
|
|
{
|
|
DirectoryInfo directoryInfo = new(group.SourceDirectory);
|
|
if (directoryInfo.LastWriteTime != directoryMaximumOfMinimumDateTime)
|
|
Directory.SetLastWriteTime(group.SourceDirectory, directoryMaximumOfMinimumDateTime);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private void WriteGroup(Group group, string angleBracket)
|
|
{
|
|
if (!(from l in @group.PropertyCollection where l?.Width is null select true).Any())
|
|
{
|
|
string key;
|
|
string json;
|
|
string checkFile;
|
|
A_Property? property;
|
|
string checkDirectory;
|
|
int sourceDirectoryLength = group.SourceDirectory.Length;
|
|
List<KeyValuePair<string, A_Property>> propertyCollectionKeyValuePairs = new();
|
|
JsonSerializerOptions writeIndentedJsonSerializerOptions = new() { WriteIndented = false };
|
|
(int level, List<string> directories) = IPath.Get(_Configuration.RootDirectory, group.SourceDirectory);
|
|
string fileName = string.Concat(string.Join(_Configuration.FileNameDirectorySeparator, directories), ".json");
|
|
for (int i = 0; i < group.FilteredSourceDirectoryFiles.Length; i++)
|
|
{
|
|
property = group.PropertyCollection[i];
|
|
if (property is null)
|
|
continue;
|
|
key = IPath.GetRelativePath(group.FilteredSourceDirectoryFiles[i], sourceDirectoryLength);
|
|
propertyCollectionKeyValuePairs.Add(new KeyValuePair<string, A_Property>(key, property));
|
|
}
|
|
checkDirectory = IPath.GetDirectory(angleBracket, level, "[{}]");
|
|
checkFile = Path.Combine(checkDirectory, fileName);
|
|
if (File.Exists(checkFile))
|
|
File.Move(checkFile, Path.Combine(checkDirectory, fileName));
|
|
checkFile = Path.Combine(checkDirectory, fileName);
|
|
json = JsonSerializer.Serialize(propertyCollectionKeyValuePairs, writeIndentedJsonSerializerOptions);
|
|
_ = IPath.WriteAllText(checkFile, json, compareBeforeWrite: true);
|
|
}
|
|
}
|
|
|
|
private (List<Tuple<string, DateTime>> filteredSourceDirectoryFileTuples, bool isValidImageFormatExtension, FileInfo propertyFileInfo, A_Property property) ParallelForWork(bool firstPass, string angleBracket, string sourceDirectory, string filteredSourceDirectoryFile)
|
|
{
|
|
List<string> parseExceptions = new();
|
|
List<Tuple<string, DateTime>> filteredSourceDirectoryFileTuples = new();
|
|
string extensionLowered = Path.GetExtension(filteredSourceDirectoryFile).ToLower();
|
|
bool isValidMetadataExtensions = _Configuration.ValidMetadataExtensions.Contains(extensionLowered);
|
|
bool isValidImageFormatExtension = _Configuration.ValidImageFormatExtensions.Contains(extensionLowered);
|
|
bool isIgnoreExtension = isValidImageFormatExtension && _Configuration.IgnoreExtensions.Contains(extensionLowered);
|
|
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filteredSourceDirectoryFile);
|
|
string filteredSourceDirectoryFileExtensionLowered = Path.Combine(sourceDirectory, $"{fileNameWithoutExtension}{extensionLowered}");
|
|
if (isValidImageFormatExtension && filteredSourceDirectoryFile.Length == filteredSourceDirectoryFileExtensionLowered.Length && filteredSourceDirectoryFile != filteredSourceDirectoryFileExtensionLowered)
|
|
File.Move(filteredSourceDirectoryFile, filteredSourceDirectoryFileExtensionLowered);
|
|
string without = Path.Combine(angleBracket.Replace("<>", "{}"), $"{fileNameWithoutExtension}.json");
|
|
FileInfo propertyFileInfo = new(Path.Combine(angleBracket.Replace("<>", "{}"), $"{fileNameWithoutExtension}{extensionLowered}.json"));
|
|
if (isValidImageFormatExtension && File.Exists(without))
|
|
{
|
|
File.Move(without, propertyFileInfo.FullName);
|
|
propertyFileInfo.Refresh();
|
|
}
|
|
A_Property property = GetProperty(filteredSourceDirectoryFileTuples, parseExceptions, firstPass, angleBracket, filteredSourceDirectoryFile, propertyFileInfo, isIgnoreExtension, isValidImageFormatExtension, isValidMetadataExtensions);
|
|
return new(filteredSourceDirectoryFileTuples, isValidImageFormatExtension, propertyFileInfo, property);
|
|
}
|
|
|
|
private int ParallelWork(bool firstPass, object @lock, long ticks, List<Tuple<string, DateTime>> sourceDirectoryChanges, int count, Group group, string angleBracket)
|
|
{
|
|
int result = 0;
|
|
if (_Log is null)
|
|
throw new Exception($"{nameof(_Log)} is null!");
|
|
ParallelOptions parallelOptions = new() { MaxDegreeOfParallelism = _MaxDegreeOfParallelism };
|
|
int totalSeconds = (int)Math.Truncate(new TimeSpan(DateTime.Now.Ticks - ticks).TotalSeconds);
|
|
ProgressBarOptions options = new() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true };
|
|
using (ProgressBar progressBar = new(group.FilteredSourceDirectoryFiles.Length, $"{group.G}) {group.R + 1:000} / {count:000} - {group.SourceDirectory} - {group.FilteredSourceDirectoryFiles.Length} file(s) - {totalSeconds} total second(s)", options))
|
|
{
|
|
_ = Parallel.For(0, group.FilteredSourceDirectoryFiles.Length, parallelOptions, i =>
|
|
{
|
|
try
|
|
{
|
|
long ticks = DateTime.Now.Ticks;
|
|
DateTime dateTime = DateTime.Now;
|
|
(List<Tuple<string, DateTime>> filteredSourceDirectoryFileTuples, bool isValidImageFormatExtension, FileInfo propertyFileInfo, A_Property property) = ParallelForWork(firstPass, angleBracket, group.SourceDirectory, group.FilteredSourceDirectoryFiles[i]);
|
|
progressBar.Tick();
|
|
if (_MaxDegreeOfParallelism < 2)
|
|
ticks = LogDelta(ticks, nameof(PropertyLogic.ParallelForWork));
|
|
lock (@lock)
|
|
{
|
|
group.PropertyCollection[i] = property;
|
|
group.PropertyFileInfoCollection[i] = propertyFileInfo;
|
|
if (isValidImageFormatExtension)
|
|
group.ValidImageFormatExtentionCollection[i] = isValidImageFormatExtension;
|
|
sourceDirectoryChanges.AddRange(from l in filteredSourceDirectoryFileTuples where l.Item2 > dateTime select l);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result += 1;
|
|
_Log.Error(string.Concat(group.SourceDirectory, Environment.NewLine, ex.Message, Environment.NewLine, ex.StackTrace), ex);
|
|
if (result == group.FilteredSourceDirectoryFiles.Length)
|
|
throw new Exception(string.Concat("All in [", group.SourceDirectory, "]failed!"));
|
|
}
|
|
});
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private string SetAngleBracketCollectionAndGetZero(Configuration configuration, string sourceDirectory)
|
|
{
|
|
string result;
|
|
AngleBracketCollection.Clear();
|
|
AngleBracketCollection.AddRange(IResult.GetDirectoryInfoCollection(configuration,
|
|
sourceDirectory,
|
|
nameof(A_Property),
|
|
string.Empty,
|
|
includeResizeGroup: false,
|
|
includeModel: false,
|
|
includePredictorModel: false,
|
|
contentDescription: string.Empty,
|
|
singletonDescription: "Properties for each image",
|
|
collectionDescription: string.Empty));
|
|
result = AngleBracketCollection[0];
|
|
return result;
|
|
}
|
|
|
|
public List<Group> GetParallelWork(Configuration configuration, List<string> topDirectories, List<(int g, string sourceDirectory, string[] sourceDirectoryFiles, int r)> groupCollection, bool firstPass, bool filterOnFirstPass)
|
|
{
|
|
List<Group> results = new();
|
|
if (_Log is null)
|
|
throw new Exception($"{nameof(_Log)} is null!");
|
|
if (_Configuration.PopulatePropertyId is null)
|
|
throw new Exception($"{nameof(_Configuration.PopulatePropertyId)} is null!");
|
|
Group group;
|
|
int exceptionCount;
|
|
string angleBracket;
|
|
object @lock = new();
|
|
long ticks = DateTime.Now.Ticks;
|
|
string[] filteredSourceDirectoryFiles;
|
|
List<Tuple<string, DateTime>> sourceDirectoryChanges = new();
|
|
string propertyRoot = IResult.GetResultsGroupDirectory(configuration, nameof(A_Property));
|
|
foreach ((int g, string sourceDirectory, string[] sourceDirectoryFiles, int r) in groupCollection)
|
|
{
|
|
if (!topDirectories.Any())
|
|
continue;
|
|
sourceDirectoryChanges.Clear();
|
|
if (firstPass && !filterOnFirstPass)
|
|
filteredSourceDirectoryFiles = sourceDirectoryFiles;
|
|
else
|
|
filteredSourceDirectoryFiles = (from l in sourceDirectoryFiles where !_Configuration.IgnoreExtensions.Contains(Path.GetExtension(l)) select l).ToArray();
|
|
if (!filteredSourceDirectoryFiles.Any())
|
|
continue;
|
|
group = new(g, sourceDirectory, filteredSourceDirectoryFiles, r);
|
|
angleBracket = SetAngleBracketCollectionAndGetZero(configuration, sourceDirectory);
|
|
exceptionCount = ParallelWork(firstPass, @lock, ticks, sourceDirectoryChanges, groupCollection.Count, group, angleBracket);
|
|
if (exceptionCount != 0)
|
|
_ExceptionsDirectories.Add(sourceDirectory);
|
|
else
|
|
results.Add(group);
|
|
bool? anyFilesMoved;
|
|
if (!firstPass || exceptionCount != 0)
|
|
anyFilesMoved = null;
|
|
else
|
|
anyFilesMoved = AnyFilesMoved(group);
|
|
if (exceptionCount != 0 || (anyFilesMoved is not null && anyFilesMoved.Value))
|
|
results.Clear();
|
|
if (exceptionCount == 0 && !firstPass && _Configuration.PopulatePropertyId.Value && (anyFilesMoved is null || !anyFilesMoved.Value))
|
|
WriteGroup(group, angleBracket);
|
|
if (Directory.GetFiles(propertyRoot, "*.txt", SearchOption.TopDirectoryOnly).Any())
|
|
{
|
|
for (int y = 0; y < int.MaxValue; y++)
|
|
{
|
|
_Log.Information("Press \"Y\" key when ready to continue or close console");
|
|
if (Console.ReadKey().Key == ConsoleKey.Y)
|
|
break;
|
|
}
|
|
_Log.Information(". . .");
|
|
}
|
|
}
|
|
return results;
|
|
}
|
|
|
|
public List<string> DoWork(Configuration configuration, List<string> topDirectories, List<(int g, string sourceDirectory, string[] sourceDirectoryFiles, int r)> groupCollection, bool firstPass)
|
|
{
|
|
List<string> results = new();
|
|
_ExceptionsDirectories.Clear();
|
|
_ = GetParallelWork(configuration, topDirectories, groupCollection, firstPass, filterOnFirstPass: false);
|
|
results.AddRange(_ExceptionsDirectories);
|
|
return results;
|
|
}
|
|
|
|
public A_Property GetProperty(string angleBracket, string sourceDirectory, string filteredSourceDirectoryFile, List<Tuple<string, DateTime>> filteredSourceDirectoryFileTuples, List<string> parseExceptions, FileInfo fileInfo)
|
|
{
|
|
A_Property result;
|
|
bool firstPass = false;
|
|
string extensionLowered = Path.GetExtension(filteredSourceDirectoryFile).ToLower();
|
|
bool isValidMetadataExtensions = _Configuration.ValidMetadataExtensions.Contains(extensionLowered);
|
|
bool isValidImageFormatExtension = _Configuration.ValidImageFormatExtensions.Contains(extensionLowered);
|
|
bool isIgnoreExtension = isValidImageFormatExtension && _Configuration.IgnoreExtensions.Contains(extensionLowered);
|
|
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filteredSourceDirectoryFile);
|
|
string filteredSourceDirectoryFileExtensionLowered = Path.Combine(sourceDirectory, $"{fileNameWithoutExtension}{extensionLowered}");
|
|
if (isValidImageFormatExtension && filteredSourceDirectoryFile.Length == filteredSourceDirectoryFileExtensionLowered.Length && filteredSourceDirectoryFile != filteredSourceDirectoryFileExtensionLowered)
|
|
File.Move(filteredSourceDirectoryFile, filteredSourceDirectoryFileExtensionLowered);
|
|
string without = Path.Combine(angleBracket.Replace("<>", "{}"), $"{fileNameWithoutExtension}.json");
|
|
FileInfo propertyFileInfo = new(Path.Combine(angleBracket.Replace("<>", "{}"), $"{fileNameWithoutExtension}{extensionLowered}.json"));
|
|
if (isValidImageFormatExtension && File.Exists(without))
|
|
{
|
|
File.Move(without, propertyFileInfo.FullName);
|
|
propertyFileInfo.Refresh();
|
|
}
|
|
result = GetProperty(filteredSourceDirectoryFileTuples, parseExceptions, firstPass, angleBracket, filteredSourceDirectoryFile, fileInfo, isIgnoreExtension, isValidImageFormatExtension, isValidMetadataExtensions);
|
|
return result;
|
|
}
|
|
|
|
public (long Ticks, string FilteredSourceDirectoryFile, string PropertyDirectory, int PropertyId)[] GetPropertyIds(Configuration configuration, List<Group> groupCollection, bool saveToCollection)
|
|
{
|
|
List<(long Ticks, string FilteredSourceDirectoryFile, string PropertyDirectory, int PropertyId)> results = new();
|
|
int level;
|
|
string angleBracket;
|
|
A_Property? property;
|
|
string checkDirectory;
|
|
List<string> directories;
|
|
string propertyDirectory;
|
|
foreach (Group group in groupCollection)
|
|
{
|
|
angleBracket = SetAngleBracketCollectionAndGetZero(configuration, group.SourceDirectory);
|
|
if (string.IsNullOrEmpty(group.SourceDirectory))
|
|
throw new Exception();
|
|
if (!saveToCollection)
|
|
propertyDirectory = angleBracket.Replace("<>", "()");
|
|
else
|
|
{
|
|
(level, directories) = IPath.Get(_Configuration.RootDirectory, group.SourceDirectory);
|
|
checkDirectory = IPath.GetDirectory(angleBracket, level, "[()]");
|
|
propertyDirectory = Path.Combine(checkDirectory, string.Join(_Configuration.FileNameDirectorySeparator, directories));
|
|
}
|
|
if (!Directory.Exists(propertyDirectory))
|
|
_ = Directory.CreateDirectory(propertyDirectory);
|
|
for (int i = 0; i < group.FilteredSourceDirectoryFiles.Length; i++)
|
|
{
|
|
property = group.PropertyCollection[i];
|
|
if (property?.Id is null)
|
|
continue;
|
|
results.Add(new(property.GetDateTimes().Min().Ticks, group.FilteredSourceDirectoryFiles[i], propertyDirectory, property.Id.Value));
|
|
}
|
|
}
|
|
return results.OrderBy(l => l.Ticks).ToArray();
|
|
}
|
|
|
|
} |