// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using Microsoft.AspNetCore.Mvc;
using Squidex.Domain.Apps.Entities.Assets;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Assets;
using Squidex.Infrastructure.Reflection;
namespace Squidex.Areas.Api.Controllers.Assets.Models
{
public sealed class AssetContentQueryDto
{
///
/// The optional version of the asset.
///
[FromQuery(Name = "version")]
public long Version { get; set; } = EtagVersion.Any;
///
/// The cache duration in seconds.
///
[FromQuery(Name = "cache")]
public long CacheDuration { get; set; }
///
/// Set it to 0 to prevent download.
///
[FromQuery(Name = "download")]
public int Download { get; set; } = 0;
///
/// The target width of the asset, if it is an image.
///
[FromQuery(Name = "width")]
public int? Width { get; set; }
///
/// The target height of the asset, if it is an image.
///
[FromQuery(Name = "height")]
public int? Height { get; set; }
///
/// Optional image quality, it is is an jpeg image.
///
[FromQuery(Name = "quality")]
public int? Quality { get; set; }
///
/// The resize mode when the width and height is defined.
///
[FromQuery(Name = "mode")]
public ResizeMode? Mode { get; set; }
///
/// Override the y focus point.
///
[FromQuery(Name = "focusX")]
public float? FocusX { get; set; }
///
/// Override the x focus point.
///
[FromQuery(Name = "focusY")]
public float? FocusY { get; set; }
///
/// True to ignore the asset focus point if any.
///
[FromQuery(Name = "nofocus")]
public bool IgnoreFocus { get; set; }
///
/// True to not use JPEG encoding when quality is set and the image is not a JPEG. Default: false.
///
[FromQuery(Name = "keepformat")]
public bool KeepFormat { get; set; }
///
/// True to force a new resize even if it already stored.
///
[FromQuery(Name = "force")]
public bool ForceResize { get; set; }
public ResizeOptions ToResizeOptions(IAssetEntity asset)
{
Guard.NotNull(asset, nameof(asset));
var result = SimpleMapper.Map(this, new ResizeOptions());
var (x, y) = GetFocusPoint(asset);
result.FocusX = x;
result.FocusY = y;
return result;
}
private (float?, float?) GetFocusPoint(IAssetEntity asset)
{
if (!IgnoreFocus)
{
if (FocusX.HasValue && FocusY.HasValue)
{
return (FocusX.Value, FocusY.Value);
}
var focusX = asset.Metadata.GetFocusX();
var focusY = asset.Metadata.GetFocusY();
if (focusX.HasValue && focusY.HasValue)
{
return (focusX.Value, focusY.Value);
}
}
return (null, null);
}
}
}