Re-write
This commit is contained in:
@ -6,7 +6,6 @@ using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using View_by_Distance.Metadata.Models;
|
||||
using View_by_Distance.Property.Models;
|
||||
using View_by_Distance.Shared.Models.Stateless;
|
||||
|
||||
namespace View_by_Distance.Resize.Models;
|
||||
@ -19,6 +18,9 @@ public class C_Resize
|
||||
|
||||
public List<string> AngleBracketCollection { get; }
|
||||
|
||||
protected readonly string _FilenameExtension;
|
||||
public string FilenameExtension => _FilenameExtension;
|
||||
|
||||
private readonly Serilog.ILogger? _Log;
|
||||
private readonly int _TempResolutionWidth;
|
||||
private readonly int _TempResolutionHeight;
|
||||
@ -35,7 +37,7 @@ public class C_Resize
|
||||
private readonly bool _ForceResizeLastWriteTimeToCreationTime;
|
||||
private readonly JsonSerializerOptions _WriteIndentedJsonSerializerOptions;
|
||||
|
||||
public C_Resize(bool forceResizeLastWriteTimeToCreationTime, bool overrideForResizeImages, bool propertiesChangedForResize, string[] validResolutions, ImageCodecInfo imageCodecInfo, EncoderParameters encoderParameters)
|
||||
public C_Resize(bool forceResizeLastWriteTimeToCreationTime, bool overrideForResizeImages, bool propertiesChangedForResize, string[] validResolutions, ImageCodecInfo imageCodecInfo, EncoderParameters encoderParameters, string filenameExtension)
|
||||
{
|
||||
_TempResolutionWidth = 3;
|
||||
_TempResolutionHeight = 4;
|
||||
@ -46,6 +48,7 @@ public class C_Resize
|
||||
_ValidResolutions = validResolutions;
|
||||
_OutputResolutionOrientationIndex = 2;
|
||||
_EncoderParameters = encoderParameters;
|
||||
_FilenameExtension = filenameExtension;
|
||||
_Log = Serilog.Log.ForContext<C_Resize>();
|
||||
AngleBracketCollection = new List<string>();
|
||||
_OverrideForResizeImages = overrideForResizeImages;
|
||||
@ -66,14 +69,44 @@ public class C_Resize
|
||||
|
||||
#pragma warning disable CA1416
|
||||
|
||||
public static (ImageCodecInfo imageCodecInfo, EncoderParameters encoderParameters) GetTuple(string outputExtension, int outputQuality)
|
||||
public static (ImageCodecInfo imageCodecInfo, EncoderParameters encoderParameters, string filenameExtension) GetGifLowQuality()
|
||||
{
|
||||
(ImageCodecInfo imageCodecInfo, EncoderParameters encoderParameters) result;
|
||||
(ImageCodecInfo, EncoderParameters, string) result;
|
||||
System.Drawing.Imaging.ImageFormat imageFormat = System.Drawing.Imaging.ImageFormat.Gif;
|
||||
ImageCodecInfo imageCodecInfo = (from l in ImageCodecInfo.GetImageEncoders() where l.FormatID == imageFormat.Guid select l).First();
|
||||
EncoderParameters encoderParameters = new(1);
|
||||
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 75L);
|
||||
if (string.IsNullOrEmpty(imageCodecInfo.FilenameExtension))
|
||||
throw new NullReferenceException(nameof(imageCodecInfo.FilenameExtension));
|
||||
result = new(imageCodecInfo, encoderParameters, imageCodecInfo.FilenameExtension.Split(';')[0].ToLower()[1..]);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static (ImageCodecInfo imageCodecInfo, EncoderParameters encoderParameters, string filenameExtension) GetPngLowQuality()
|
||||
{
|
||||
(ImageCodecInfo, EncoderParameters, string) result;
|
||||
System.Drawing.Imaging.ImageFormat imageFormat = System.Drawing.Imaging.ImageFormat.Png;
|
||||
ImageCodecInfo imageCodecInfo = (from l in ImageCodecInfo.GetImageEncoders() where l.FormatID == imageFormat.Guid select l).First();
|
||||
EncoderParameters encoderParameters = new(1);
|
||||
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 75L);
|
||||
if (string.IsNullOrEmpty(imageCodecInfo.FilenameExtension))
|
||||
throw new NullReferenceException(nameof(imageCodecInfo.FilenameExtension));
|
||||
result = new(imageCodecInfo, encoderParameters, imageCodecInfo.FilenameExtension.Split(';')[0].ToLower()[1..]);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static (ImageCodecInfo imageCodecInfo, EncoderParameters encoderParameters, string filenameExtension) GetTuple(string outputExtension, int outputQuality)
|
||||
{
|
||||
(ImageCodecInfo, EncoderParameters, string) result;
|
||||
System.Drawing.Imaging.ImageFormat imageFormat = outputExtension switch
|
||||
{
|
||||
".gif" => System.Drawing.Imaging.ImageFormat.Gif,
|
||||
".jfif" => System.Drawing.Imaging.ImageFormat.Jpeg,
|
||||
".jpe" => System.Drawing.Imaging.ImageFormat.Jpeg,
|
||||
".jpeg" => System.Drawing.Imaging.ImageFormat.Jpeg,
|
||||
".jpg" => System.Drawing.Imaging.ImageFormat.Jpeg,
|
||||
".png" => System.Drawing.Imaging.ImageFormat.Png,
|
||||
".tif" => System.Drawing.Imaging.ImageFormat.Tiff,
|
||||
".tiff" => System.Drawing.Imaging.ImageFormat.Tiff,
|
||||
_ => throw new Exception(),
|
||||
};
|
||||
@ -82,7 +115,9 @@ public class C_Resize
|
||||
// encoderParameters.Param[0] = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, CType(75L, Int32)) 'Default
|
||||
// encoderParameters.Param[0] = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, CType(95L, Int32)) 'Paint
|
||||
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, outputQuality);
|
||||
result = new(imageCodecInfo, encoderParameters);
|
||||
if (string.IsNullOrEmpty(imageCodecInfo.FilenameExtension))
|
||||
throw new NullReferenceException(nameof(imageCodecInfo.FilenameExtension));
|
||||
result = new(imageCodecInfo, encoderParameters, imageCodecInfo.FilenameExtension.Split(';')[0].ToLower()[1..]);
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -129,7 +164,7 @@ public class C_Resize
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] SaveResizedSubfile3(string subFile, int[] resize, byte[] bytes, FileHolder? fileHolder)
|
||||
private byte[] SaveResizedSubfile3(string subFile, int[] resize, byte[] bytes, Shared.Models.FileHolder? fileHolder)
|
||||
{
|
||||
byte[] results;
|
||||
Bitmap bitmap;
|
||||
@ -181,7 +216,7 @@ public class C_Resize
|
||||
return results;
|
||||
}
|
||||
|
||||
private byte[] SaveResizedSubfile5(string subFile, int[] resize, byte[] bytes, FileHolder? fileHolder)
|
||||
private byte[] SaveResizedSubfile5(string subFile, int[] resize, byte[] bytes, Shared.Models.FileHolder? fileHolder)
|
||||
{
|
||||
byte[] results;
|
||||
Bitmap bitmap;
|
||||
@ -248,11 +283,11 @@ public class C_Resize
|
||||
|
||||
#pragma warning restore CA1416
|
||||
|
||||
private byte[] SaveResizedSubfile(string subFile, A_Property property, int[] resize, FileHolder? fileHolder)
|
||||
private byte[] SaveResizedSubfile(string subFile, Shared.Models.Property property, int[] resize, Shared.Models.FileHolder? fileHolder)
|
||||
{
|
||||
byte[] results;
|
||||
string dateTimeFormat = Property.Models.Stateless.A_Property.DateTimeFormat();
|
||||
DateTime dateTime = Property.Models.Stateless.A_Property.GetMinimumDateTime(property);
|
||||
string dateTimeFormat = Shared.Models.Stateless.Methods.IProperty.DateTimeFormat();
|
||||
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)
|
||||
@ -284,7 +319,7 @@ public class C_Resize
|
||||
return results;
|
||||
}
|
||||
|
||||
public byte[] GetResizedBytes(string outputResolution, string cResultsFullGroupDirectory, List<Tuple<string, DateTime>> subFileTuples, Item item, A_Property property, Dictionary<string, int[]> imageResizes)
|
||||
public byte[] GetResizedBytes(string outputResolution, string cResultsFullGroupDirectory, List<Tuple<string, DateTime>> subFileTuples, Shared.Models.Item item, Shared.Models.Property property, Dictionary<string, int[]> imageResizes)
|
||||
{
|
||||
byte[] results;
|
||||
if (item.ImageFileHolder is null)
|
||||
@ -300,7 +335,7 @@ public class C_Resize
|
||||
return results;
|
||||
}
|
||||
|
||||
public void SaveResizedSubfile(string outputResolution, string cResultsFullGroupDirectory, List<Tuple<string, DateTime>> subFileTuples, Item item, string original, Dictionary<string, int[]> imageResizes)
|
||||
public void SaveResizedSubfile(string outputResolution, string cResultsFullGroupDirectory, List<Tuple<string, DateTime>> subFileTuples, Shared.Models.Item item, string original, Dictionary<string, int[]> imageResizes)
|
||||
{
|
||||
if (item.Property is null)
|
||||
throw new NullReferenceException(nameof(item.Property));
|
||||
@ -308,7 +343,7 @@ public class C_Resize
|
||||
throw new NullReferenceException(nameof(item.ImageFileHolder));
|
||||
if (item.ResizedFileHolder is null)
|
||||
throw new NullReferenceException(nameof(item.ResizedFileHolder));
|
||||
FileHolder fileHolder = item.ResizedFileHolder;
|
||||
Shared.Models.FileHolder fileHolder = item.ResizedFileHolder;
|
||||
if (!imageResizes.ContainsKey(outputResolution))
|
||||
throw new Exception();
|
||||
if (!fileHolder.Exists)
|
||||
@ -320,7 +355,7 @@ public class C_Resize
|
||||
if (File.Exists(parentCheck))
|
||||
{
|
||||
File.Move(parentCheck, fileInfo.FullName);
|
||||
item.SetResizedFileHolder(FileHolder.Refresh(fileHolder));
|
||||
item.SetResizedFileHolder(_FilenameExtension, Shared.Models.Stateless.Methods.IFileHolder.Refresh(fileHolder));
|
||||
}
|
||||
}
|
||||
int[] resize = imageResizes[outputResolution];
|
||||
@ -339,7 +374,7 @@ public class C_Resize
|
||||
else
|
||||
{
|
||||
bool check = false;
|
||||
string[] changesFrom = new string[] { nameof(A_Property), nameof(B_Metadata), nameof(C_Resize) };
|
||||
string[] changesFrom = new string[] { nameof(Property.Models.A_Property), nameof(B_Metadata), nameof(C_Resize) };
|
||||
List<DateTime> dateTimes = (from l in subFileTuples where changesFrom.Contains(l.Item1) select l.Item2).ToList();
|
||||
if (_OverrideForResizeImages)
|
||||
check = true;
|
||||
@ -378,7 +413,7 @@ public class C_Resize
|
||||
return result;
|
||||
}
|
||||
|
||||
private Dictionary<string, int[]> GetImageResizes(A_Property property, List<KeyValuePair<string, string>> metadataCollection, string original)
|
||||
private Dictionary<string, int[]> GetImageResizes(Shared.Models.Property property, List<KeyValuePair<string, string>> metadataCollection, string original)
|
||||
{
|
||||
Dictionary<string, int[]> results = new();
|
||||
int[] desired;
|
||||
@ -424,7 +459,7 @@ public class C_Resize
|
||||
return results;
|
||||
}
|
||||
|
||||
public Dictionary<string, int[]> GetResizeKeyValuePairs(string cResultsFullGroupDirectory, List<Tuple<string, DateTime>> subFileTuples, List<string> parseExceptions, string original, List<KeyValuePair<string, string>> metadataCollection, Item item)
|
||||
public Dictionary<string, int[]> GetResizeKeyValuePairs(string cResultsFullGroupDirectory, List<Tuple<string, DateTime>> subFileTuples, List<string> parseExceptions, string original, List<KeyValuePair<string, string>> metadataCollection, Shared.Models.Item item)
|
||||
{
|
||||
Dictionary<string, int[]> results;
|
||||
if (item.Property?.Id is null)
|
||||
@ -432,7 +467,7 @@ public class C_Resize
|
||||
if (item.ImageFileHolder is null)
|
||||
throw new NullReferenceException(nameof(item.ImageFileHolder));
|
||||
string json;
|
||||
string[] changesFrom = new string[] { nameof(A_Property), nameof(B_Metadata) };
|
||||
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, "{}", Property.Models.Stateless.IResult.AllInOne, $"{item.Property.Id.Value}{item.ImageFileHolder.ExtensionLowered}.json");
|
||||
@ -504,7 +539,7 @@ public class C_Resize
|
||||
json = JsonSerializer.Serialize(results, _WriteIndentedJsonSerializerOptions);
|
||||
bool updateDateWhenMatches = dateTimes.Any() && fileInfo.Exists && dateTimes.Max() > fileInfo.LastWriteTime;
|
||||
DateTime? dateTime = !updateDateWhenMatches ? null : dateTimes.Max();
|
||||
if (Property.Models.Stateless.IPath.WriteAllText(fileInfo.FullName, json, updateDateWhenMatches, compareBeforeWrite: true, updateToWhenMatches: dateTime))
|
||||
if (Shared.Models.Stateless.Methods.IPath.WriteAllText(fileInfo.FullName, json, updateDateWhenMatches, compareBeforeWrite: true, updateToWhenMatches: dateTime))
|
||||
{
|
||||
if (!_ForceResizeLastWriteTimeToCreationTime && (!fileInfo.Exists || fileInfo.LastWriteTime == fileInfo.CreationTime))
|
||||
subFileTuples.Add(new Tuple<string, DateTime>(nameof(C_Resize), DateTime.Now));
|
||||
|
Reference in New Issue
Block a user