2021-08-10 11:23:37 +02:00
|
|
|
using System;
|
2018-06-29 19:52:40 +02:00
|
|
|
using System.IO;
|
2020-05-20 13:51:21 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core.IO;
|
|
|
|
|
using Umbraco.Cms.Core.Media;
|
|
|
|
|
using Umbraco.Cms.Core.Models;
|
|
|
|
|
using Umbraco.Cms.Web.Common.Attributes;
|
|
|
|
|
using Umbraco.Extensions;
|
|
|
|
|
using Constants = Umbraco.Cms.Core.Constants;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2021-02-18 11:06:02 +01:00
|
|
|
namespace Umbraco.Cms.Web.BackOffice.Controllers
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A controller used to return images for media
|
|
|
|
|
/// </summary>
|
2020-06-09 13:01:05 +10:00
|
|
|
[PluginController(Constants.Web.Mvc.BackOfficeApiArea)]
|
2018-06-29 19:52:40 +02:00
|
|
|
public class ImagesController : UmbracoAuthorizedApiController
|
|
|
|
|
{
|
2021-04-27 09:52:17 +02:00
|
|
|
private readonly MediaFileManager _mediaFileManager;
|
2020-02-07 15:01:03 -08:00
|
|
|
private readonly IImageUrlGenerator _imageUrlGenerator;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2020-08-27 09:03:01 +02:00
|
|
|
public ImagesController(
|
2021-04-27 09:52:17 +02:00
|
|
|
MediaFileManager mediaFileManager,
|
2020-08-26 08:05:15 +02:00
|
|
|
IImageUrlGenerator imageUrlGenerator)
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2021-04-27 09:52:17 +02:00
|
|
|
_mediaFileManager = mediaFileManager;
|
2020-02-07 15:01:03 -08:00
|
|
|
_imageUrlGenerator = imageUrlGenerator;
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the big thumbnail image for the original image path
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="originalImagePath"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// If there is no original image is found then this will return not found.
|
|
|
|
|
/// </remarks>
|
2020-05-20 13:51:21 +02:00
|
|
|
public IActionResult GetBigThumbnail(string originalImagePath)
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
|
|
|
|
return string.IsNullOrWhiteSpace(originalImagePath)
|
2020-05-20 13:51:21 +02:00
|
|
|
? Ok()
|
2018-08-07 17:10:02 +10:00
|
|
|
: GetResized(originalImagePath, 500);
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a resized image for the image at the given path
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="imagePath"></param>
|
|
|
|
|
/// <param name="width"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// If there is no media, image property or image file is found then this will return not found.
|
|
|
|
|
/// </remarks>
|
2020-05-20 13:51:21 +02:00
|
|
|
public IActionResult GetResized(string imagePath, int width)
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
|
|
|
|
var ext = Path.GetExtension(imagePath);
|
2022-02-28 23:59:18 +01:00
|
|
|
|
|
|
|
|
// check if imagePath is local to prevent open redirect
|
|
|
|
|
if (!Uri.IsWellFormedUriString(imagePath, UriKind.Relative))
|
|
|
|
|
{
|
|
|
|
|
return Unauthorized();
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-29 19:52:40 +02:00
|
|
|
// we need to check if it is an image by extension
|
2020-09-22 21:12:04 +02:00
|
|
|
if (_imageUrlGenerator.IsSupportedImageFormat(ext) == false)
|
2022-02-28 23:59:18 +01:00
|
|
|
{
|
2020-05-20 13:51:21 +02:00
|
|
|
return NotFound();
|
2022-02-28 23:59:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// redirect to ImageProcessor thumbnail with rnd generated from last modified time of original media file
|
2019-12-18 16:10:33 +11:00
|
|
|
DateTimeOffset? imageLastModified = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-04-27 09:52:17 +02:00
|
|
|
imageLastModified = _mediaFileManager.FileSystem.GetLastModified(imagePath);
|
2019-12-18 16:10:33 +11:00
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
// if we get an exception here it's probably because the image path being requested is an image that doesn't exist
|
|
|
|
|
// in the local media file system. This can happen if someone is storing an absolute path to an image online, which
|
|
|
|
|
// is perfectly legal but in that case the media file system isn't going to resolve it.
|
|
|
|
|
// so ignore and we won't set a last modified date.
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-07 15:01:03 -08:00
|
|
|
var rnd = imageLastModified.HasValue ? $"&rnd={imageLastModified:yyyyMMddHHmmss}" : null;
|
2020-08-26 08:05:15 +02:00
|
|
|
var imageUrl = _imageUrlGenerator.GetImageUrl(new ImageUrlGenerationOptions(imagePath)
|
|
|
|
|
{
|
|
|
|
|
Width = width,
|
|
|
|
|
ImageCropMode = ImageCropMode.Max,
|
|
|
|
|
CacheBusterValue = rnd
|
|
|
|
|
});
|
2022-02-28 23:59:18 +01:00
|
|
|
if (Url.IsLocalUrl(imageUrl))
|
|
|
|
|
{
|
|
|
|
|
return new LocalRedirectResult(imageUrl, false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return Unauthorized();
|
|
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
2019-11-05 13:45:42 +01:00
|
|
|
|
2020-03-24 12:19:04 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a processed image for the image at the given path
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="imagePath"></param>
|
|
|
|
|
/// <param name="width"></param>
|
|
|
|
|
/// <param name="height"></param>
|
|
|
|
|
/// <param name="focalPointLeft"></param>
|
|
|
|
|
/// <param name="focalPointTop"></param>
|
|
|
|
|
/// <param name="mode"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// If there is no media, image property or image file is found then this will return not found.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public string GetProcessedImageUrl(string imagePath,
|
2020-08-26 08:05:15 +02:00
|
|
|
int? width = null,
|
|
|
|
|
int? height = null,
|
|
|
|
|
decimal? focalPointLeft = null,
|
|
|
|
|
decimal? focalPointTop = null,
|
2020-09-09 19:13:37 +02:00
|
|
|
ImageCropMode mode = ImageCropMode.Max,
|
2020-08-26 08:05:15 +02:00
|
|
|
string cacheBusterValue = "",
|
|
|
|
|
decimal? cropX1 = null,
|
|
|
|
|
decimal? cropX2 = null,
|
|
|
|
|
decimal? cropY1 = null,
|
|
|
|
|
decimal? cropY2 = null
|
|
|
|
|
)
|
|
|
|
|
{
|
2020-03-24 12:19:04 +01:00
|
|
|
var options = new ImageUrlGenerationOptions(imagePath)
|
|
|
|
|
{
|
2021-08-10 11:23:37 +02:00
|
|
|
Width = width,
|
2020-03-24 12:19:04 +01:00
|
|
|
Height = height,
|
2020-03-24 12:29:16 +01:00
|
|
|
ImageCropMode = mode,
|
2021-08-10 11:23:37 +02:00
|
|
|
CacheBusterValue = cacheBusterValue
|
2020-03-24 12:19:04 +01:00
|
|
|
};
|
2021-08-10 11:23:37 +02:00
|
|
|
|
2020-03-24 12:19:04 +01:00
|
|
|
if (focalPointLeft.HasValue && focalPointTop.HasValue)
|
|
|
|
|
{
|
2021-08-10 17:20:03 +02:00
|
|
|
options.FocalPoint = new ImageUrlGenerationOptions.FocalPointPosition(focalPointLeft.Value, focalPointTop.Value);
|
2021-08-10 11:23:37 +02:00
|
|
|
}
|
|
|
|
|
else if (cropX1.HasValue && cropX2.HasValue && cropY1.HasValue && cropY2.HasValue)
|
|
|
|
|
{
|
|
|
|
|
options.Crop = new ImageUrlGenerationOptions.CropCoordinates(cropX1.Value, cropY1.Value, cropX2.Value, cropY2.Value);
|
2020-03-24 12:19:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _imageUrlGenerator.GetImageUrl(options);
|
|
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
}
|