This commit is contained in:
2022-11-20 23:20:28 -07:00
parent c6aa7e8e3c
commit 3b988ba152
85 changed files with 1355 additions and 980 deletions

View File

@ -5,6 +5,8 @@ using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using View_by_Distance.Metadata.Models;
using View_by_Distance.Property.Models.Stateless;
using View_by_Distance.Shared.Models;
using View_by_Distance.Shared.Models.Stateless;
namespace View_by_Distance.Resize.Models;
@ -64,6 +66,18 @@ public class C_Resize
return result;
}
public void SetAngleBracketCollection(Property.Models.Configuration configuration, string cResultsFullGroupDirectory, string sourceDirectory)
{
AngleBracketCollection.Clear();
AngleBracketCollection.AddRange(IResult.GetDirectoryInfoCollection(configuration,
sourceDirectory,
cResultsFullGroupDirectory,
contentDescription: "Resized image",
singletonDescription: "Resize dimensions for each resolution",
collectionDescription: string.Empty,
converted: true));
}
#pragma warning disable CA1416
public static (ImageCodecInfo imageCodecInfo, EncoderParameters encoderParameters, string filenameExtension) GetGifLowQuality()
@ -161,16 +175,12 @@ public class C_Resize
}
}
private byte[] SaveResizedSubfile3(Shared.Models.Item item, int[] resize, bool returnAndDoNotWrite, byte[] bytes)
private byte[] SaveResizedSubfile3(MappingFromItem mappingFromItem, int[] resize, bool returnAndDoNotWrite, byte[] bytes)
{
if (item.ImageFileHolder is null)
throw new NullReferenceException(nameof(item.ImageFileHolder));
if (item.ResizedFileHolder is null)
throw new NullReferenceException(nameof(item.ResizedFileHolder));
byte[] results;
Bitmap bitmap;
int outputResolutionWidth = resize[_OutputResolutionWidthIndex];
using Bitmap temp = new(item.ImageFileHolder.FullName, useIcm: false);
using Bitmap temp = new(mappingFromItem.ImageFileHolder.FullName, useIcm: false);
int outputResolutionHeight = resize[_OutputResolutionHeightIndex];
PropertyItem[] propertyItems = temp.PropertyItems;
int outputResolutionOrientation = resize[_OutputResolutionOrientationIndex];
@ -211,21 +221,17 @@ public class C_Resize
{
results = Array.Empty<byte>();
CopyPropertyItems(bytes, propertyItems, bitmap);
bitmap.Save(item.ResizedFileHolder.FullName, _ImageCodecInfo, _EncoderParameters);
bitmap.Save(mappingFromItem.ResizedFileHolder.FullName, _ImageCodecInfo, _EncoderParameters);
}
bitmap.Dispose();
return results;
}
private byte[] SaveResizedSubfile5(Shared.Models.Item item, int[] resize, bool returnAndDoNotWrite, byte[] bytes)
private byte[] SaveResizedSubfile5(MappingFromItem mappingFromItem, int[] resize, bool returnAndDoNotWrite, byte[] bytes)
{
if (item.ImageFileHolder is null)
throw new NullReferenceException(nameof(item.ImageFileHolder));
if (item.ResizedFileHolder is null)
throw new NullReferenceException(nameof(item.ResizedFileHolder));
byte[] results;
Bitmap bitmap;
using Bitmap temp = new(item.ImageFileHolder.FullName, useIcm: false);
using Bitmap temp = new(mappingFromItem.ImageFileHolder.FullName, useIcm: false);
PropertyItem[] propertyItems = temp.PropertyItems;
int tempResolutionWidth = resize[_TempResolutionWidth];
int tempResolutionHeight = resize[_TempResolutionHeight];
@ -279,7 +285,7 @@ public class C_Resize
{
results = Array.Empty<byte>();
CopyPropertyItems(bytes, propertyItems, bitmap);
bitmap.Save(item.ResizedFileHolder.FullName, _ImageCodecInfo, _EncoderParameters);
bitmap.Save(mappingFromItem.ResizedFileHolder.FullName, _ImageCodecInfo, _EncoderParameters);
}
}
bitmap.Dispose();
@ -288,28 +294,26 @@ public class C_Resize
#pragma warning restore CA1416
private byte[] SaveResizedSubfile(Shared.Models.Item item, int[] resize, bool returnAndDoNotWrite)
private byte[] SaveResizedSubfile(Shared.Models.Property property, MappingFromItem mappingFromItem, int[] resize, bool returnAndDoNotWrite)
{
if (item.Property is null)
throw new NullReferenceException(nameof(item.Property));
byte[] results;
// string subFile, Shared.Models.Property property, Shared.Models.FileHolder? fileHolder
string dateTimeFormat = Shared.Models.Stateless.Methods.IProperty.DateTimeFormat();
DateTime dateTime = Shared.Models.Stateless.Methods.IProperty.GetMinimumDateTime(item.Property);
DateTime dateTime = Shared.Models.Stateless.Methods.IProperty.GetMinimumDateTime(property);
string dateTimeValue = dateTime.ToString(dateTimeFormat);
byte[] bytes = _ASCIIEncoding.GetBytes(dateTimeValue);
if (_ASCIIEncoding.GetString(bytes, 0, bytes.Length) != dateTimeValue)
throw new Exception();
if (resize.Length == 3)
results = SaveResizedSubfile3(item, resize, returnAndDoNotWrite, bytes);
results = SaveResizedSubfile3(mappingFromItem, resize, returnAndDoNotWrite, bytes);
else if (resize.Length == 5)
results = SaveResizedSubfile5(item, resize, returnAndDoNotWrite, bytes);
results = SaveResizedSubfile5(mappingFromItem, resize, returnAndDoNotWrite, bytes);
else
throw new Exception();
return results;
}
public byte[] GetResizedBytes(string outputResolution, string cResultsFullGroupDirectory, List<Tuple<string, DateTime>> subFileTuples, Shared.Models.Item item, Dictionary<string, int[]> imageResizes)
public byte[] GetResizedBytes(string outputResolution, string cResultsFullGroupDirectory, List<Tuple<string, DateTime>> subFileTuples, Shared.Models.Property property, MappingFromItem mappingFromItem, Dictionary<string, int[]> imageResizes)
{
byte[] results;
if (!imageResizes.ContainsKey(outputResolution))
@ -318,23 +322,19 @@ public class C_Resize
int outputResolutionWidth = resize[_OutputResolutionWidthIndex];
int outputResolutionHeight = resize[_OutputResolutionHeightIndex];
int outputResolutionOrientation = resize[_OutputResolutionOrientationIndex];
results = SaveResizedSubfile(item, resize, returnAndDoNotWrite: true);
results = SaveResizedSubfile(property, mappingFromItem, resize, returnAndDoNotWrite: true);
subFileTuples.Add(new Tuple<string, DateTime>(nameof(C_Resize), DateTime.Now));
return results;
}
public void SaveResizedSubfile(string outputResolution, string cResultsFullGroupDirectory, List<Tuple<string, DateTime>> subFileTuples, Shared.Models.Item item, string original, Dictionary<string, int[]> imageResizes)
public void SaveResizedSubfile(Property.Models.Configuration configuration, string outputResolution, string cResultsFullGroupDirectory, List<Tuple<string, DateTime>> subFileTuples, Item item, Shared.Models.Property property, MappingFromItem mappingFromItem, string original, Dictionary<string, int[]> imageResizes)
{
if (item.Property is null)
throw new NullReferenceException(nameof(item.Property));
if (item.ImageFileHolder is null)
throw new NullReferenceException(nameof(item.ImageFileHolder));
if (item.ResizedFileHolder is null)
throw new NullReferenceException(nameof(item.ResizedFileHolder));
if (mappingFromItem.ResizedFileHolder is null)
throw new NullReferenceException(nameof(mappingFromItem.ResizedFileHolder));
if (!imageResizes.ContainsKey(outputResolution))
throw new Exception();
FileInfo fileInfo = new(item.ResizedFileHolder.FullName);
if (!fileInfo.Exists)
FileInfo fileInfo = new(mappingFromItem.ResizedFileHolder.FullName);
if (!fileInfo.FullName.Contains(configuration.ResultAllInOne) && !fileInfo.Exists)
{
if (fileInfo.Directory?.Parent is null)
throw new Exception();
@ -342,7 +342,7 @@ public class C_Resize
if (File.Exists(parentCheck))
{
File.Move(parentCheck, fileInfo.FullName);
item.SetResizedFileHolder(_FileNameExtension, Shared.Models.Stateless.Methods.IFileHolder.Refresh(item.ResizedFileHolder));
item.SetResizedFileHolder(_FileNameExtension, Shared.Models.Stateless.Methods.IFileHolder.Refresh(mappingFromItem.ResizedFileHolder));
fileInfo.Refresh();
}
}
@ -355,8 +355,8 @@ public class C_Resize
{
if (!fileInfo.Exists)
{
File.Copy(item.ImageFileHolder.FullName, fileInfo.FullName);
item.SetResizedFileHolder(_FileNameExtension, Shared.Models.Stateless.Methods.IFileHolder.Refresh(item.ResizedFileHolder));
File.Copy(mappingFromItem.ImageFileHolder.FullName, fileInfo.FullName);
item.SetResizedFileHolder(_FileNameExtension, Shared.Models.Stateless.Methods.IFileHolder.Refresh(mappingFromItem.ResizedFileHolder));
subFileTuples.Add(new Tuple<string, DateTime>(nameof(C_Resize), DateTime.Now));
}
}
@ -373,14 +373,14 @@ public class C_Resize
check = true;
if (check)
{
_ = SaveResizedSubfile(item, resize, returnAndDoNotWrite: false);
item.SetResizedFileHolder(_FileNameExtension, Shared.Models.Stateless.Methods.IFileHolder.Refresh(item.ResizedFileHolder));
_ = SaveResizedSubfile(property, mappingFromItem, resize, returnAndDoNotWrite: false);
item.SetResizedFileHolder(_FileNameExtension, Shared.Models.Stateless.Methods.IFileHolder.Refresh(mappingFromItem.ResizedFileHolder));
subFileTuples.Add(new Tuple<string, DateTime>(nameof(C_Resize), DateTime.Now));
}
}
}
private int[] GetCollection(string outputResolution)
private static int[] GetCollection(string outputResolution)
{
List<int> results = new();
string[] segments = outputResolution.Split('x');
@ -447,37 +447,30 @@ public class C_Resize
return results;
}
public Dictionary<string, int[]> GetResizeKeyValuePairs(Property.Models.Configuration configuration, string cResultsFullGroupDirectory, List<Tuple<string, DateTime>> subFileTuples, List<string> parseExceptions, string original, List<KeyValuePair<string, string>> metadataCollection, Shared.Models.Item item)
public FileHolder GetResizedFileHolder(Item item)
{
FileHolder result = new(Path.Combine(AngleBracketCollection[0].Replace("<>", "()"), Path.GetFileName(item.ImageFileHolder.FullName)));
return result;
}
public Dictionary<string, int[]> GetResizeKeyValuePairs(Property.Models.Configuration configuration, string cResultsFullGroupDirectory, List<Tuple<string, DateTime>> subFileTuples, List<string> parseExceptions, string original, List<KeyValuePair<string, string>> metadataCollection, Shared.Models.Property property, MappingFromItem mappingFromItem)
{
Dictionary<string, int[]> results;
if (item.Property?.Id is null)
throw new NullReferenceException(nameof(item.Property.Id));
if (item.ImageFileHolder is null)
throw new NullReferenceException(nameof(item.ImageFileHolder));
string json;
string[] changesFrom = new string[] { nameof(Property.Models.A_Property), nameof(B_Metadata) };
List<DateTime> dateTimes = (from l in subFileTuples where changesFrom.Contains(l.Item1) select l.Item2).ToList();
string usingRelativePath = Path.Combine(AngleBracketCollection[0].Replace("<>", "{}"), string.Concat(item.ImageFileHolder.NameWithoutExtension, ".json"));
string cResizeSingletonFile = Path.Combine(cResultsFullGroupDirectory, "{}", configuration.ResultAllInOne, $"{item.Property.Id.Value}{item.ImageFileHolder.ExtensionLowered}.json");
string cResizeSingletonFile = Path.Combine(cResultsFullGroupDirectory, "{}", configuration.ResultAllInOne, $"{mappingFromItem.Id}{mappingFromItem.ImageFileHolder.ExtensionLowered}.json");
FileInfo fileInfo = new(cResizeSingletonFile);
if (!fileInfo.Exists)
if (!fileInfo.FullName.Contains(configuration.ResultAllInOne) && !fileInfo.Exists)
{
if (File.Exists(usingRelativePath))
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(usingRelativePath, fileInfo.FullName);
File.Move(parentCheck, fileInfo.FullName);
fileInfo.Refresh();
}
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 (_ForceResizeLastWriteTimeToCreationTime && !fileInfo.Exists && File.Exists(Path.ChangeExtension(fileInfo.FullName, ".delete")))
{
@ -509,7 +502,7 @@ public class C_Resize
results = keyValuePairs;
if ((from l in results where l.Value[0] == l.Value[1] select true).Any())
{
results = GetImageResizes(item.Property, metadataCollection, original);
results = GetImageResizes(property, metadataCollection, original);
if (!(from l in results where l.Value[0] == l.Value[1] select true).Any())
throw new Exception("Was square!");
}
@ -523,7 +516,7 @@ public class C_Resize
}
if (results is null || !results.Any())
{
results = GetImageResizes(item.Property, metadataCollection, original);
results = GetImageResizes(property, metadataCollection, original);
json = JsonSerializer.Serialize(results, _WriteIndentedJsonSerializerOptions);
bool updateDateWhenMatches = dateTimes.Any() && fileInfo.Exists && dateTimes.Max() > fileInfo.LastWriteTime;
DateTime? dateTime = !updateDateWhenMatches ? null : dateTimes.Max();
@ -539,8 +532,6 @@ public class C_Resize
}
}
}
Shared.Models.FileHolder fileHolder = new(Path.Combine(AngleBracketCollection[0].Replace("<>", "()"), Path.GetFileName(item.ImageFileHolder.FullName)));
item.SetResizedFileHolder(_FileNameExtension, fileHolder);
return results;
}