2018-06-29 19:52:40 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
2020-05-20 13:51:21 +02:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2020-06-09 13:01:05 +10:00
|
|
|
|
using Umbraco.Core;
|
2018-08-29 01:15:46 +10:00
|
|
|
|
using Umbraco.Core.Configuration.UmbracoSettings;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
using Umbraco.Core.IO;
|
2020-03-24 09:37:46 +01:00
|
|
|
|
using Umbraco.Core.Media;
|
2020-02-07 15:01:03 -08:00
|
|
|
|
using Umbraco.Core.Models;
|
2020-05-20 13:51:21 +02:00
|
|
|
|
using Umbraco.Web.Common.Attributes;
|
2020-05-20 17:39:07 +02:00
|
|
|
|
using Umbraco.Web.Models;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2020-06-03 17:17:30 +02:00
|
|
|
|
namespace Umbraco.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
|
|
|
|
|
|
{
|
2018-10-26 15:06:53 +02:00
|
|
|
|
private readonly IMediaFileSystem _mediaFileSystem;
|
2020-03-12 15:30:22 +01:00
|
|
|
|
private readonly IContentSettings _contentSettings;
|
2020-02-07 15:01:03 -08:00
|
|
|
|
private readonly IImageUrlGenerator _imageUrlGenerator;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2020-03-12 15:30:22 +01:00
|
|
|
|
public ImagesController(IMediaFileSystem mediaFileSystem, IContentSettings contentSettings, IImageUrlGenerator imageUrlGenerator)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
|
|
|
|
|
_mediaFileSystem = mediaFileSystem;
|
2020-03-12 15:30:22 +01:00
|
|
|
|
_contentSettings = contentSettings;
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
// we need to check if it is an image by extension
|
2020-03-12 15:30:22 +01:00
|
|
|
|
if (_contentSettings.IsImageFile(ext) == false)
|
2020-05-20 13:51:21 +02:00
|
|
|
|
return NotFound();
|
2018-06-29 19:52:40 +02: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
|
|
|
|
|
|
{
|
|
|
|
|
|
imageLastModified = _mediaFileSystem.GetLastModified(imagePath);
|
2020-05-20 13:51:21 +02:00
|
|
|
|
|
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-05-20 17:39:07 +02:00
|
|
|
|
var imageUrl = _imageUrlGenerator.GetImageUrl(new ImageUrlGenerationOptions(imagePath) { UpScale = false, Width = width, AnimationProcessMode = "first", ImageCropMode = ImageCropMode.Max, CacheBusterValue = rnd });
|
2019-12-18 16:10:33 +11:00
|
|
|
|
|
2020-05-20 13:51:21 +02:00
|
|
|
|
return new RedirectResult(imageUrl, false);
|
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="animationProcessMode"></param>
|
|
|
|
|
|
/// <param name="mode"></param>
|
|
|
|
|
|
/// <param name="upscale"></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,
|
|
|
|
|
|
int? width = null,
|
|
|
|
|
|
int? height = null,
|
|
|
|
|
|
int? focalPointLeft = null,
|
|
|
|
|
|
int? focalPointTop = null,
|
|
|
|
|
|
string animationProcessMode = "first",
|
2020-05-20 17:39:07 +02:00
|
|
|
|
ImageCropMode mode = ImageCropMode.Max,
|
2020-03-24 12:19:04 +01:00
|
|
|
|
bool upscale = false,
|
|
|
|
|
|
string cacheBusterValue = "")
|
|
|
|
|
|
{
|
|
|
|
|
|
var options = new ImageUrlGenerationOptions(imagePath)
|
|
|
|
|
|
{
|
2020-03-24 12:29:16 +01:00
|
|
|
|
AnimationProcessMode = animationProcessMode,
|
2020-03-24 12:19:04 +01:00
|
|
|
|
CacheBusterValue = cacheBusterValue,
|
|
|
|
|
|
Height = height,
|
2020-03-24 12:29:16 +01:00
|
|
|
|
ImageCropMode = mode,
|
|
|
|
|
|
UpScale = upscale,
|
2020-03-24 12:19:04 +01:00
|
|
|
|
Width = width,
|
|
|
|
|
|
};
|
|
|
|
|
|
if (focalPointLeft.HasValue && focalPointTop.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
options.FocalPoint = new ImageUrlGenerationOptions.FocalPointPosition(focalPointTop.Value, focalPointLeft.Value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return _imageUrlGenerator.GetImageUrl(options);
|
|
|
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|