Browse Source

Make asset formats configurable.

pull/1056/head
Sebastian 3 years ago
parent
commit
dac32542d6
  1. 4
      backend/src/Squidex.Domain.Apps.Entities/Assets/AssetOptions.cs
  2. 13
      backend/src/Squidex/Areas/Api/Controllers/Assets/AssetContentController.cs
  3. 24
      backend/src/Squidex/Areas/Api/Controllers/Assets/Models/AssetContentQueryDto.cs
  4. 10
      backend/src/Squidex/appsettings.json

4
backend/src/Squidex.Domain.Apps.Entities/Assets/AssetOptions.cs

@ -13,6 +13,10 @@ public sealed class AssetOptions
public bool CanCache { get; set; }
public bool AllowAvifAuto { get; set; }
public bool AllowWebpAuto { get; set; } = true;
public int DefaultPageSize { get; set; } = 200;
public int MaxResults { get; set; } = 200;

13
backend/src/Squidex/Areas/Api/Controllers/Assets/AssetContentController.cs

@ -8,6 +8,7 @@
using System.Globalization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;
using Squidex.Areas.Api.Controllers.Assets.Models;
using Squidex.Assets;
@ -30,19 +31,22 @@ public sealed class AssetContentController : ApiController
private readonly IAssetQueryService assetQuery;
private readonly IAssetLoader assetLoader;
private readonly IAssetThumbnailGenerator assetGenerator;
private readonly AssetOptions assetOptions;
public AssetContentController(
ICommandBus commandBus,
IAssetFileStore assetFileStore,
IAssetQueryService assetQuery,
IAssetLoader assetLoader,
IAssetThumbnailGenerator assetGenerator)
IAssetThumbnailGenerator assetGenerator,
IOptions<AssetOptions> assetOptions)
: base(commandBus)
{
this.assetFileStore = assetFileStore;
this.assetQuery = assetQuery;
this.assetLoader = assetLoader;
this.assetGenerator = assetGenerator;
this.assetOptions = assetOptions.Value;
}
/// <summary>
@ -138,7 +142,12 @@ public sealed class AssetContentController : ApiController
Response.Headers[HeaderNames.CacheControl] = $"public,max-age={request.CacheDuration}";
}
var resizeOptions = request.ToResizeOptions(asset, assetGenerator, HttpContext.Request);
var resizeOptions = request.ToResizeOptions(
asset,
assetOptions.AllowAvifAuto,
assetOptions.AllowWebpAuto,
assetGenerator,
HttpContext.Request);
var contentLength = (long?)null;
var contentCallback = (FileCallback?)null;

24
backend/src/Squidex/Areas/Api/Controllers/Assets/Models/AssetContentQueryDto.cs

@ -105,7 +105,12 @@ public sealed class AssetContentQueryDto
[FromQuery(Name = "format")]
public ImageFormat? Format { get; set; }
public ResizeOptions ToResizeOptions(Asset asset, IAssetThumbnailGenerator assetGenerator, HttpRequest request)
public ResizeOptions ToResizeOptions(
Asset asset,
bool allowAvifAuto,
bool allowWebpAuto,
IAssetThumbnailGenerator assetGenerator,
HttpRequest request)
{
Guard.NotNull(asset);
@ -117,12 +122,17 @@ public sealed class AssetContentQueryDto
result.FocusY = y;
result.TargetWidth = Width;
result.TargetHeight = Height;
result.Format = GetFormat(asset, assetGenerator, request);
result.Format = GetFormat(asset, allowAvifAuto, allowWebpAuto, assetGenerator, request);
return result;
}
private ImageFormat? GetFormat(Asset asset, IAssetThumbnailGenerator assetGenerator, HttpRequest request)
private ImageFormat? GetFormat(
Asset asset,
bool allowAvifAuto,
bool allowWebpAuto,
IAssetThumbnailGenerator assetGenerator,
HttpRequest request)
{
if (Format.HasValue || !Auto)
{
@ -140,13 +150,13 @@ public sealed class AssetContentQueryDto
return accept.Any(x => x?.Contains(mimeType, StringComparison.OrdinalIgnoreCase) == true) && assetGenerator.CanReadAndWrite(mimeType);
}
#if ENABLE_AVIF
if (Accepts("image/avif"))
if (allowAvifAuto && Accepts("image/avif"))
{
return ImageFormat.AVIF;
}
#endif
if (Accepts("image/webp"))
if (allowWebpAuto && Accepts("image/webp"))
{
return ImageFormat.WEBP;
}

10
backend/src/Squidex/appsettings.json

@ -297,6 +297,16 @@
// The timeout when searching for multiple items in the database.
"timeoutQuery": "00:00:05",
// Deliver the assets in AVIG format automatically if the browser supports that.
//
// WARNING: If you change this feature from false to true, you might experience a performance spike the images are getting prepared.
"allowAvifAuto": false,
// Deliver the assets in WEBP format automatically if the browser supports that.
//
// WARNING: If you change this feature from false to true, you might experience a performance spike the images are getting prepared.
"allowWebpAuto": true,
// Create one folder per app.
//
// WARNING: If you change this parameter, previous assets are not available anymore.

Loading…
Cancel
Save