Re-write
This commit is contained in:
@ -1,84 +1,62 @@
|
||||
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 System.Text.Json.Serialization;
|
||||
using View_by_Distance.Shared.Models.Methods;
|
||||
using View_by_Distance.Property.Models.Stateless;
|
||||
using View_by_Distance.Shared.Models;
|
||||
using View_by_Distance.Shared.Models.Properties;
|
||||
using View_by_Distance.Shared.Models.Stateless;
|
||||
|
||||
namespace View_by_Distance.Property.Models;
|
||||
|
||||
/// <summary>
|
||||
// A_Property
|
||||
/// </summary>
|
||||
public class A_Property : Shared.Models.Properties.IProperty, IProperty
|
||||
public class A_Property
|
||||
{
|
||||
|
||||
protected DateTime _CreationTime;
|
||||
protected DateTime? _DateTime;
|
||||
protected DateTime? _DateTimeDigitized;
|
||||
protected DateTime? _DateTimeOriginal;
|
||||
protected long _FileSize;
|
||||
protected DateTime? _GPSDateStamp;
|
||||
protected int? _Height;
|
||||
protected int? _Id;
|
||||
protected int[] _Indices;
|
||||
protected DateTime _LastWriteTime;
|
||||
protected string _Make;
|
||||
protected string _Model;
|
||||
protected string _Orientation;
|
||||
protected int? _Width;
|
||||
public DateTime CreationTime => _CreationTime;
|
||||
public DateTime? DateTime => _DateTime;
|
||||
public DateTime? DateTimeDigitized => _DateTimeDigitized;
|
||||
public DateTime? DateTimeOriginal => _DateTimeOriginal;
|
||||
public long FileSize => _FileSize;
|
||||
public DateTime? GPSDateStamp => _GPSDateStamp;
|
||||
public int? Height => _Height;
|
||||
public int? Id => _Id;
|
||||
public int[] Indices => _Indices;
|
||||
public DateTime LastWriteTime => _LastWriteTime;
|
||||
public string Make => _Make;
|
||||
public string Model => _Model;
|
||||
public string Orientation => _Orientation;
|
||||
public int? Width => _Width;
|
||||
protected readonly List<string> _ExceptionsDirectories;
|
||||
protected readonly Dictionary<int, int[]> _KeyValuePairs;
|
||||
protected readonly Dictionary<int, int[]> _IndicesFromNew;
|
||||
|
||||
[JsonConstructor]
|
||||
public A_Property(DateTime creationTime, DateTime? dateTime, DateTime? dateTimeDigitized, DateTime? dateTimeOriginal, long fileSize, DateTime? gpsDateStamp, int? height, int? id, int[] indices, DateTime lastWriteTime, string make, string model, string orientation, int? width)
|
||||
public bool Reverse { get; }
|
||||
public List<string> AngleBracketCollection { get; }
|
||||
public List<string> ExceptionsDirectories => _ExceptionsDirectories;
|
||||
|
||||
private readonly Model? _Model;
|
||||
private readonly Serilog.ILogger? _Log;
|
||||
private readonly string _OutputExtension;
|
||||
private readonly string[] _VerifyToSeason;
|
||||
private readonly int _MaxDegreeOfParallelism;
|
||||
private readonly ASCIIEncoding _ASCIIEncoding;
|
||||
private readonly Configuration _Configuration;
|
||||
private readonly PredictorModel? _PredictorModel;
|
||||
private readonly JsonSerializerOptions _WriteIndentedJsonSerializerOptions;
|
||||
|
||||
public A_Property(int maxDegreeOfParallelism, Configuration configuration, string outputExtension, bool reverse, Model? model, PredictorModel? predictorModel, Dictionary<int, int[]> indicesFromNew, Dictionary<int, int[]> keyValuePairs)
|
||||
{
|
||||
_CreationTime = creationTime;
|
||||
_DateTime = dateTime;
|
||||
_DateTimeDigitized = dateTimeDigitized;
|
||||
_DateTimeOriginal = dateTimeOriginal;
|
||||
_FileSize = fileSize;
|
||||
_GPSDateStamp = gpsDateStamp;
|
||||
_Height = height;
|
||||
_Id = id;
|
||||
_Indices = indices;
|
||||
_LastWriteTime = lastWriteTime;
|
||||
_Make = make;
|
||||
_Model = model;
|
||||
_Orientation = orientation;
|
||||
_Width = width;
|
||||
Reverse = reverse;
|
||||
_KeyValuePairs = keyValuePairs;
|
||||
_Configuration = configuration;
|
||||
_ExceptionsDirectories = new();
|
||||
_IndicesFromNew = indicesFromNew;
|
||||
_PredictorModel = predictorModel;
|
||||
_OutputExtension = outputExtension;
|
||||
_ASCIIEncoding = new ASCIIEncoding();
|
||||
AngleBracketCollection = new List<string>();
|
||||
_Log = Serilog.Log.ForContext<A_Property>();
|
||||
_MaxDegreeOfParallelism = maxDegreeOfParallelism;
|
||||
_WriteIndentedJsonSerializerOptions = new JsonSerializerOptions { WriteIndented = true };
|
||||
if (configuration.VerifyToSeason is null || !configuration.VerifyToSeason.Any())
|
||||
throw new Exception();
|
||||
_VerifyToSeason = configuration.VerifyToSeason.Select(l => Path.Combine(configuration.RootDirectory, l)).ToArray();
|
||||
}
|
||||
|
||||
#nullable disable
|
||||
|
||||
public A_Property()
|
||||
{
|
||||
_CreationTime = System.DateTime.MinValue;
|
||||
_DateTime = null;
|
||||
_DateTimeDigitized = null;
|
||||
_DateTimeOriginal = null;
|
||||
_FileSize = long.MinValue;
|
||||
_GPSDateStamp = null;
|
||||
_Height = null;
|
||||
_Id = null;
|
||||
_Indices = Array.Empty<int>();
|
||||
_LastWriteTime = System.DateTime.MinValue;
|
||||
_Make = string.Empty;
|
||||
_Model = string.Empty;
|
||||
_Orientation = string.Empty;
|
||||
_Width = null;
|
||||
}
|
||||
|
||||
#nullable restore
|
||||
public A_Property(int maxDegreeOfParallelism, Configuration configuration, string outputExtension, bool reverse, Model? model, PredictorModel? predictorModel) :
|
||||
this(maxDegreeOfParallelism, configuration, outputExtension, reverse, model, predictorModel, new(), new())
|
||||
{ }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
@ -86,38 +64,658 @@ public class A_Property : Shared.Models.Properties.IProperty, IProperty
|
||||
return result;
|
||||
}
|
||||
|
||||
public List<DateTime> GetDateTimes() => Stateless.A_Property.GetDateTimes(_CreationTime, _LastWriteTime, _DateTime, _DateTimeDigitized, _DateTimeOriginal, _GPSDateStamp);
|
||||
|
||||
public (bool?, string[]) IsWrongYear(string filteredSourceDirectoryFile, DateTime? minimumDateTime)
|
||||
private long LogDelta(long ticks, string? methodName)
|
||||
{
|
||||
string[] results = Array.Empty<string>();
|
||||
bool? result = null;
|
||||
string year;
|
||||
string directoryName;
|
||||
string[] directorySegments;
|
||||
string? check = Path.GetFullPath(filteredSourceDirectoryFile);
|
||||
string? pathRoot = Path.GetPathRoot(filteredSourceDirectoryFile);
|
||||
if (string.IsNullOrEmpty(pathRoot))
|
||||
throw new Exception();
|
||||
if (minimumDateTime.HasValue)
|
||||
year = minimumDateTime.Value.ToString("yyyy");
|
||||
long result;
|
||||
if (_Log is null)
|
||||
throw new NullReferenceException(nameof(_Log));
|
||||
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;
|
||||
}
|
||||
|
||||
public static List<DateTime> GetMetadataDateTimesByPattern(string dateTimeFormat, IFileHolder filteredSourceDirectoryFileHolder)
|
||||
{
|
||||
List<DateTime> results = GetMetadataDateTimesByPattern(dateTimeFormat, filteredSourceDirectoryFileHolder.FullName);
|
||||
return results;
|
||||
}
|
||||
|
||||
#pragma warning disable CA1416
|
||||
|
||||
private Shared.Models.Property GetImageProperty(IFileHolder filteredSourceDirectoryFileHolder, bool populateId, bool isIgnoreExtension, bool isValidImageFormatExtension, bool isValidMetadataExtensions, int? id, List<int> indices)
|
||||
{
|
||||
Shared.Models.Property result;
|
||||
if (_Log is null)
|
||||
throw new NullReferenceException(nameof(_Log));
|
||||
long ticks;
|
||||
byte[] bytes;
|
||||
string value;
|
||||
long fileLength;
|
||||
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;
|
||||
if (!isValidImageFormatExtension && isValidMetadataExtensions)
|
||||
{
|
||||
dateTimeFormat = "ddd MMM dd HH:mm:ss yyyy";
|
||||
List<DateTime> dateTimes = GetMetadataDateTimesByPattern(dateTimeFormat, filteredSourceDirectoryFileHolder);
|
||||
if (dateTimes.Any())
|
||||
dateTimeOriginal = dateTimes.Min();
|
||||
}
|
||||
else if (!isIgnoreExtension && isValidImageFormatExtension)
|
||||
{
|
||||
if (populateId && (id is null || !indices.Any()) && !_IndicesFromNew.Any() && !_KeyValuePairs.Any())
|
||||
throw new Exception("In order to keep six character indices at least one need to have an item!");
|
||||
try
|
||||
{
|
||||
using Image image = Image.FromFile(filteredSourceDirectoryFileHolder.FullName);
|
||||
if (populateId && (id is null || !indices.Any()))
|
||||
{
|
||||
using Bitmap bitmap = new(image);
|
||||
string angleBracket = AngleBracketCollection[0];
|
||||
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 = Shared.Models.Stateless.Methods.IProperty.GetDeterministicHashCode(bytes);
|
||||
if (_MaxDegreeOfParallelism < 2)
|
||||
ticks = LogDelta(ticks, nameof(Shared.Models.Stateless.Methods.IProperty.GetDeterministicHashCode));
|
||||
}
|
||||
if (_Configuration.WriteBitmapDataBytes)
|
||||
{
|
||||
FileInfo contentFileInfo = new(Path.Combine(angleBracket.Replace("<>", "()"), filteredSourceDirectoryFileHolder.Name));
|
||||
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 = Shared.Models.Stateless.Methods.IProperty.GetDeterministicHashCode(encoding);
|
||||
if (_MaxDegreeOfParallelism < 2)
|
||||
ticks = LogDelta(ticks, nameof(Shared.Models.Stateless.Methods.IProperty.GetDeterministicHashCode));
|
||||
if (!_KeyValuePairs.ContainsKey(encodingHash))
|
||||
indices.Add(encodingHash);
|
||||
else
|
||||
indices.AddRange(_KeyValuePairs[encodingHash]);
|
||||
}
|
||||
}
|
||||
width = image.Width;
|
||||
height = image.Height;
|
||||
dateTimeFormat = Shared.Models.Stateless.Methods.IProperty.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, " <", filteredSourceDirectoryFileHolder.Name, ">"));
|
||||
}
|
||||
}
|
||||
else
|
||||
dateTimeOriginal = null;
|
||||
if (filteredSourceDirectoryFileHolder.Length is null)
|
||||
fileLength = 0;
|
||||
else
|
||||
fileLength = filteredSourceDirectoryFileHolder.Length.Value;
|
||||
result = new(filteredSourceDirectoryFileHolder.CreationTime, dateTime, dateTimeDigitized, dateTimeOriginal, fileLength, gpsDateStamp, height, id, indices.ToArray(), filteredSourceDirectoryFileHolder.LastWriteTime, make, model, orientation, width);
|
||||
return result;
|
||||
}
|
||||
|
||||
#pragma warning restore CA1416
|
||||
|
||||
private Shared.Models.Property GetPropertyOfPrivate(Item item, bool firstPass, List<Tuple<string, DateTime>> filteredSourceDirectoryFileTuples, List<string> parseExceptions, bool isIgnoreExtension, bool isValidMetadataExtensions)
|
||||
{
|
||||
Shared.Models.Property? result;
|
||||
if (item.ImageFileHolder is null)
|
||||
throw new NullReferenceException(nameof(item.ImageFileHolder));
|
||||
string json;
|
||||
int? id = null;
|
||||
List<int> indices = new();
|
||||
bool hasWrongYearProperty = false;
|
||||
string[] changesFrom = Array.Empty<string>();
|
||||
string angleBracket = AngleBracketCollection[0];
|
||||
bool populateId = !firstPass && _Configuration.PopulatePropertyId;
|
||||
string without = Path.Combine(angleBracket.Replace("<>", "{}"), $"{item.ImageFileHolder.NameWithoutExtension}.json");
|
||||
FileInfo fileInfo = new(Path.Combine(angleBracket.Replace("<>", "{}"), $"{item.ImageFileHolder.NameWithoutExtension}{item.ImageFileHolder.ExtensionLowered}.json"));
|
||||
if (item.ValidImageFormatExtension && File.Exists(without))
|
||||
{
|
||||
File.Move(without, fileInfo.FullName);
|
||||
fileInfo.Refresh();
|
||||
}
|
||||
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 && !fileInfo.Exists && File.Exists(Path.ChangeExtension(fileInfo.FullName, ".delete")))
|
||||
{
|
||||
File.Move(Path.ChangeExtension(fileInfo.FullName, ".delete"), fileInfo.FullName);
|
||||
fileInfo.Refresh();
|
||||
}
|
||||
if (_Configuration.ForcePropertyLastWriteTimeToCreationTime && fileInfo.Exists && fileInfo.LastWriteTime != fileInfo.CreationTime)
|
||||
{
|
||||
File.SetLastWriteTime(fileInfo.FullName, fileInfo.CreationTime);
|
||||
fileInfo.Refresh();
|
||||
}
|
||||
if (_Configuration.PropertiesChangedForProperty)
|
||||
result = null;
|
||||
else if (!fileInfo.Exists)
|
||||
result = null;
|
||||
else if (!fileInfo.FullName.EndsWith(".json") && !fileInfo.FullName.EndsWith(".old"))
|
||||
throw new ArgumentException("must be a *.json file");
|
||||
else if (dateTimes.Any() && dateTimes.Max() > fileInfo.LastWriteTime)
|
||||
result = null;
|
||||
else
|
||||
{
|
||||
List<DateTime> dateTimes = GetDateTimes();
|
||||
year = dateTimes.Min().ToString("yyyy");
|
||||
json = File.ReadAllText(fileInfo.FullName);
|
||||
try
|
||||
{
|
||||
if (item.ImageFileHolder is null)
|
||||
throw new NullReferenceException(nameof(item.ImageFileHolder));
|
||||
bool check = true;
|
||||
Shared.Models.Property? property = JsonSerializer.Deserialize<Shared.Models.Property>(json);
|
||||
if (!isIgnoreExtension && item.ValidImageFormatExtension && ((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(item.ImageFileHolder, populateId, isIgnoreExtension, item.ValidImageFormatExtension, isValidMetadataExtensions, id, indices);
|
||||
}
|
||||
if (!isIgnoreExtension && item.ValidImageFormatExtension && 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(item.ImageFileHolder, populateId, isIgnoreExtension, item.ValidImageFormatExtension, isValidMetadataExtensions, id, indices);
|
||||
}
|
||||
if (!isIgnoreExtension && item.ValidImageFormatExtension && populateId && property is not null && property.LastWriteTime != item.ImageFileHolder.LastWriteTime)
|
||||
{
|
||||
check = false;
|
||||
id = null;
|
||||
indices.Clear();
|
||||
property = GetImageProperty(item.ImageFileHolder, populateId, isIgnoreExtension, item.ValidImageFormatExtension, isValidMetadataExtensions, id, indices);
|
||||
}
|
||||
if (!isIgnoreExtension && item.ValidImageFormatExtension && property?.Width is not null && property?.Height is not null && property.Width.Value == property.Height.Value && item.ImageFileHolder.Exists)
|
||||
{
|
||||
check = false;
|
||||
id = property?.Id;
|
||||
if (property is not null && property.Indices.Any())
|
||||
indices = property.Indices.ToList();
|
||||
property = GetImageProperty(item.ImageFileHolder, populateId, isIgnoreExtension, item.ValidImageFormatExtension, 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 (filteredSourceDirectoryFileFileInfo.CreationTime != property?.CreationTime || filteredSourceDirectoryFileFileInfo.LastWriteTime != property?.LastWriteTime)
|
||||
// {
|
||||
// check = false;
|
||||
// id = null;
|
||||
// indices.Clear();
|
||||
// property = GetImagePropertyB(filteredSourceDirectoryFile, populateId, isIgnoreExtension, isValidImageFormatExtension, isValidMetadataExtensions, id, indices);
|
||||
// }
|
||||
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));
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < int.MaxValue; i++)
|
||||
if (result is null)
|
||||
{
|
||||
check = Path.GetDirectoryName(check);
|
||||
if (string.IsNullOrEmpty(check) || check == pathRoot)
|
||||
break;
|
||||
directoryName = Path.GetFileName(check);
|
||||
directorySegments = directoryName.Split(' ');
|
||||
(result, results) = Stateless.A_Property.IsWrongYear(directorySegments, year);
|
||||
if (result.HasValue)
|
||||
break;
|
||||
if (item.ImageFileHolder is null)
|
||||
throw new NullReferenceException(nameof(item.ImageFileHolder));
|
||||
result = GetImageProperty(item.ImageFileHolder, populateId, isIgnoreExtension, item.ValidImageFormatExtension, isValidMetadataExtensions, id, indices);
|
||||
json = JsonSerializer.Serialize(result, _WriteIndentedJsonSerializerOptions);
|
||||
if (populateId && Shared.Models.Stateless.Methods.IPath.WriteAllText(fileInfo.FullName, json, updateDateWhenMatches: true, compareBeforeWrite: true))
|
||||
{
|
||||
if (!_Configuration.ForcePropertyLastWriteTimeToCreationTime && (!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));
|
||||
}
|
||||
}
|
||||
}
|
||||
return new(result, results);
|
||||
else if (hasWrongYearProperty)
|
||||
{
|
||||
json = JsonSerializer.Serialize(result, _WriteIndentedJsonSerializerOptions);
|
||||
if (Shared.Models.Stateless.Methods.IPath.WriteAllText(fileInfo.FullName, json, updateDateWhenMatches: true, 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(string sourceDirectory, Item[] filteredItems)
|
||||
{
|
||||
bool result = false;
|
||||
if (_Log is null)
|
||||
throw new NullReferenceException(nameof(_Log));
|
||||
int season;
|
||||
string[] matches;
|
||||
string deleteFile;
|
||||
bool? isWrongYear;
|
||||
string seasonName;
|
||||
DateTime dateTime;
|
||||
string destinationFile;
|
||||
DateTime minimumDateTime;
|
||||
string destinationDirectory;
|
||||
string[] sourceDirectorySegments;
|
||||
DateTime directoryMaximumOfMinimumDateTime = DateTime.MinValue;
|
||||
foreach (Item filteredItem in filteredItems)
|
||||
{
|
||||
if (!filteredItem.ValidImageFormatExtension || filteredItem.Property is null || filteredItem.ImageFileHolder is null)
|
||||
continue;
|
||||
minimumDateTime = Shared.Models.Stateless.Methods.IProperty.GetMinimumDateTime(filteredItem.Property);
|
||||
if (minimumDateTime > directoryMaximumOfMinimumDateTime)
|
||||
directoryMaximumOfMinimumDateTime = minimumDateTime;
|
||||
if (minimumDateTime != filteredItem.ImageFileHolder.CreationTime)
|
||||
{
|
||||
(isWrongYear, matches) = Shared.Models.Stateless.Methods.IProperty.IsWrongYear(filteredItem.ImageFileHolder.FullName, minimumDateTime);
|
||||
if (isWrongYear is null || !isWrongYear.Value)
|
||||
dateTime = minimumDateTime;
|
||||
else
|
||||
{
|
||||
if (!matches.Any())
|
||||
continue;
|
||||
if (!DateTime.TryParseExact(matches[0], "yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime))
|
||||
continue;
|
||||
}
|
||||
try
|
||||
{ File.SetCreationTime(filteredItem.ImageFileHolder.FullName, dateTime); }
|
||||
catch (Exception)
|
||||
{ }
|
||||
}
|
||||
if (!_VerifyToSeason.Contains(sourceDirectory))
|
||||
continue;
|
||||
if (!filteredItem.ImageFileHolder.FullName.Contains("zzz ") && !filteredItem.ImageFileHolder.FullName.Contains("Camera ") && filteredItem.Property.DateTimeOriginal.HasValue)
|
||||
{
|
||||
TimeSpan timeSpan = new(filteredItem.Property.DateTimeOriginal.Value.Ticks - filteredItem.Property.LastWriteTime.Ticks);
|
||||
if (timeSpan.TotalHours > 6)
|
||||
{
|
||||
_Log.Warning($"*** propertyHolder.FileInfo.FullName <{filteredItem.ImageFileHolder.FullName}>");
|
||||
_Log.Warning($"*** DateTimeOriginal <{filteredItem.Property.DateTimeOriginal.Value}>");
|
||||
_Log.Warning($"*** LastWriteTime <{filteredItem.Property.LastWriteTime}>");
|
||||
_Log.Warning($"*** TotalHours <{timeSpan.TotalHours}>");
|
||||
}
|
||||
}
|
||||
sourceDirectorySegments = Path.GetFileName(sourceDirectory).Split(' ');
|
||||
(season, seasonName) = Shared.Models.Stateless.Methods.IProperty.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 == sourceDirectory)
|
||||
continue;
|
||||
lock (filteredItem)
|
||||
filteredItem.SetMoved(true);
|
||||
if (!result)
|
||||
result = true;
|
||||
if (!Directory.Exists(destinationDirectory))
|
||||
_ = Directory.CreateDirectory(destinationDirectory);
|
||||
destinationFile = Path.Combine(destinationDirectory, filteredItem.ImageFileHolder.Name);
|
||||
if (File.Exists(destinationFile))
|
||||
{
|
||||
if (_OutputExtension is not ".jpg" and not ".jpeg")
|
||||
throw new Exception();
|
||||
if (destinationFile.EndsWith(".jpg", ignoreCase: true, CultureInfo.CurrentCulture))
|
||||
destinationFile = Path.Combine(destinationDirectory, Path.ChangeExtension(filteredItem.ImageFileHolder.Name, ".jpeg"));
|
||||
else if (destinationFile.EndsWith(".jpeg", ignoreCase: true, CultureInfo.CurrentCulture))
|
||||
destinationFile = Path.Combine(destinationDirectory, Path.ChangeExtension(filteredItem.ImageFileHolder.Name, ".jpg"));
|
||||
}
|
||||
if (File.Exists(destinationFile))
|
||||
{
|
||||
_Log.Information($"*** source <{filteredItem.ImageFileHolder.FullName}>");
|
||||
_Log.Information($"*** destination <{destinationFile}>");
|
||||
if (filteredItem.ImageFileHolder.Exists)
|
||||
{
|
||||
deleteFile = Path.ChangeExtension(filteredItem.ImageFileHolder.FullName, ".delete");
|
||||
if (File.Exists(deleteFile))
|
||||
File.Delete(deleteFile);
|
||||
File.Move(filteredItem.ImageFileHolder.FullName, deleteFile);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
File.Move(filteredItem.ImageFileHolder.FullName, destinationFile);
|
||||
if (filteredItem.ImageFileHolder.Exists)
|
||||
{
|
||||
deleteFile = Path.ChangeExtension(filteredItem.ImageFileHolder.FullName, ".delete");
|
||||
if (File.Exists(deleteFile))
|
||||
File.Delete(deleteFile);
|
||||
File.Move(filteredItem.ImageFileHolder.FullName, deleteFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (directoryMaximumOfMinimumDateTime != DateTime.MinValue)
|
||||
{
|
||||
System.IO.DirectoryInfo directoryInfo = new(sourceDirectory);
|
||||
if (directoryInfo.LastWriteTime != directoryMaximumOfMinimumDateTime)
|
||||
Directory.SetLastWriteTime(sourceDirectory, directoryMaximumOfMinimumDateTime);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void ParallelForWork(bool firstPass, string sourceDirectory, List<Tuple<string, DateTime>> filteredSourceDirectoryFileTuples, List<Tuple<string, DateTime>> sourceDirectoryChanges, Item item)
|
||||
{
|
||||
if (item.ImageFileHolder is null)
|
||||
throw new NullReferenceException(nameof(item.ImageFileHolder));
|
||||
Shared.Models.Property property;
|
||||
List<string> parseExceptions = new();
|
||||
bool isValidMetadataExtensions = _Configuration.ValidMetadataExtensions.Contains(item.ImageFileHolder.ExtensionLowered);
|
||||
bool isIgnoreExtension = item.ValidImageFormatExtension && _Configuration.IgnoreExtensions.Contains(item.ImageFileHolder.ExtensionLowered);
|
||||
string filteredSourceDirectoryFileExtensionLowered = Path.Combine(sourceDirectory, $"{item.ImageFileHolder.NameWithoutExtension}{item.ImageFileHolder.ExtensionLowered}");
|
||||
if (item.ValidImageFormatExtension && item.ImageFileHolder.FullName.Length == filteredSourceDirectoryFileExtensionLowered.Length && item.ImageFileHolder.FullName != filteredSourceDirectoryFileExtensionLowered)
|
||||
File.Move(item.ImageFileHolder.FullName, filteredSourceDirectoryFileExtensionLowered);
|
||||
if (item.Changed is null || item.Changed.Value || item.Property is null)
|
||||
{
|
||||
property = GetPropertyOfPrivate(item, firstPass, filteredSourceDirectoryFileTuples, parseExceptions, isIgnoreExtension, isValidMetadataExtensions);
|
||||
lock (sourceDirectoryChanges)
|
||||
sourceDirectoryChanges.Add(new Tuple<string, DateTime>(nameof(A_Property), DateTime.Now));
|
||||
lock (item)
|
||||
item.Update(property);
|
||||
}
|
||||
}
|
||||
|
||||
private void ParallelWork(bool firstPass, List<Exception> exceptions, List<Tuple<string, DateTime>> sourceDirectoryChanges, int containersCount, Shared.Models.Container container, Item[] filteredItems, int totalSeconds)
|
||||
{
|
||||
List<Tuple<string, DateTime>> filteredSourceDirectoryFileTuples = new();
|
||||
ParallelOptions parallelOptions = new() { MaxDegreeOfParallelism = _MaxDegreeOfParallelism };
|
||||
ProgressBarOptions options = new() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true };
|
||||
string message = $"{container.R:000}.{container.G} / {containersCount:000}) {filteredItems.Length:000} file(s) - {totalSeconds} total second(s) - {container.SourceDirectory}";
|
||||
using ProgressBar progressBar = new(filteredItems.Length, message, options);
|
||||
_ = Parallel.For(0, filteredItems.Length, parallelOptions, i =>
|
||||
{
|
||||
try
|
||||
{
|
||||
long ticks = DateTime.Now.Ticks;
|
||||
DateTime dateTime = DateTime.Now;
|
||||
List<Tuple<string, DateTime>> collection;
|
||||
ParallelForWork(firstPass, container.SourceDirectory, sourceDirectoryChanges, filteredSourceDirectoryFileTuples, filteredItems[i]);
|
||||
if (i == 0 || sourceDirectoryChanges.Any())
|
||||
progressBar.Tick();
|
||||
lock (filteredSourceDirectoryFileTuples)
|
||||
collection = (from l in filteredSourceDirectoryFileTuples where l.Item2 > dateTime select l).ToList();
|
||||
lock (sourceDirectoryChanges)
|
||||
sourceDirectoryChanges.AddRange(collection);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
lock (exceptions)
|
||||
exceptions.Add(ex);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void SetAngleBracketCollection(string sourceDirectory)
|
||||
{
|
||||
AngleBracketCollection.Clear();
|
||||
AngleBracketCollection.AddRange(IResult.GetDirectoryInfoCollection(_Configuration,
|
||||
_Model,
|
||||
_PredictorModel,
|
||||
sourceDirectory,
|
||||
nameof(A_Property),
|
||||
string.Empty,
|
||||
includeResizeGroup: false,
|
||||
includeModel: false,
|
||||
includePredictorModel: false,
|
||||
contentDescription: string.Empty,
|
||||
singletonDescription: "Properties for each image",
|
||||
collectionDescription: string.Empty));
|
||||
}
|
||||
|
||||
public void ParallelWork(long ticks, List<Shared.Models.Container> containers, bool firstPass)
|
||||
{
|
||||
if (_Log is null)
|
||||
throw new NullReferenceException(nameof(_Log));
|
||||
int totalSeconds;
|
||||
bool? anyFilesMoved;
|
||||
Item[] filteredItems;
|
||||
List<Exception> exceptions = new();
|
||||
int containersCount = containers.Count;
|
||||
List<Tuple<string, DateTime>> sourceDirectoryChanges = new();
|
||||
string propertyRoot = IResult.GetResultsGroupDirectory(_Configuration, nameof(A_Property));
|
||||
foreach (Shared.Models.Container container in containers)
|
||||
{
|
||||
if (!container.Items.Any())
|
||||
continue;
|
||||
sourceDirectoryChanges.Clear();
|
||||
if (firstPass)
|
||||
filteredItems = (from l in container.Items where l.NoJson is null || !l.NoJson.Value && (l.Changed is null || l.Changed.Value) select l).ToArray();
|
||||
else
|
||||
filteredItems = (from l in container.Items where l.ImageFileHolder is not null && !_Configuration.IgnoreExtensions.Contains(l.ImageFileHolder.ExtensionLowered) select l).ToArray();
|
||||
if (!filteredItems.Any())
|
||||
continue;
|
||||
totalSeconds = (int)Math.Truncate(new TimeSpan(DateTime.Now.Ticks - ticks).TotalSeconds);
|
||||
SetAngleBracketCollection(container.SourceDirectory);
|
||||
ParallelWork(firstPass, exceptions, sourceDirectoryChanges, containersCount, container, filteredItems, totalSeconds);
|
||||
foreach (Exception exception in exceptions)
|
||||
_Log.Error(string.Concat(container.SourceDirectory, Environment.NewLine, exception.Message, Environment.NewLine, exception.StackTrace), exception);
|
||||
if (exceptions.Count == filteredItems.Length)
|
||||
throw new Exception(string.Concat("All in [", container.SourceDirectory, "]failed!"));
|
||||
if (exceptions.Count != 0)
|
||||
_ExceptionsDirectories.Add(container.SourceDirectory);
|
||||
if (!firstPass || exceptions.Count != 0)
|
||||
anyFilesMoved = null;
|
||||
else
|
||||
anyFilesMoved = AnyFilesMoved(container.SourceDirectory, filteredItems);
|
||||
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 (System.Console.ReadKey().Key == ConsoleKey.Y)
|
||||
break;
|
||||
}
|
||||
_Log.Information(". . .");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Shared.Models.Property GetProperty(Item item, List<Tuple<string, DateTime>> filteredSourceDirectoryFileTuples, List<string> parseExceptions)
|
||||
{
|
||||
Shared.Models.Property result;
|
||||
if (item.ImageFileHolder is null)
|
||||
throw new NullReferenceException(nameof(item.ImageFileHolder));
|
||||
bool firstPass = false;
|
||||
bool isValidMetadataExtensions = _Configuration.ValidMetadataExtensions.Contains(item.ImageFileHolder.ExtensionLowered);
|
||||
bool isIgnoreExtension = item.ValidImageFormatExtension && _Configuration.IgnoreExtensions.Contains(item.ImageFileHolder.ExtensionLowered);
|
||||
result = GetPropertyOfPrivate(item, firstPass, filteredSourceDirectoryFileTuples, parseExceptions, isIgnoreExtension, isValidMetadataExtensions);
|
||||
return result;
|
||||
}
|
||||
|
||||
public (long Ticks, string FilteredSourceDirectoryFile, string PropertyDirectory, int PropertyId)[] GetPropertyIds(List<DirectoryInfo> groupCollection, bool saveToCollection)
|
||||
{
|
||||
List<(long Ticks, string FilteredSourceDirectoryFile, string PropertyDirectory, int PropertyId)> results = new();
|
||||
int level;
|
||||
string checkDirectory;
|
||||
List<string> directories;
|
||||
string propertyDirectory;
|
||||
Shared.Models.Property? property;
|
||||
string angleBracket = AngleBracketCollection[0];
|
||||
foreach (DirectoryInfo group in groupCollection)
|
||||
{
|
||||
SetAngleBracketCollection(group.SourceDirectory);
|
||||
if (string.IsNullOrEmpty(group.SourceDirectory))
|
||||
throw new Exception();
|
||||
if (!saveToCollection)
|
||||
propertyDirectory = angleBracket.Replace("<>", "()");
|
||||
else
|
||||
{
|
||||
(level, directories) = Shared.Models.Stateless.Methods.IPath.Get(_Configuration.RootDirectory, group.SourceDirectory);
|
||||
checkDirectory = Shared.Models.Stateless.Methods.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.SourceDirectoryFileHolderCollection.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();
|
||||
}
|
||||
|
||||
public static List<Shared.Models.Container> Get(Configuration configuration, A_Property propertyLogic)
|
||||
{
|
||||
List<Shared.Models.Container> results;
|
||||
long ticks = DateTime.Now.Ticks;
|
||||
List<string> exceptionsDirectories = new();
|
||||
results = Stateless.Container.GetContainers(configuration, propertyLogic);
|
||||
propertyLogic.ParallelWork(ticks, results, firstPass: false);
|
||||
if (exceptionsDirectories.Any())
|
||||
throw new Exception();
|
||||
return results;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user