// ==========================================================================
// AssetDto.cs
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex Group
// All rights reserved.
// ==========================================================================
using System;
using System.ComponentModel.DataAnnotations;
using NodaTime;
using Squidex.Infrastructure;
using Squidex.Infrastructure.CQRS.Commands;
using Squidex.Write.Assets.Commands;
namespace Squidex.Controllers.Api.Assets.Models
{
public sealed class AssetDto
{
///
/// The id of the asset.
///
public Guid Id { get; set; }
///
/// The file name.
///
[Required]
public string FileName { get; set; }
///
/// The mime type.
///
[Required]
public string MimeType { get; set; }
///
/// The size of the file in bytes.
///
public long FileSize { 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; }
public static AssetDto Create(CreateAsset command, EntityCreatedResult result)
{
var now = SystemClock.Instance.GetCurrentInstant();
var response = new AssetDto
{
Id = result.IdOrValue,
Version = result.Version,
Created = now,
CreatedBy = command.Actor,
LastModified = now,
LastModifiedBy = command.Actor,
FileName = command.File.FileName,
FileSize = command.File.FileSize,
MimeType = command.File.MimeType,
IsImage = command.ImageInfo != null,
PixelWidth = command.ImageInfo?.PixelWidth,
PixelHeight = command.ImageInfo?.PixelHeight
};
return response;
}
}
}