Skip to content

Commit

Permalink
Image processing
Browse files Browse the repository at this point in the history
Updated readme + module rename
  • Loading branch information
alanta committed Jun 8, 2020
1 parent 97aceba commit e79457b
Show file tree
Hide file tree
Showing 12 changed files with 267 additions and 222 deletions.
4 changes: 2 additions & 2 deletions Kontent.Statiq.Tests/Models/CustomTypeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public class CustomTypeProvider : ITypeProvider

public Type GetType(string contentType)
{
return _codenames.Keys.FirstOrDefault(type => GetCodename(type).Equals(contentType));
return _codenames.Keys.FirstOrDefault(type => string.Equals(GetCodename(type), contentType));
}

public string GetCodename(Type contentType)
public string? GetCodename(Type contentType)
{
return _codenames.TryGetValue(contentType, out var codename) ? codename : null;
}
Expand Down
13 changes: 5 additions & 8 deletions Kontent.Statiq.Tests/Tools/FakeDeliveryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static class MockDeliveryClient
// More info https://github.com/Kentico/kontent-delivery-sdk-net/wiki/Faking-responses


public static IDeliveryClient Create(string response, Func<IOptionalClientSetup, IOptionalClientSetup> configureClient = null)
public static IDeliveryClient Create(string response, Func<IOptionalClientSetup, IOptionalClientSetup>? configureClient = null)
{
const string testUrl = "https://tests.fake.url";

Expand All @@ -45,22 +45,19 @@ private static DeliveryOptions MockDeliveryOptions(string baseUrl)
.WithCustomEndpoint($"{baseUrl}/{{0}}")
.Build();

private static IDeliveryClient CreateMockDeliveryClient(DeliveryOptions deliveryOptions, HttpClient httpClient, Func<IOptionalClientSetup, IOptionalClientSetup> configureClient)
private static IDeliveryClient CreateMockDeliveryClient(DeliveryOptions deliveryOptions, HttpClient httpClient, Func<IOptionalClientSetup, IOptionalClientSetup>? configureClient)
{
//var contentLinkUrlResolver = A.Fake<IContentLinkUrlResolver>();
//var modelProvider = A.Fake<IModelProvider>();
var retryPolicy = A.Fake<IRetryPolicy>();
var retryPolicyProvider = A.Fake<IRetryPolicyProvider>();
A.CallTo(() => retryPolicyProvider.GetRetryPolicy())
.Returns(retryPolicy);
A.CallTo(() => retryPolicy.ExecuteAsync(A<Func<Task<HttpResponseMessage>>>._))
.ReturnsLazily(c => c.GetArgument<Func<Task<HttpResponseMessage>>>(0)());
.ReturnsLazily(c => c.GetArgument<Func<Task<HttpResponseMessage>>>(0)!.Invoke() );

var client = DeliveryClientBuilder
.WithOptions(_ => deliveryOptions)
.WithDeliveryHttpClient(new DeliveryHttpClient(httpClient))
//.WithContentLinkUrlResolver(contentLinkUrlResolver)
//.WithModelProvider(modelProvider)
.ApplyIfNotNull( configureClient )
.WithRetryPolicyProvider(retryPolicyProvider)
.Build();
Expand All @@ -69,9 +66,9 @@ private static IDeliveryClient CreateMockDeliveryClient(DeliveryOptions delivery
}
}

public static class Helpers
internal static class Helpers
{
public static TBuilder ApplyIfNotNull<TBuilder>(this TBuilder builder, Func<TBuilder, TBuilder> configure)
public static TBuilder ApplyIfNotNull<TBuilder>(this TBuilder builder, Func<TBuilder, TBuilder>? configure)
{
return configure != null ? configure(builder) : builder;
}
Expand Down
105 changes: 0 additions & 105 deletions Kontent.Statiq.Tests/When_processing_assets.cs

This file was deleted.

54 changes: 54 additions & 0 deletions Kontent.Statiq.Tests/When_processing_images.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using FluentAssertions;
using Statiq.Testing;
using System.Threading.Tasks;
using Xunit;

namespace Kontent.Statiq.Tests
{
public class When_processing_images : BaseFixture
{
[Fact]
public async Task It_should_extract_asset_urls()
{
// Given
const string url = "https://the.cms/assets/image1.jpg";
string content = $"<img src='{url}'/>";

var document = new TestDocument(content);

// When
var assetParser = new KontentImageProcessor();
var output = await ExecuteAsync(new[]{document}, assetParser);

// Then
output.Length.Should().Be(1);
var outputDoc = output[0];

var urls = outputDoc.GetKontentImageDownloads();

urls.Should().Contain( dl => dl.OriginalUrl == url );
}

[Theory,
InlineData("https://the.cms/images/image1.jpg", "/assets/img/image1.jpg"),
InlineData("https://the.cms/images/image1.jpg?w=110&h=340", "/assets/img/2cddd89b6d47fa06efe3e14ceada65c7-image1.jpg"),
InlineData("https://the.cms/images/image1.jpg?h=340&w=110", "/assets/img/2cddd89b6d47fa06efe3e14ceada65c7-image1.jpg")]
public async Task It_should_replace_url_with_local_path(string sourceUrl, string localUrl)
{
// Given
string content = $"<img src='{sourceUrl}'/>";

var document = new TestDocument(content);

// When
var assetParser = new KontentImageProcessor().WithLocalPath("/assets/img");
var output = await ExecuteAsync(new[] { document }, assetParser);

// Then
output.Length.Should().Be(1);
var outputDoc = output[0];

outputDoc.Content.Should().Contain($"src=\"{localUrl}\"");
}
}
}
24 changes: 5 additions & 19 deletions Kontent.Statiq/Html/HtmlHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Kentico.Kontent.Delivery.ImageTransformation;
using Statiq.Common;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Kontent.Statiq.Html
Expand All @@ -12,6 +11,11 @@ namespace Kontent.Statiq.Html
/// </summary>
public static class HtmlHelpers
{
/// <summary>
/// Build an image url from an <see cref="Asset"/>.
/// </summary>
/// <param name="asset"></param>
/// <returns>An <see cref="ImageUrlBuilder"/> for the asset.</returns>
public static ImageUrlBuilder ImageUrl(this Asset asset)
{
return new ImageUrlBuilder(asset.Url);
Expand All @@ -33,23 +37,5 @@ public static string GetFirstAssetUrl(this IDocument document, string codename)
var assets = document.Get<IEnumerable<Asset>>(codename);
return assets?.FirstOrDefault()?.Url ?? string.Empty;
}

/// <summary>
/// Get the first asset url from a document for assets that are stored locally using the code name. This is intended for untyped content.
/// </summary>
/// <param name="document">The Statiq document.</param>
/// <param name="codename">The codename of the field that holds the asset.</param>
/// <param name="folderPath">The folder where local assets are stored.</param>
/// <returns>The asset url or an empty string if not available.</returns>
public static string GetFirstAssetLocalUrl(this IDocument document, string codename, string folderPath = "")
{
var assetUrl = GetFirstAssetUrl(document, codename);
if (string.IsNullOrEmpty(assetUrl))
{
return string.Empty;
}

return Path.Combine("/", folderPath, KontentAssetHelper.GetAssetFileName(assetUrl)).Replace(@"\", "/");
}
}
}
17 changes: 10 additions & 7 deletions Kontent.Statiq/Kontent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ public sealed class Kontent : Module
/// <summary>
/// The preview API key to use. Set this if you want to use preview (unpublished) content./>.
/// </summary>
public string PreviewApiKey { get; set; }
public string? PreviewApiKey { get; set; }

/// <summary>
/// The production API key to use. Set either this key if you don't want preview content and have secured your API (paid subscribtion required)./>.
/// The production API key to use. Set either this key if you don't want preview content and have secured your API (paid subscription required)./>.
/// </summary>
public string ProductionApiKey { get; set; }
public string? ProductionApiKey { get; set; }

/// <summary>
/// The Kontent project id. This is required.
/// The Kontent project id. This is required unless you provide your own Kontent client instance.
/// </summary>
public string ProjectId { get; }
public string? ProjectId { get; }

/// <summary>
/// The code name of the field uses to fill the main Content field on the Statiq document. This is mostly useful for untyped content.
/// </summary>
public string ContentField { get; set; }
public string? ContentField { get; set; }

private readonly Lazy<IDeliveryClient> _client;

Expand All @@ -58,7 +58,7 @@ public Kontent(IDeliveryClient client)
/// </summary>
/// <param name="projectId">Kontent project ID</param>
/// <param name="previewApiKey">The preview API key (optional)</param>
public Kontent(string projectId, string previewApiKey = null)
public Kontent(string projectId, string? previewApiKey = null)
{
if (string.IsNullOrWhiteSpace(projectId))
throw new ArgumentException("Value cannot be null or whitespace.", nameof(projectId));
Expand Down Expand Up @@ -122,6 +122,9 @@ private async Task<IDocument> CreateDocument(IExecutionContext context, ContentI

foreach (var element in item.Elements)
{
if( element == null )
continue;

string type = element.Value.type;

switch (type)
Expand Down
15 changes: 0 additions & 15 deletions Kontent.Statiq/KontentAssetDownload.cs

This file was deleted.

Loading

0 comments on commit e79457b

Please sign in to comment.