File tree Expand file tree Collapse file tree 4 files changed +66
-21
lines changed
Lagrange.OneBot/Core/Message/Entity Expand file tree Collapse file tree 4 files changed +66
-21
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -7,9 +7,7 @@ namespace Lagrange.OneBot.Core.Message.Entity;
7
7
[ Serializable ]
8
8
public partial class ImageSegment ( string url )
9
9
{
10
- private readonly HttpClient _client = new ( ) ;
11
-
12
- public ImageSegment ( ) : this ( "" ) => _client = new HttpClient ( ) ;
10
+ public ImageSegment ( ) : this ( "" ) { }
13
11
14
12
[ JsonPropertyName ( "file" ) ] public string Url { get ; set ; } = url ;
15
13
}
@@ -21,24 +19,9 @@ public partial class ImageSegment : ISegment
21
19
22
20
public void Build ( MessageBuilder builder , ISegment segment )
23
21
{
24
- if ( segment is ImageSegment imageSegment and not { Url : "" } )
22
+ if ( segment is ImageSegment imageSegment and not { Url : "" } && CommonResolver . Resolve ( imageSegment . Url ) is { } image )
25
23
{
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 ) ;
42
25
}
43
26
}
44
27
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -63,7 +63,7 @@ Please use Lagrange.Core responsibly and in accordance with the law.
63
63
| [ Text] | 🟢 |
64
64
| [ Face] | 🟢 |
65
65
| [ Image] | 🟢 |
66
- | [ Record] | 🔴 |
66
+ | [ Record] | 🟡 |
67
67
| [ Video] | 🔴 |
68
68
| [ At] | 🟢 |
69
69
| [ Rps] | 🔴 |
You can’t perform that action at this time.
0 commit comments