Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure assets are downloaded just once #18

Merged
merged 1 commit into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Kontent.Statiq.Tests/LinqExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Statiq.Common;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Xunit;

namespace Kontent.Statiq.Tests
{
public class LinqExtensionsTests
{
[Fact]
public void DistinctRemovesDuplicateEntriesFromASequence()
{
// Arrange
var assets = new List<KontentImageDownload> {
new KontentImageDownload("a", new NormalizedPath("/a/")),
new KontentImageDownload("a", new NormalizedPath("/a/")),
new KontentImageDownload("b", new NormalizedPath("/b/"))
};

// Act
var assetUrls = assets.DistinctBy(a => a.LocalPath);

// Assert
Assert.Equal(2, assetUrls.Count());
Assert.NotEqual(assetUrls.FirstOrDefault().LocalPath, assetUrls.LastOrDefault().LocalPath);
}
}
}
2 changes: 1 addition & 1 deletion Kontent.Statiq/KontentDownloadImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected override async Task<IEnumerable<IDocument>> ExecuteContextAsync(IExecu
.SelectMany(doc => doc.GetKontentImageDownloads())
.ToArray();

var assetUrls = assets.Select(a => a.OriginalUrl).ToArray(); // TODO: distinct by local path
var assetUrls = assets.DistinctBy(a => a.LocalPath).Select(a => a.OriginalUrl).ToArray();

WithUris(assetUrls.ToArray());

Expand Down
31 changes: 31 additions & 0 deletions Kontent.Statiq/LinqExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;

namespace Kontent.Statiq
{
/// <summary>
/// Extension methods enriching the LINQ syntax.
/// </summary>
public static class LinqExtensions
{
/// <summary>
/// Returns distinct elements from a sequence by using an equality comparer based on specified properties.
/// </summary>
/// <typeparam name="TSource">The type of the elements of source.</typeparam>
/// <typeparam name="TKey">The type of the selector key.</typeparam>
/// <param name="source">The sequence to remove duplicate elements from.</param>
/// <param name="keySelector">Selector allowing to specify one or more properties to base the filtering on.</param>
/// <returns><see cref="IEnumerable{T}"/> that contains distinct elements from the source sequence.</returns>
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
HashSet<TKey> seenKeys = new HashSet<TKey>();
foreach (TSource element in source)
{
if (seenKeys.Add(keySelector(element)))
{
yield return element;
}
}
}
}
}