Skip to content

Commit

Permalink
Download image
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan authored and Linwenxuan committed Oct 13, 2023
1 parent 7311a23 commit fac253b
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions Lagrange.OneBot/Core/Message/Entity/ImageSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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.");
Expand Down

0 comments on commit fac253b

Please sign in to comment.