[OneBot] code refactor of ImageSegment.cs

This commit is contained in:
Linwenxuan 2023-10-18 10:31:33 +08:00
parent 63f0ffaeac
commit a9a554dbf4
1 changed files with 13 additions and 16 deletions

View File

@ -21,26 +21,23 @@ public partial class ImageSegment : ISegment
public void Build(MessageBuilder builder, ISegment segment) public void Build(MessageBuilder builder, ISegment segment)
{ {
if (segment is ImageSegment imageSegment) if (segment is ImageSegment imageSegment and not { Url: "" })
{ {
if (imageSegment is not { Url: "" }) if (imageSegment.Url.StartsWith("http"))
{ {
if (imageSegment.Url.StartsWith("http")) builder.Image(_client.GetAsync(imageSegment.Url).Result.Content.ReadAsByteArrayAsync().Result);
{ }
builder.Image(_client.GetAsync(imageSegment.Url).Result.Content.ReadAsByteArrayAsync().Result);
}
if (imageSegment.Url.StartsWith("file")) if (imageSegment.Url.StartsWith("file"))
{ {
string path = imageSegment.Url.Replace("file:///", ""); string path = imageSegment.Url.Replace("file:///", "");
builder.Image(File.ReadAllBytes(path)); builder.Image(File.ReadAllBytes(path));
} }
if (imageSegment.Url.StartsWith("base64")) if (imageSegment.Url.StartsWith("base64"))
{ {
string base64 = imageSegment.Url.Replace("base64://", ""); string base64 = imageSegment.Url.Replace("base64://", "");
builder.Image(Convert.FromBase64String(base64)); builder.Image(Convert.FromBase64String(base64));
}
} }
} }
} }