Skip to content

Commit d152345

Browse files
LinwenxuanLinwenxuan
authored andcommitted
[OneBot] Support Receive for RecordSegment.cs
1 parent 8040381 commit d152345

File tree

4 files changed

+66
-21
lines changed

4 files changed

+66
-21
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace Lagrange.OneBot.Core.Message.Entity;
2+
3+
public static class CommonResolver
4+
{
5+
private static readonly HttpClient Client = new();
6+
7+
public static byte[]? Resolve(string url)
8+
{
9+
if (url.StartsWith("http"))
10+
{
11+
return Client.GetAsync(url).Result.Content.ReadAsByteArrayAsync().Result;
12+
}
13+
14+
if (url.StartsWith("file"))
15+
{
16+
string path = new Uri(url).LocalPath;
17+
return File.ReadAllBytes(path);
18+
}
19+
20+
if (url.StartsWith("base64"))
21+
{
22+
string base64 = url.Replace("base64://", "");
23+
return Convert.FromBase64String(base64);
24+
}
25+
26+
return null;
27+
}
28+
}

Lagrange.OneBot/Core/Message/Entity/ImageSegment.cs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ namespace Lagrange.OneBot.Core.Message.Entity;
77
[Serializable]
88
public partial class ImageSegment(string url)
99
{
10-
private readonly HttpClient _client = new();
11-
12-
public ImageSegment() : this("") => _client = new HttpClient();
10+
public ImageSegment() : this("") { }
1311

1412
[JsonPropertyName("file")] public string Url { get; set; } = url;
1513
}
@@ -21,24 +19,9 @@ public partial class ImageSegment : ISegment
2119

2220
public void Build(MessageBuilder builder, ISegment segment)
2321
{
24-
if (segment is ImageSegment imageSegment and not { Url: "" })
22+
if (segment is ImageSegment imageSegment and not { Url: "" } && CommonResolver.Resolve(imageSegment.Url) is { } image)
2523
{
26-
if (imageSegment.Url.StartsWith("http"))
27-
{
28-
builder.Image(_client.GetAsync(imageSegment.Url).Result.Content.ReadAsByteArrayAsync().Result);
29-
}
30-
31-
if (imageSegment.Url.StartsWith("file"))
32-
{
33-
string path = new Uri(imageSegment.Url).LocalPath;
34-
builder.Image(File.ReadAllBytes(path));
35-
}
36-
37-
if (imageSegment.Url.StartsWith("base64"))
38-
{
39-
string base64 = imageSegment.Url.Replace("base64://", "");
40-
builder.Image(Convert.FromBase64String(base64));
41-
}
24+
builder.Image(image);
4225
}
4326
}
4427

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.Text.Json.Serialization;
2+
using Lagrange.Core.Message;
3+
using Lagrange.Core.Message.Entity;
4+
5+
namespace Lagrange.OneBot.Core.Message.Entity;
6+
7+
[Serializable]
8+
public partial class RecordSegment(string url)
9+
{
10+
public RecordSegment() : this("") { }
11+
12+
[JsonPropertyName("file")] public string Url { get; set; } = url;
13+
}
14+
15+
[SegmentSubscriber(typeof(RecordEntity), "record")]
16+
public partial class RecordSegment : ISegment
17+
{
18+
public IMessageEntity ToEntity() => new ImageEntity(url);
19+
20+
public void Build(MessageBuilder builder, ISegment segment)
21+
{
22+
if (segment is RecordSegment recordSegment and not { Url: "" } && CommonResolver.Resolve(recordSegment.Url) is { } image)
23+
{
24+
builder.Record(image);
25+
}
26+
}
27+
28+
public ISegment FromEntity(IMessageEntity entity)
29+
{
30+
if (entity is not RecordEntity recordEntity) throw new ArgumentException("Invalid entity type.");
31+
32+
return new RecordSegment(recordEntity.AudioUrl);
33+
}
34+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Please use Lagrange.Core responsibly and in accordance with the law.
6363
| [Text] | 🟢 |
6464
| [Face] | 🟢 |
6565
| [Image] | 🟢 |
66-
| [Record] | 🔴 |
66+
| [Record] | 🟡 |
6767
| [Video] | 🔴 |
6868
| [At] | 🟢 |
6969
| [Rps] | 🔴 |

0 commit comments

Comments
 (0)