Detect potential errors copying metadata and avoid them (#2447) (#6997)

This commit is contained in:
Tony Mitchell 2020-10-08 16:16:32 -07:00 committed by GitHub
parent 42ebc42c98
commit 38e03e6bb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 1 deletions

View File

@ -123,6 +123,9 @@
<Content Include="Test.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestMetadataIssue2447.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestPortrait.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

View File

@ -30,6 +30,18 @@ namespace ImageResizer.Models
image => Assert.Equal("Test", ((BitmapMetadata)image.Frames[0].Metadata).Comment));
}
[Fact]
public void ExecuteCopiesFrameMetadataExceptWhenMetadataCannotBeCloned()
{
var operation = new ResizeOperation("TestMetadataIssue2447.jpg", _directory, Settings());
operation.Execute();
AssertEx.Image(
_directory.File(),
image => Assert.Null(((BitmapMetadata)image.Frames[0].Metadata).CameraModel));
}
[Fact]
public void ExecuteKeepsDateModified()
{

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 MiB

View File

@ -64,11 +64,25 @@ namespace ImageResizer.Models
foreach (var originalFrame in decoder.Frames)
{
BitmapMetadata metadata = (BitmapMetadata)originalFrame.Metadata;
if (metadata != null)
{
try
{
// Detect whether metadata can copied successfully
_ = metadata.Clone();
}
catch (ArgumentException)
{
metadata = null;
}
}
encoder.Frames.Add(
BitmapFrame.Create(
Transform(originalFrame),
thumbnail: null,
(BitmapMetadata)originalFrame.Metadata, // TODO: Add an option to strip any metadata that doesn't affect rendering (issue #3)
metadata, // TODO: Add an option to strip any metadata that doesn't affect rendering (issue #3)
colorContexts: null));
}