Skip to content

Commit

Permalink
Merge pull request #150 from contentful/fix/improve-imgurlbuilder-for…
Browse files Browse the repository at this point in the history
…mats

Fix/improve imgurlbuilder formats
  • Loading branch information
Roblinde authored Aug 21, 2018
2 parents f43d614 + 309e176 commit c78395e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
8 changes: 4 additions & 4 deletions Contentful.AspNetCore/Contentful.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Description>Official .NET SDK for the Contentful Content Delivery and Management API for ASP.NET core.</Description>
<PackageId>contentful.aspnetcore</PackageId>
<NeutralLanguage>en-US</NeutralLanguage>
<VersionPrefix>3.3.0</VersionPrefix>
<VersionPrefix>3.3.1</VersionPrefix>
<TargetFramework>netstandard2.0</TargetFramework>
<Authors>Contentful</Authors>
<Copyright>Contentful GmbH.</Copyright>
Expand All @@ -14,14 +14,14 @@
<PackageLicenseUrl>https://github.com/contentful/contentful.net/master/LICENSE</PackageLicenseUrl>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>git://github.com/contentful/contentful.net</RepositoryUrl>
<Version>3.3.0</Version>
<AssemblyVersion>3.3.0.0</AssemblyVersion>
<Version>3.3.1</Version>
<AssemblyVersion>3.3.1.0</AssemblyVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DocumentationFile>bin\Release\netstandard1.5\Contentful.AspNetCore.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="contentful.csharp" Version="3.3.0" />
<PackageReference Include="contentful.csharp" Version="3.3.1" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Razor" Version="2.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Runtime" Version="2.1.0" />
Expand Down
22 changes: 22 additions & 0 deletions Contentful.Core.Tests/Images/ImageUrlBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,27 @@ public void SettingBackgroundShouldAddCorrectQueryString(string bg, string expec
//Assert
Assert.Equal(expected, result);
}

[Fact]
public void SettingFormatToPngShouldNotAllowForJpgSpecificValues()
{
//Arrange
var builder = new ImageUrlBuilder();
//Act
var result = builder.SetFormat(ImageFormat.Png).SetJpgQuality(34).UseProgressiveJpg().Build();
//Assert
Assert.Equal("?fm=png", result);
}

[Fact]
public void SettingFormatToJpgShouldAllowForJpgSpecificValues()
{
//Arrange
var builder = new ImageUrlBuilder();
//Act
var result = builder.SetFormat(ImageFormat.Jpg).SetJpgQuality(34).UseProgressiveJpg().Build();
//Assert
Assert.Equal("?fm=jpg&q=34&fl=progressive", result);
}
}
}
8 changes: 4 additions & 4 deletions Contentful.Core/Contentful.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PackageId>contentful.csharp</PackageId>
<AssemblyTitle>contentful.net</AssemblyTitle>
<NeutralLanguage>en-US</NeutralLanguage>
<VersionPrefix>3.3.0</VersionPrefix>
<VersionPrefix>3.3.1</VersionPrefix>
<TargetFramework>netstandard2.0</TargetFramework>
<Authors>Contentful</Authors>
<Copyright>Contentful GmbH.</Copyright>
Expand All @@ -18,9 +18,9 @@
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<AssemblyVersion>3.3.0.0</AssemblyVersion>
<FileVersion>3.3.0.0</FileVersion>
<Version>3.3.0</Version>
<AssemblyVersion>3.3.1.0</AssemblyVersion>
<FileVersion>3.3.1.0</FileVersion>
<Version>3.3.1</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.5.0" />
Expand Down
18 changes: 18 additions & 0 deletions Contentful.Core/Images/ImageUrlBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public ImageUrlBuilder SetBackgroundColor(string rgbColor)
/// <returns>The formatted querystring.</returns>
public string Build()
{
ClearImproperValues();
var sb = new StringBuilder();
var hasQuery = false;

Expand All @@ -188,5 +189,22 @@ public string Build()

return sb.ToString();
}

private void ClearImproperValues()
{
// If the querystrings contain both jpg specific values and a specific format, make sure the format is jpg, else remove the jpg specific values.
if (_querystringValues.Any(c => c.Key == "q" || c.Key == "fl") && _querystringValues.Any(c => c.Key == "fm")){
var format = _querystringValues.First(c => c.Key == "fm").Value;

if(format != "jpg")
{
var quality = _querystringValues.FirstOrDefault(c => c.Key == "q");
var progressive = _querystringValues.FirstOrDefault(c => c.Key == "fl" && c.Value == "progressive");

_querystringValues.Remove(quality);
_querystringValues.Remove(progressive);
}
}
}
}
}

0 comments on commit c78395e

Please sign in to comment.