Iceshrimp.NET/Iceshrimp.Backend/Controllers/Mastodon/Schemas/Entities/AttachmentEntity.cs

61 lines
2.1 KiB
C#

using J = System.Text.Json.Serialization.JsonPropertyNameAttribute;
using JI = System.Text.Json.Serialization.JsonIgnoreAttribute;
namespace Iceshrimp.Backend.Controllers.Mastodon.Schemas.Entities;
public class AttachmentEntity
{
[JI] public required bool Sensitive;
[JI] public required AttachmentType Type;
[J("id")] public required string Id { get; set; }
[J("url")] public required string Url { get; set; }
[J("remote_url")] public string? RemoteUrl { get; set; }
[J("preview_url")] public string? PreviewUrl { get; set; }
[J("text_url")] public string? TextUrl { get; set; }
[J("meta")] public AttachmentMetadata? Metadata { get; set; }
[J("description")] public string? Description { get; set; }
[J("blurhash")] public string? Blurhash { get; set; }
[J("type")]
public string TypeString => Type switch
{
AttachmentType.Unknown => "unknown",
AttachmentType.Image => "image",
AttachmentType.Gif => "image",
AttachmentType.Video => "video",
AttachmentType.Audio => "audio",
_ => throw new ArgumentOutOfRangeException()
};
public static AttachmentType GetType(string mime)
{
if (mime == "image/gif") return AttachmentType.Gif;
if (mime.StartsWith("image/")) return AttachmentType.Image;
if (mime.StartsWith("video/")) return AttachmentType.Video;
if (mime.StartsWith("audio/")) return AttachmentType.Audio;
return AttachmentType.Unknown;
}
}
public enum AttachmentType
{
Unknown,
Image,
Gif,
Video,
Audio
}
public class AttachmentMetadata(int width, int height)
{
[J("original")] public OriginalAttachmentMetadata Original => new(width, height);
}
public class OriginalAttachmentMetadata(int width, int height)
{
[J("width")] public int Width => width;
[J("height")] public int Height => height;
[J("size")] public string Size => $"{width}x{height}";
[J("aspect")] public float Aspect => (float)width / height;
}