2021-08-09 15:52:55 +02:00
using System ;
2021-08-06 12:59:56 +02:00
using System.Collections.Generic ;
2020-09-22 15:06:03 +02:00
using System.Globalization ;
2021-08-09 15:52:55 +02:00
using System.Linq ;
2020-02-07 15:01:03 -08:00
using System.Text ;
2021-08-09 15:52:55 +02:00
using SixLabors.ImageSharp ;
2021-02-09 10:22:42 +01:00
using Umbraco.Cms.Core.Media ;
using Umbraco.Cms.Core.Models ;
2020-02-07 15:01:03 -08:00
2021-02-12 12:33:52 +01:00
namespace Umbraco.Cms.Infrastructure.Media
2020-02-07 15:01:03 -08:00
{
2021-08-13 14:23:27 +02:00
/// <summary>
/// Exposes a method that generates an image URL based on the specified options that can be processed by ImageSharp.
/// </summary>
/// <seealso cref="Umbraco.Cms.Core.Media.IImageUrlGenerator" />
2020-03-30 21:27:35 +02:00
public class ImageSharpImageUrlGenerator : IImageUrlGenerator
2020-02-07 15:01:03 -08:00
{
2021-08-09 15:52:55 +02:00
private static readonly string [ ] s_supportedImageFileTypes = Configuration . Default . ImageFormats . SelectMany ( f = > f . FileExtensions ) . ToArray ( ) ;
2021-08-13 14:23:27 +02:00
/// <inheritdoc />
/// <remarks>
/// This uses the default instance of the ImageSharp configuration, so we need to ensure we don't new up a different instance when configuring the middleware.
/// </remarks>
2021-08-09 15:52:55 +02:00
public IEnumerable < string > SupportedImageFileTypes { get ; } = s_supportedImageFileTypes ;
2020-09-22 15:06:03 +02:00
2021-08-13 14:23:27 +02:00
/// <inheritdoc/>
2020-02-07 15:01:03 -08:00
public string GetImageUrl ( ImageUrlGenerationOptions options )
{
2021-08-06 14:22:27 +02:00
if ( options = = null )
{
return null ;
}
2020-02-08 11:05:14 -08:00
2021-08-06 14:22:27 +02:00
var imageUrl = new StringBuilder ( options . ImageUrl ) ;
2020-02-07 15:01:03 -08:00
2021-08-09 15:52:55 +02:00
bool queryStringHasStarted = false ;
void AppendQueryString ( string value )
2021-08-06 14:22:27 +02:00
{
2021-08-09 15:52:55 +02:00
imageUrl . Append ( queryStringHasStarted ? '&' : '?' ) ;
queryStringHasStarted = true ;
2020-02-07 15:01:03 -08:00
2021-08-09 15:52:55 +02:00
imageUrl . Append ( value ) ;
2020-02-07 15:01:03 -08:00
}
2021-08-09 15:52:55 +02:00
void AddQueryString ( string key , params IConvertible [ ] values )
= > AppendQueryString ( key + '=' + string . Join ( "," , values . Select ( x = > x . ToString ( CultureInfo . InvariantCulture ) ) ) ) ;
2020-02-07 15:01:03 -08:00
2021-08-09 15:52:55 +02:00
if ( options . FocalPoint ! = null )
2021-08-06 14:22:27 +02:00
{
2021-08-10 17:17:40 +02:00
AddQueryString ( "rxy" , options . FocalPoint . Left , options . FocalPoint . Top ) ;
2021-08-06 14:22:27 +02:00
}
2020-02-09 11:12:29 -08:00
2021-08-09 15:52:55 +02:00
if ( options . Crop ! = null )
2021-08-06 14:22:27 +02:00
{
2021-08-10 23:55:37 +02:00
AddQueryString ( "cc" , options . Crop . Left , options . Crop . Top , options . Crop . Right , options . Crop . Bottom ) ;
2021-08-06 14:22:27 +02:00
}
2020-02-09 11:12:29 -08:00
2021-08-09 15:52:55 +02:00
if ( options . ImageCropMode . HasValue )
2021-08-06 14:22:27 +02:00
{
2021-08-09 15:52:55 +02:00
AddQueryString ( "rmode" , options . ImageCropMode . Value . ToString ( ) . ToLowerInvariant ( ) ) ;
2021-08-06 14:22:27 +02:00
}
2021-08-09 15:52:55 +02:00
if ( options . ImageCropAnchor . HasValue )
2021-08-06 14:22:27 +02:00
{
2021-08-09 15:52:55 +02:00
AddQueryString ( "ranchor" , options . ImageCropAnchor . Value . ToString ( ) . ToLowerInvariant ( ) ) ;
2021-08-06 14:22:27 +02:00
}
2021-08-09 15:52:55 +02:00
if ( options . Width . HasValue )
2021-08-06 14:22:27 +02:00
{
2021-08-09 15:52:55 +02:00
AddQueryString ( "width" , options . Width . Value ) ;
2021-08-06 14:22:27 +02:00
}
2021-08-09 15:52:55 +02:00
if ( options . Height . HasValue )
2021-08-06 14:22:27 +02:00
{
2021-08-09 15:52:55 +02:00
AddQueryString ( "height" , options . Height . Value ) ;
2021-08-06 14:22:27 +02:00
}
2021-08-09 15:52:55 +02:00
if ( options . Quality . HasValue )
2021-08-06 14:22:27 +02:00
{
2021-08-09 15:52:55 +02:00
AddQueryString ( "quality" , options . Quality . Value ) ;
2021-08-06 14:22:27 +02:00
}
2021-08-11 17:18:23 +02:00
if ( string . IsNullOrWhiteSpace ( options . FurtherOptions ) = = false )
2021-08-06 14:22:27 +02:00
{
2021-08-09 15:52:55 +02:00
AppendQueryString ( options . FurtherOptions . TrimStart ( '?' , '&' ) ) ;
2021-08-06 14:22:27 +02:00
}
2021-08-11 17:18:23 +02:00
if ( string . IsNullOrWhiteSpace ( options . CacheBusterValue ) = = false )
2021-08-06 14:22:27 +02:00
{
2021-08-09 15:52:55 +02:00
AddQueryString ( "rnd" , options . CacheBusterValue ) ;
2021-08-06 14:22:27 +02:00
}
return imageUrl . ToString ( ) ;
2020-02-09 11:12:29 -08:00
}
2020-02-07 15:01:03 -08:00
}
}