namespace Uwaa.Pleroma; public class Attachment { /// /// The ID of the attachment in the database. /// [JsonPropertyName("id")] public string ID { get; set; } = null!; /// /// Alternate text that describes what is in the media attachment, to be used for the visually impaired or when media attachments do not load /// [JsonPropertyName("description")] public string? Description { get; set; } /// /// Additional pleroma-specific data. /// [JsonPropertyName("pleroma")] public PleromaAttachmentData Pleroma { get; set; } = null!; /// /// The location of a scaled-down preview of the attachment /// [JsonPropertyName("preview_url")] public string PreviewURL { get; set; } = null!; /// /// The location of the full-size original attachment on the remote website. String (URL), or null if the attachment is local /// [JsonPropertyName("remote_url")] public string? RemoteURL { get; set; } /// /// A shorter URL for the attachment /// [JsonPropertyName("text_url")] public string? TextURL { get; set; } /// /// The type of the attachment /// [JsonPropertyName("type")] public AttachmentType Type { get; set; } /// /// The location of the original full-size attachment /// [JsonPropertyName("url")] public string URL { get; set; } = null!; /// /// Downloads the attachment. /// public async Task Download() { using HttpClient client = new HttpClient(); HttpResponseMessage res = await client.GetAsync(URL); return await res.Content.ReadAsByteArrayAsync(); } } [JsonConverter(typeof(EnumLowerCaseConverter))] public enum AttachmentType { Unknown, Image, Video, Audio, } /// /// Additional pleroma-specific data for an . /// public class PleromaAttachmentData { /// /// MIME type of the attachment /// [JsonPropertyName("mime_type")] public string MIMEType { get; set; } = null!; /// /// Name of the attachment, typically the filename /// [JsonPropertyName("name")] public string Name { get; set; } = null!; } [JsonConverter(typeof(MediaIDConverter))] public readonly struct MediaID(string id) { public static implicit operator MediaID(string id) => new MediaID(id); public static implicit operator MediaID(Attachment attachment) => new MediaID(attachment.ID); public readonly string ID = id; public override string ToString() => ID; } class MediaIDConverter : JsonConverter { public override bool CanConvert(Type type) => type.IsAssignableTo(typeof(MediaID)); public override MediaID Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { return new MediaID(reader.GetString() ?? throw new NullReferenceException("Expected a string, got null")); } public override void Write(Utf8JsonWriter writer, MediaID value, JsonSerializerOptions options) { writer.WriteStringValue(value.ID); } }