From 3e7b84054d7f7878b6eab939e0dbf0b48b66d3f3 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 24 Aug 2018 19:41:33 +0100 Subject: [PATCH] Don't allow duplicate formats in configuration. --- src/ImageSharp/Formats/ImageFormatManager.cs | 17 +++++++++++++++-- tests/ImageSharp.Tests/ConfigurationTests.cs | 14 +++++++++----- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/ImageSharp/Formats/ImageFormatManager.cs b/src/ImageSharp/Formats/ImageFormatManager.cs index 63fd02d8d6..fdbc4ee442 100644 --- a/src/ImageSharp/Formats/ImageFormatManager.cs +++ b/src/ImageSharp/Formats/ImageFormatManager.cs @@ -13,6 +13,12 @@ namespace SixLabors.ImageSharp.Formats /// public class ImageFormatManager { + /// + /// Used for locking against as there is no ConcurrentSet type. + /// + /// + private static readonly object HashLock = new object(); + /// /// The list of supported keyed to mime types. /// @@ -26,7 +32,7 @@ namespace SixLabors.ImageSharp.Formats /// /// The list of supported s. /// - private readonly ConcurrentBag imageFormats = new ConcurrentBag(); + private readonly HashSet imageFormats = new HashSet(); /// /// The list of supported s. @@ -74,7 +80,14 @@ namespace SixLabors.ImageSharp.Formats Guard.NotNull(format, nameof(format)); Guard.NotNull(format.MimeTypes, nameof(format.MimeTypes)); Guard.NotNull(format.FileExtensions, nameof(format.FileExtensions)); - this.imageFormats.Add(format); + + lock (HashLock) + { + if (!this.imageFormats.Contains(format)) + { + this.imageFormats.Add(format); + } + } } /// diff --git a/tests/ImageSharp.Tests/ConfigurationTests.cs b/tests/ImageSharp.Tests/ConfigurationTests.cs index 1a7183df81..5c5eb9e9d9 100644 --- a/tests/ImageSharp.Tests/ConfigurationTests.cs +++ b/tests/ImageSharp.Tests/ConfigurationTests.cs @@ -2,13 +2,9 @@ // Licensed under the Apache License, Version 2.0. using System; -using System.Collections.Generic; -using System.IO; using System.Linq; -using SixLabors.ImageSharp.Formats; -using SixLabors.ImageSharp.IO; -using SixLabors.ImageSharp.PixelFormats; using Moq; +using SixLabors.ImageSharp.IO; using Xunit; // ReSharper disable InconsistentNaming @@ -96,5 +92,13 @@ namespace SixLabors.ImageSharp.Tests provider.Verify(x => x.Configure(config)); } + + [Fact] + public void DefaultConfigurationHasCorrectFormatCount() + { + Configuration config = Configuration.Default; + + Assert.Equal(4, config.ImageFormats.Count()); + } } } \ No newline at end of file