Skip to content

Commit

Permalink
Use route parameters in Image proxy
Browse files Browse the repository at this point in the history
Do not use query params to store token and content link as it
may interfere with image rendering at customer sites.

Closes #210
Closes #239
Closes #240
  • Loading branch information
barteksekula committed Sep 1, 2023
1 parent 43fb4e0 commit 6a5f38f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
46 changes: 40 additions & 6 deletions src/Advanced.CMS.AdvancedReviews/ImageProxyController.cs
@@ -1,7 +1,11 @@
using Advanced.CMS.ExternalReviews.ReviewLinksRepository;
using System.Collections.Generic;
using Advanced.CMS.ExternalReviews.ReviewLinksRepository;
using EPiServer;
using EPiServer.Core;
using EPiServer.Core.Internal;
using EPiServer.Framework.Blobs;
using EPiServer.ImageLibrary;
using EPiServer.Web;
using Microsoft.AspNetCore.Mvc;

namespace Advanced.CMS.AdvancedReviews
Expand All @@ -10,15 +14,21 @@ public class ImageProxyController: Controller
{
private readonly IExternalReviewLinksRepository _externalReviewLinksRepository;
private readonly IContentLoader _contentLoader;
private readonly ThumbnailManager _thumbnailManager;
private readonly IMimeTypeResolver _mimeTypeResolver;

public ImageProxyController(IExternalReviewLinksRepository externalReviewLinksRepository, IContentLoader contentLoader)
private string ThumbnailMimeType => _mimeTypeResolver.GetMimeMapping(ThumbnailHelper.ThumbnailExtension);

public ImageProxyController(IExternalReviewLinksRepository externalReviewLinksRepository,
IContentLoader contentLoader, ThumbnailManager thumbnailManager, IMimeTypeResolver mimeTypeResolver)
{
_externalReviewLinksRepository = externalReviewLinksRepository;
_contentLoader = contentLoader;
_thumbnailManager = thumbnailManager;
_mimeTypeResolver = mimeTypeResolver;
}

[HttpGet]
public IActionResult Index([FromQuery] string token, [FromQuery] string contentLink)
public IActionResult Index([FromRoute] string token, [FromRoute] string contentLink, [FromQuery] int? width, [FromQuery] int? height)
{
if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(contentLink))
{
Expand All @@ -37,12 +47,36 @@ public IActionResult Index([FromQuery] string token, [FromQuery] string contentL
}

var content = _contentLoader.Get<IContent>(contentReference);
if (!(content is ImageData imageData))
if (content is not ImageData imageData)
{
return new NotFoundResult();
}

return File(imageData.BinaryData.ReadAllBytes(), imageData.MimeType);
var returnThumbnail = width.HasValue && height.HasValue;

var originalBlobBytes = imageData.BinaryData.ReadAllBytes();
var blobToReturn = returnThumbnail ? Generate(originalBlobBytes, width.Value, height.Value) : originalBlobBytes;

return File(blobToReturn, imageData.MimeType);
}

private byte[] Generate(byte[] blobBytes, int width, int height)
{
var imgOperation = new ImageOperation(ImageEditorCommand.ResizeKeepScale, width, height)
{
// use transparency color
BackgroundColor = "#00000000"
};

try
{
return _thumbnailManager.ImageService.RenderImage(blobBytes,
new List<ImageOperation> { imgOperation }, ThumbnailMimeType, 1, 50);
}
catch
{
return null;
}
}
}
}
Expand Up @@ -21,7 +21,7 @@ public void MapEndpoints(IEndpointRouteBuilder endpointRouteBuilder)
{
var options = endpointRouteBuilder.ServiceProvider.GetInstance<ExternalReviewOptions>();

endpointRouteBuilder.MapControllerRoute("ImageProxy", "/ImageProxy",
endpointRouteBuilder.MapControllerRoute("ImageProxy", "/ImageProxy/{token}/{contentLink}",
new { controller = "ImageProxy", action = "Index" });

endpointRouteBuilder.MapControllerRoute("ExternalReviewLogin",
Expand Down
Expand Up @@ -20,7 +20,7 @@ public ExternalReviewUrlGenerator(ExternalReviewState externalReviewState)

public string GetProxiedImageUrl(ContentReference contentLink)
{
return $"/ImageProxy?token={_externalReviewState.Token}&contentLink={contentLink}";
return $"/ImageProxy/{_externalReviewState.Token}/{contentLink}";
}
}
}

0 comments on commit 6a5f38f

Please sign in to comment.