2 Commits

Author SHA1 Message Date
66f366e9cf Changed GetDimensions to handle a stream at the end and one exit 2025-06-15 07:22:20 -07:00
9a51d995cc validation-image-file 2025-04-20 14:16:03 -07:00
3 changed files with 49 additions and 15 deletions

View File

@ -8,16 +8,16 @@ internal static class Dimensions
#pragma warning disable IDE0230
private static readonly Dictionary<byte[], Func<BinaryReader, Size?>> _ImageFormatDecoders = new()
{
{ new byte[] { 0xff, 0xd8 }, DecodeJfif },
{ new byte[] { 0x42, 0x4D }, DecodeBitmap },
{ new byte[] { 0x52, 0x49, 0x46, 0x46 }, DecodeWebP },
{ new byte[] { 0x47, 0x49, 0x46, 0x38, 0x37, 0x61 }, DecodeGif },
{ new byte[] { 0x47, 0x49, 0x46, 0x38, 0x39, 0x61 }, DecodeGif },
{ new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }, DecodePng },
{ new byte[] { 0xff, 0xd8 }, DecodeJfif },
{ new byte[] { 0x52, 0x49, 0x46, 0x46 }, DecodeWebP },
};
#pragma warning restore IDE0230
private static bool StartsWith(byte[] thisBytes, byte[] thatBytes)
private static bool StartsWith(List<byte> thisBytes, byte[] thatBytes)
{
for (int i = 0; i < thatBytes.Length; i += 1)
{
@ -103,30 +103,49 @@ internal static class Dimensions
internal static Size? GetDimensions(BinaryReader binaryReader)
{
int maxMagicBytesLength = _ImageFormatDecoders.Keys.OrderByDescending(x => x.Length).First().Length;
byte[] magicBytes = new byte[maxMagicBytesLength];
for (int i = 0; i < maxMagicBytesLength; i += 1)
Size? result;
List<byte> magicBytes = [];
int[] magicBytesLengths = (from l in _ImageFormatDecoders.Keys where l.Length <= binaryReader.BaseStream.Length orderby l.Length descending select l.Length).ToArray();
if (magicBytesLengths.Length == 0)
result = null;
else
{
magicBytes[i] = binaryReader.ReadByte();
result = null;
if (binaryReader.BaseStream.Length == binaryReader.BaseStream.Position)
_ = binaryReader.BaseStream.Seek(0, SeekOrigin.Begin);
for (int i = 0; i < magicBytesLengths[0]; i++)
{
magicBytes.Add(binaryReader.ReadByte());
foreach (KeyValuePair<byte[], Func<BinaryReader, Size?>> kvPair in _ImageFormatDecoders)
{
if (StartsWith(magicBytes, kvPair.Key))
return kvPair.Value(binaryReader);
{
result = kvPair.Value(binaryReader);
break;
}
}
return null;
if (result is not null)
break;
}
}
return result;
}
internal static Size? GetDimensions(string path)
{
using BinaryReader binaryReader = new(File.OpenRead(path));
return GetDimensions(binaryReader);
Size? result;
using FileStream fileStream = File.OpenRead(path);
using BinaryReader binaryReader = new(fileStream);
result = GetDimensions(binaryReader);
return result;
}
internal static Size? GetDimensions(Stream stream)
{
Size? result;
using BinaryReader binaryReader = new(stream);
return GetDimensions(binaryReader);
result = GetDimensions(binaryReader);
return result;
}
}

View File

@ -19,7 +19,9 @@ public record RenameSettings(string Company,
string RelativePropertyCollectionFile,
bool RequireRootDirectoryExists,
string[] SidecarExtensions,
bool SkipIdFiles) : Shared.Models.Properties.IRenameSettings
bool SkipIdFiles,
int ValidationImageDeterministicHashCodeId,
string ValidationImageFile) : Shared.Models.Properties.IRenameSettings
{
public override string ToString()

View File

@ -127,9 +127,22 @@ public partial class Rename : IRename, IDisposable
LogNetToHoursSince(logger);
long ticks = DateTime.Now.Ticks;
_ProgressBarOptions = new() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true };
Verify(logger, appSettings, rename);
RenameWork(logger, appSettings, rename, ticks);
}
private static void Verify(ILogger<Program>? logger, AppSettings appSettings, IRename rename)
{
FileHolder fileHolder = FileHolder.Get(appSettings.RenameSettings.ValidationImageFile);
FilePath filePath = FilePath.Get(appSettings.ResultSettings, appSettings.MetadataSettings, fileHolder, index: null);
DeterministicHashCode deterministicHashCode = rename.GetDeterministicHashCode(filePath);
if (deterministicHashCode.Id is null)
throw new NullReferenceException(nameof(deterministicHashCode));
if (deterministicHashCode.Id.Value != appSettings.RenameSettings.ValidationImageDeterministicHashCodeId)
throw new Exception("Deterministic hash code id is incorrect!");
logger?.LogDebug("Validated deterministic hash code id");
}
private static void LogNetToHoursSince(ILogger<Program>? logger)
{
double secondsInAHour = 3600f;