Add ImageUrlGeneratorController

This commit is contained in:
Benjamin Carleski
2020-02-08 16:49:12 -08:00
parent 408ee452e1
commit 7e7d38e74b
5 changed files with 82 additions and 1 deletions

View File

@@ -0,0 +1,36 @@
/**
* @ngdoc service
* @name umbraco.resources.imageUrlGeneratorResource
* @function
*
* @description
* Used by the various controllers to get an image URL formatted correctly for the current image URL generator
*/
(function () {
'use strict';
function imageUrlGeneratorResource($http, umbRequestHelper) {
function getCropUrl(mediaPath, width, height, imageCropMode, animationProcessMode) {
return umbRequestHelper.resourcePromise(
$http.get(
umbRequestHelper.getApiUrl(
"imageUrlGeneratorApiBaseUrl",
"GetCropUrl",
{ mediaPath, width, height, imageCropMode, animationProcessMode })),
'Failed to get crop URL');
}
var resource = {
getCropUrl: getCropUrl
};
return resource;
}
angular.module('umbraco.resources').factory('imageUrlGeneratorResource', imageUrlGeneratorResource);
})();

View File

@@ -314,6 +314,10 @@ namespace Umbraco.Web.Editors
"tinyMceApiBaseUrl", _urlHelper.GetUmbracoApiServiceBaseUrl<TinyMceController>(
controller => controller.UploadImage())
},
{
"imageUrlGeneratorApiBaseUrl", _urlHelper.GetUmbracoApiServiceBaseUrl<ImageUrlGeneratorController>(
controller => controller.GetCropUrl(null, null, null, null, null))
},
}
},
{

View File

@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Umbraco.Core.Models;
using Umbraco.Web.Models;
using Umbraco.Web.Mvc;
namespace Umbraco.Web.Editors
{
/// <summary>
/// The API controller used for getting URLs for images with parameters
/// </summary>
/// <remarks>
/// <para>
/// This controller allows for retrieving URLs for processed images, such as resized, cropped,
/// or otherwise altered. These can be different based on the IImageUrlGenerator
/// implementation in use, and so back-office could should not rely on hard-coded string
/// building to generate correct URLs
/// </para>
/// </remarks>
[PluginController("UmbracoApi")]
public class ImageUrlGeneratorController
{
private readonly IImageUrlGenerator _imageUrlGenerator;
public ImageUrlGeneratorController(IImageUrlGenerator imageUrlGenerator)
{
_imageUrlGenerator = imageUrlGenerator;
}
public string GetCropUrl(string mediaPath, int? width = null, int? height = null, ImageCropMode? imageCropMode = null, string animationProcessMode = null)
{
return mediaPath.GetCropUrl(_imageUrlGenerator, null, width: width, height: height, imageCropMode: imageCropMode, animationProcessMode: animationProcessMode);
}
}
}

View File

@@ -306,7 +306,8 @@ namespace Umbraco.Web
string cacheBusterValue = null,
string furtherOptions = null,
ImageCropRatioMode? ratioMode = null,
bool upScale = true)
bool upScale = true,
string animationProcessMode = null)
{
if (string.IsNullOrEmpty(imageUrl)) return string.Empty;
@@ -353,6 +354,7 @@ namespace Umbraco.Web
options.Quality = quality;
options.Width = ratioMode != null && ratioMode.Value == ImageCropRatioMode.Width ? null : width;
options.Height = ratioMode != null && ratioMode.Value == ImageCropRatioMode.Height ? null : height;
options.AnimationProcessMode = animationProcessMode;
if (ratioMode == ImageCropRatioMode.Width && height != null)
{

View File

@@ -150,6 +150,7 @@
<Compile Include="Editors\BackOfficePreviewModel.cs" />
<Compile Include="Editors\Filters\ContentSaveModelValidator.cs" />
<Compile Include="Editors\Filters\MediaSaveModelValidator.cs" />
<Compile Include="Editors\ImageUrlGeneratorController.cs" />
<Compile Include="Editors\PackageController.cs" />
<Compile Include="Editors\KeepAliveController.cs" />
<Compile Include="Editors\MacrosController.cs" />