Download image

This commit is contained in:
Linwenxuan 2023-10-13 15:30:56 +08:00
parent 7311a23d64
commit fac253bbf2
1 changed files with 12 additions and 4 deletions

View File

@ -7,8 +7,10 @@ namespace Lagrange.OneBot.Core.Message.Entity;
[Serializable]
public partial class ImageSegment(string url)
{
public ImageSegment() : this("") { }
private readonly HttpClient _client = new();
public ImageSegment() : this("") => _client = new HttpClient();
[JsonPropertyName("file")] public string Url { get; set; } = url;
}
@ -17,8 +19,14 @@ public partial class ImageSegment : ISegment
{
public IMessageEntity ToEntity() => new ImageEntity(url);
public void Build(MessageBuilder builder, ISegment segment) => throw new NotImplementedException();
public void Build(MessageBuilder builder, ISegment segment)
{
if (segment is ImageSegment imageSegment)
{
if (imageSegment.Url != "") builder.Image(_client.GetAsync(imageSegment.Url).Result.Content.ReadAsByteArrayAsync().Result);
}
}
public ISegment FromEntity(IMessageEntity entity)
{
if (entity is not ImageEntity imageEntity) throw new ArgumentException("Invalid entity type.");