Skip to content

Commit

Permalink
fix regression #14715 by adding an additional check for the newly bui…
Browse files Browse the repository at this point in the history
…ld metadata object (#15198)
  • Loading branch information
CleanCodeDeveloper committed Dec 29, 2021
1 parent b6affde commit d39c412
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/modules/imageresizer/ui/Extensions/BitmapMetadataExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,31 @@ object GetQueryWithPreCheck(BitmapMetadata metadata, string query)
}
}

/// <summary>
/// Detect whether the metadata object is valid and can be copied successfully
/// </summary>
/// <remarks>
/// ImageMetadata.Clone() causes an exception if there is something wrong with the metadata object.
/// Operation is rather expensive.
/// </remarks>
/// <param name="metadata">Metadata object to be checked</param>
/// <returns>true if valid, false if invalid</returns>
public static bool IsMetadataObjectValid(this BitmapMetadata metadata)
{
try
{
_ = metadata.Clone();

return true;
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception)
#pragma warning restore CA1031 // Do not catch general exception types
{
return false;
}
}

/// <summary>
/// Prints all metadata to debug console
/// </summary>
Expand Down
10 changes: 9 additions & 1 deletion src/modules/imageresizer/ui/Models/ResizeOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,15 @@ public void Execute()
}
}

metadata = newMetadata;
if (newMetadata.IsMetadataObjectValid())
{
metadata = newMetadata;
}
else
{
// Seems like we build an invalid metadata object. ImageResizer will fail when trying to write the image to disk. We discard all metadata to be able to save the image.
metadata = null;
}
}
catch (ArgumentException ex)
{
Expand Down

0 comments on commit d39c412

Please sign in to comment.