// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschränkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using Newtonsoft.Json; using NodaTime; using Squidex.Domain.Apps.Entities.Assets; using Squidex.Infrastructure; using Squidex.Infrastructure.Reflection; using Squidex.Shared; using Squidex.Web; namespace Squidex.Areas.Api.Controllers.Assets.Models { public sealed class AssetDto : Resource, IGenerateETag { /// /// The id of the asset. /// public Guid Id { get; set; } /// /// The file name. /// [Required] public string FileName { get; set; } /// /// The file hash. /// [Required] public string FileHash { get; set; } /// /// The slug. /// [Required] public string Slug { get; set; } /// /// The mime type. /// [Required] public string MimeType { get; set; } /// /// The file type. /// [Required] public string FileType { get; set; } /// /// The asset tags. /// public HashSet Tags { get; set; } /// /// The size of the file in bytes. /// public long FileSize { get; set; } /// /// The version of the file. /// public long FileVersion { get; set; } /// /// Determines of the created file is an image. /// public bool IsImage { get; set; } /// /// The width of the image in pixels if the asset is an image. /// public int? PixelWidth { get; set; } /// /// The height of the image in pixels if the asset is an image. /// public int? PixelHeight { get; set; } /// /// The user that has created the schema. /// [Required] public RefToken CreatedBy { get; set; } /// /// The user that has updated the asset. /// [Required] public RefToken LastModifiedBy { get; set; } /// /// The date and time when the asset has been created. /// public Instant Created { get; set; } /// /// The date and time when the asset has been modified last. /// public Instant LastModified { get; set; } /// /// The version of the asset. /// public long Version { get; set; } /// /// The metadata. /// [JsonProperty("_meta")] public AssetMetadata Metadata { get; set; } public static AssetDto FromAsset(IAssetEntity asset, ApiController controller, string app, HashSet tags = null, bool isDuplicate = false) { var response = SimpleMapper.Map(asset, new AssetDto { FileType = asset.FileName.FileType() }); if (tags != null) { response.Tags = tags; } if (isDuplicate) { response.Metadata = new AssetMetadata { IsDuplicate = "true" }; } return CreateLinks(response, controller, app); } private static AssetDto CreateLinks(AssetDto response, ApiController controller, string app) { var values = new { app, id = response.Id }; response.AddSelfLink(controller.Url(x => nameof(x.GetAsset), values)); if (controller.HasPermission(Permissions.AppAssetsUpdate)) { response.AddPutLink("update", controller.Url(x => nameof(x.PutAsset), values)); response.AddPutLink("upload", controller.Url(x => nameof(x.PutAssetContent), values)); } if (controller.HasPermission(Permissions.AppAssetsDelete)) { response.AddDeleteLink("delete", controller.Url(x => nameof(x.DeleteAsset), values)); } response.AddGetLink("content", controller.Url(x => nameof(x.GetAssetContent), new { id = response.Id, version = response.FileVersion })); if (!string.IsNullOrWhiteSpace(response.Slug)) { response.AddGetLink("content/slug", controller.Url(x => nameof(x.GetAssetContentBySlug), new { app, idOrSlug = response.Slug, version = response.Version })); } return response; } } }