Skip to content

Commit

Permalink
Merge pull request #684 from SixLabors/js/DuplicateFormats
Browse files Browse the repository at this point in the history
Don't allow duplicate formats in configuration.
  • Loading branch information
JimBobSquarePants authored Aug 26, 2018
2 parents d990a7c + 1c72e61 commit 301c2ce
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
23 changes: 18 additions & 5 deletions src/ImageSharp/Formats/ImageFormatManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ namespace SixLabors.ImageSharp.Formats
/// </summary>
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>
/// The list of supported <see cref="IImageEncoder"/> keyed to mime types.
/// </summary>
Expand All @@ -26,7 +32,7 @@ public class ImageFormatManager
/// <summary>
/// The list of supported <see cref="IImageFormat"/>s.
/// </summary>
private readonly ConcurrentBag<IImageFormat> imageFormats = new ConcurrentBag<IImageFormat>();
private readonly HashSet<IImageFormat> imageFormats = new HashSet<IImageFormat>();

/// <summary>
/// The list of supported <see cref="IImageFormatDetector"/>s.
Expand Down Expand Up @@ -74,7 +80,14 @@ public void AddImageFormat(IImageFormat format)
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);
}
}
}

/// <summary>
Expand All @@ -86,9 +99,9 @@ public IImageFormat FindFormatByFileExtension(string extension)
{
Guard.NotNullOrWhiteSpace(extension, nameof(extension));

if (extension[0] == '.')
{
extension = extension.Substring(1);
if (extension[0] == '.')
{
extension = extension.Substring(1);
}

return this.imageFormats.FirstOrDefault(x => x.FileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase));
Expand Down
31 changes: 24 additions & 7 deletions tests/ImageSharp.Tests/ConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -19,8 +15,8 @@ namespace SixLabors.ImageSharp.Tests
/// </summary>
public class ConfigurationTests
{
public Configuration ConfigurationEmpty { get; private set; }
public Configuration DefaultConfiguration { get; private set; }
public Configuration ConfigurationEmpty { get; }
public Configuration DefaultConfiguration { get; }

public ConfigurationTests()
{
Expand Down Expand Up @@ -96,5 +92,26 @@ public void AddFormatCallsConfig()

provider.Verify(x => x.Configure(config));
}

[Fact]
public void ConfigurationCannotAddDuplicates()
{
const int count = 4;
Configuration config = Configuration.Default;

Assert.Equal(count, config.ImageFormats.Count());

config.ImageFormatsManager.AddImageFormat(ImageFormats.Bmp);

Assert.Equal(count, config.ImageFormats.Count());
}

[Fact]
public void DefaultConfigurationHasCorrectFormatCount()
{
Configuration config = Configuration.Default;

Assert.Equal(4, config.ImageFormats.Count());
}
}
}

0 comments on commit 301c2ce

Please sign in to comment.