Skip to content

Commit

Permalink
Merge branch 'release/5.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
jstemerdink committed Aug 24, 2023
2 parents 1e22bf7 + 8bf99a9 commit 611bc24
Show file tree
Hide file tree
Showing 37 changed files with 1,335 additions and 891 deletions.
Binary file modified EPi.Libraries.FavIcons.ImageResizer/GlobalSuppressions.cs
Binary file not shown.
44 changes: 22 additions & 22 deletions EPi.Libraries.FavIcons.ImageResizer/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
// Copyright © 2022 Jeroen Stemerdink.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AssemblyInfo.cs" company="Jeroen Stemerdink">
// Copyright © 2023 Jeroen Stemerdink.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Reflection;
Expand Down
2 changes: 1 addition & 1 deletion EPi.Libraries.FavIcons.ImageResizer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ By Jeroen Stemerdink
[![Platform](https://img.shields.io/badge/platform-.NET%206-blue.svg?style=flat)](https://msdn.microsoft.com/en-us/library/w0x726c2%28v=vs.110%29.aspx)
[![Platform](https://img.shields.io/badge/EPiServer-%2012-orange.svg?style=flat)](http://world.episerver.com/cms/)
[![NuGet](https://img.shields.io/badge/NuGet-Release-blue.svg)](http://nuget.episerver.com/en/OtherPages/Package/?packageId=EPi.Libraries.Favicons.ImageResizer)
[![GitHub license](https://img.shields.io/badge/license-MIT%20license-blue.svg?style=flat)](../LICENSE)
[![GitHub license](https://img.shields.io/badge/license-MIT%20license-blue.svg?style=flat)](../license.txt)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=jstemerdink%3AEPi.Libraries.Favicons&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=jstemerdink%3AEPi.Libraries.Favicons)

## Description
Expand Down
149 changes: 88 additions & 61 deletions EPi.Libraries.FavIcons.ImageResizer/ResizeService.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
// Copyright © 2022 Jeroen Stemerdink.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ResizeService.cs" company="Jeroen Stemerdink">
// Copyright © 2023 Jeroen Stemerdink.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace EPi.Libraries.Favicons.ImageResizer
{
using System;
using System.Globalization;
using EPi.Libraries.Favicons.Business.Services;

using Business.Services;

using EPiServer;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.DataAccess;
using EPiServer.Framework.Blobs;
using EPiServer.Logging;
using EPiServer.ServiceLocation;

using Imageflow.Fluent;

using Microsoft.Extensions.Logging;

/// <summary>
/// Class ResizeService.
/// </summary>
Expand All @@ -46,6 +47,32 @@ namespace EPi.Libraries.Favicons.ImageResizer
[ServiceConfiguration(typeof(IResizeService), Lifecycle = ServiceInstanceScope.Singleton)]
public class ResizeService : ResizeServiceBase
{
private readonly ILogger<ResizeService> logger;

/// <summary>
/// Initializes a new instance of the <see cref="ResizeService" /> class.
/// </summary>
/// <param name="contentRepository">The content repository.</param>
/// <param name="contentTypeRepository">The content type repository.</param>
/// <param name="contentMediaResolver">The content media resolver.</param>
/// <param name="blobFactory">The BLOB factory.</param>
/// <param name="logger">The logger.</param>
public ResizeService(
IContentRepository contentRepository,
IContentTypeRepository contentTypeRepository,
ContentMediaResolver contentMediaResolver,
IBlobFactory blobFactory,
ILogger<ResizeService> logger)
: base(
contentRepository: contentRepository,
contentTypeRepository: contentTypeRepository,
contentMediaResolver: contentMediaResolver,
blobFactory: blobFactory,
logger: logger)
{
this.logger = logger;
}

/// <summary>
/// Creates the favicon.
/// </summary>
Expand All @@ -61,82 +88,82 @@ public override void CreateFavicon(
int width,
int height)
{
// Get a suitable MediaData type from extension
Type mediaType = this.ContentMediaResolver.Service.GetFirstMatching(".png");

ContentType contentType = this.ContentTypeRepository.Service.Load(mediaType);

try
{
if (imageBytes.Length == 0)
{
Logger.Debug("[Favicons] Error creating icon. Original file is empty.");
this.logger.Log(
logLevel: LogLevel.Debug,
"[Favicons] Error creating icon. Original file is empty.");
return;
}

ArraySegment<byte>? processedImageData = this.ProcessImage(imageBytes, width, height);
ArraySegment<byte>? processedImageData = this.ProcessImage(
imageBytes: imageBytes,
width: width,
height: height);

if (!processedImageData.HasValue)
{
Logger.Debug("[Favicons] Error creating icon. Processed file is empty.");
this.logger.Log(
logLevel: LogLevel.Debug,
"[Favicons] Error creating icon. Processed file is empty.");
return;
}

byte[] processedImageBytes = processedImageData.Value.Array;

if (processedImageBytes?.Length == 0)
{
Logger.Debug("[Favicons] Error creating icon. Processed file is empty.");
this.logger.Log(
logLevel: LogLevel.Debug,
"[Favicons] Error creating icon. Processed file is empty.");
return;
}

// Get a suitable MediaData type from extension
Type mediaType = this.ContentMediaResolver.GetFirstMatching(".png");

ContentType contentType = this.ContentTypeRepository.Load(modelType: mediaType);

// Get a new empty file data
ImageData media = this.ContentRepository.Service.GetDefault<ImageData>(rootFolder, contentType.ID);
ImageData media = this.ContentRepository.GetDefault<ImageData>(
parentLink: rootFolder,
contentTypeID: contentType.ID);

media.Name = string.Format(CultureInfo.InvariantCulture, "{0}-{1}x{2}.png", filePrefix, width, height);
media.Name = string.Format(
provider: CultureInfo.InvariantCulture,
"{0}-{1}x{2}.png",
arg0: filePrefix,
arg1: width,
arg2: height);

// Create a blob in the binary container
Blob blob = this.BlobFactory.Service.CreateBlob(media.BinaryDataContainer, ".png");
blob.WriteAllBytes(processedImageBytes);
Blob blob = this.BlobFactory.CreateBlob(id: media.BinaryDataContainer, ".png");

blob.WriteAllBytes(data: processedImageBytes);

// Assign to file and publish changes
media.BinaryData = blob;
this.ContentRepository.Service.Save(media, SaveAction.Publish);
}
catch (AccessDeniedException accessDeniedException)
{
Logger.Error("[Favicons] Error creating icon.", accessDeniedException);
}
catch (ArgumentNullException argumentNullException)
{
Logger.Error("[Favicons] Error creating icon.", argumentNullException);
}
catch (FormatException formatException)
{
Logger.Error("[Favicons] Error creating icon.", formatException);
this.ContentRepository.Save(content: media, action: SaveAction.Publish);
}
catch (Exception exception)
{
Logger.Error("[Favicons] Error creating icon.", exception);
this.logger.Log(logLevel: LogLevel.Error, exception: exception, "[Favicons] Error creating icon.");
}
}

private ArraySegment<byte>? ProcessImage(byte[] imageBytes, int width, int height)
{
using (ImageJob b = new())
{
BuildNode buildNode = b.Decode(imageBytes);
BuildNode buildNode = b.Decode(source: imageBytes);

BuildJobResult r = buildNode
.ResizerCommands($"width={width}&height={height}&crop=auto&format=png")
.EncodeToBytes(new PngQuantEncoder(100, 80))
.Finish()
.InProcessAsync()
.Result;
BuildJobResult r = buildNode.ResizerCommands($"width={width}&height={height}&crop=auto&format=png")
.EncodeToBytes(new PngQuantEncoder(100, 80)).Finish().InProcessAsync().Result;

return r.First.TryGetBytes();
}
}
}
}
}
Binary file modified EPi.Libraries.Favicons.ImageProcessor/GlobalSuppressions.cs
Binary file not shown.
25 changes: 24 additions & 1 deletion EPi.Libraries.Favicons.ImageProcessor/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
using System;
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AssemblyInfo.cs" company="Jeroen Stemerdink">
// Copyright © 2023 Jeroen Stemerdink.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
Expand Down
2 changes: 1 addition & 1 deletion EPi.Libraries.Favicons.ImageProcessor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ By Jeroen Stemerdink
[![Platform](https://img.shields.io/badge/platform-.NET%207-blue.svg?style=flat)](https://msdn.microsoft.com/en-us/library/w0x726c2%28v=vs.110%29.aspx)
[![Platform](https://img.shields.io/badge/EPiServer-%2012-orange.svg?style=flat)](http://world.episerver.com/cms/)
[![NuGet](https://img.shields.io/badge/NuGet-Release-blue.svg)](http://nuget.episerver.com/en/OtherPages/Package/?packageId=EPi.Libraries.Favicons)
[![GitHub license](https://img.shields.io/badge/license-MIT%20license-blue.svg?style=flat)](LICENSE)
[![GitHub license](https://img.shields.io/badge/license-MIT%20license-blue.svg?style=flat)](../license.txt)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=jstemerdink%3AEPi.Libraries.Favicons&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=jstemerdink%3AEPi.Libraries.Favicons)

## Description
Expand Down
Loading

0 comments on commit 611bc24

Please sign in to comment.