Skip to content

Commit

Permalink
Feature/maintain domain (#8)
Browse files Browse the repository at this point in the history
Keep original image domain
  • Loading branch information
ErikHen authored Sep 12, 2022
1 parent 5d40f6a commit 40d34ae
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion PictureRenderer/PictureRenderer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>3.3.1</Version>
<Version>3.4</Version>
<Authors>Erik Henningson</Authors>
<Company />
<Product />
Expand Down
11 changes: 9 additions & 2 deletions PictureRenderer/PictureUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,14 @@ private static string BuildQueryString(Uri uri, PictureProfileBase profile, int
// "quality" have to be after "format".
queryItems = AddQualityQuery(queryItems, profile);

return uri.AbsolutePath + "?" + queryItems.ToString();
var domain = string.Empty;
if (!uri.Host.Contains("dummy-xyz.com"))
{
//keep the original image url domain.
domain = uri.GetLeftPart(UriPartial.Authority);
}

return domain + uri.AbsolutePath + "?" + queryItems.ToString();
}

private static NameValueCollection AddFocalPointQuery((double x, double y) focalPoint, NameValueCollection queryItems)
Expand Down Expand Up @@ -172,7 +179,7 @@ private static Uri GetUriFromPath(string imagePath)
{
if (!Uri.IsWellFormedUriString(imagePath, UriKind.Absolute))
{
imagePath = "https://dummy.com" + imagePath; //to be able to use the Uri object.
imagePath = "https://dummy-xyz.com" + imagePath; //to be able to use the Uri object.
if (!Uri.IsWellFormedUriString(imagePath, UriKind.Absolute))
{
throw new ArgumentException($"Image url '{imagePath}' is not well formatted.");
Expand Down
22 changes: 22 additions & 0 deletions PictureRendererTests/PictureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,28 @@ public void RenderWithImgWidthTest()
Assert.Equal(expected, result);
}

[Fact()]
public void RenderWithQuerystringTest()
{
const string expected = "<picture><source srcset=\"/myImage.jpg?format=webp&width=150&height=150&quality=20 150w, /myImage.jpg?format=webp&width=300&height=300&quality=20 300w\" sizes=\"150px\" type=\"image/webp\"/><source srcset=\"/myImage.jpg?width=150&height=150&quality=20 150w, /myImage.jpg?width=300&height=300&quality=20 300w\" sizes=\"150px\" /><img alt=\"alt text\" src=\"/myImage.jpg?width=400&height=400&quality=20\" loading=\"lazy\" decoding=\"async\" /></picture>";
var profile = GetTestImageProfile();

var result = PictureRenderer.Picture.Render("/myImage.jpg?quality=20", profile, "alt text");

Assert.Equal(expected, result);
}

[Fact()]
public void RenderWithDomainTest()
{
const string expected = "<picture><source srcset=\"https://mydomain.com/myImage.jpg?format=webp&width=150&height=150&quality=7 150w, https://mydomain.com/myImage.jpg?format=webp&width=300&height=300&quality=7 300w\" sizes=\"150px\" type=\"image/webp\"/><source srcset=\"https://mydomain.com/myImage.jpg?width=150&height=150&quality=7 150w, https://mydomain.com/myImage.jpg?width=300&height=300&quality=7 300w\" sizes=\"150px\" /><img alt=\"alt text\" src=\"https://mydomain.com/myImage.jpg?width=400&height=400&quality=7\" loading=\"lazy\" decoding=\"async\" /></picture>";
var profile = GetTestImageProfile();

var result = PictureRenderer.Picture.Render("https://mydomain.com/myImage.jpg?quality=7", profile, "alt text");

Assert.Equal(expected, result);
}

private static ImageSharpProfile GetTestImageProfile()
{
//use this to test with both single and multiple images
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ See also [sample projects](https://github.com/ErikHen/PictureRenderer.Samples).
<br><br>

## Version history
* **3.4** Keep domain in image urls (earlier it always returned an absolute path).
* **3.3.1** Add space between attributes.
* **3.3** Adding possibility to set width attribute on img element. Needed for rare edge case in Optimizely CMS.
* **3.2** Changing some things reported as warnings by Sonarcloud.
Expand Down

0 comments on commit 40d34ae

Please sign in to comment.