Browse Source

Don't allow duplicate formats in configuration.

af/merge-core
James Jackson-South 8 years ago
parent
commit
3e7b84054d
  1. 17
      src/ImageSharp/Formats/ImageFormatManager.cs
  2. 14
      tests/ImageSharp.Tests/ConfigurationTests.cs

17
src/ImageSharp/Formats/ImageFormatManager.cs

@ -13,6 +13,12 @@ namespace SixLabors.ImageSharp.Formats
/// </summary> /// </summary>
public class ImageFormatManager public class ImageFormatManager
{ {
/// <summary>
/// Used for locking against as there is no ConcurrentSet type.
/// <see href="https://github.com/dotnet/corefx/issues/6318"/>
/// </summary>
private static readonly object HashLock = new object();
/// <summary> /// <summary>
/// The list of supported <see cref="IImageEncoder"/> keyed to mime types. /// The list of supported <see cref="IImageEncoder"/> keyed to mime types.
/// </summary> /// </summary>
@ -26,7 +32,7 @@ namespace SixLabors.ImageSharp.Formats
/// <summary> /// <summary>
/// The list of supported <see cref="IImageFormat"/>s. /// The list of supported <see cref="IImageFormat"/>s.
/// </summary> /// </summary>
private readonly ConcurrentBag<IImageFormat> imageFormats = new ConcurrentBag<IImageFormat>(); private readonly HashSet<IImageFormat> imageFormats = new HashSet<IImageFormat>();
/// <summary> /// <summary>
/// The list of supported <see cref="IImageFormatDetector"/>s. /// The list of supported <see cref="IImageFormatDetector"/>s.
@ -74,7 +80,14 @@ namespace SixLabors.ImageSharp.Formats
Guard.NotNull(format, nameof(format)); Guard.NotNull(format, nameof(format));
Guard.NotNull(format.MimeTypes, nameof(format.MimeTypes)); Guard.NotNull(format.MimeTypes, nameof(format.MimeTypes));
Guard.NotNull(format.FileExtensions, nameof(format.FileExtensions)); Guard.NotNull(format.FileExtensions, nameof(format.FileExtensions));
this.imageFormats.Add(format);
lock (HashLock)
{
if (!this.imageFormats.Contains(format))
{
this.imageFormats.Add(format);
}
}
} }
/// <summary> /// <summary>

14
tests/ImageSharp.Tests/ConfigurationTests.cs

@ -2,13 +2,9 @@
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System; using System;
using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.IO;
using SixLabors.ImageSharp.PixelFormats;
using Moq; using Moq;
using SixLabors.ImageSharp.IO;
using Xunit; using Xunit;
// ReSharper disable InconsistentNaming // ReSharper disable InconsistentNaming
@ -96,5 +92,13 @@ namespace SixLabors.ImageSharp.Tests
provider.Verify(x => x.Configure(config)); provider.Verify(x => x.Configure(config));
} }
[Fact]
public void DefaultConfigurationHasCorrectFormatCount()
{
Configuration config = Configuration.Default;
Assert.Equal(4, config.ImageFormats.Count());
}
} }
} }
Loading…
Cancel
Save