Skip to content

Commit

Permalink
[OneBot] Support Receive for RecordSegment.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan authored and Linwenxuan committed Feb 2, 2024
1 parent 8040381 commit d152345
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 21 deletions.
28 changes: 28 additions & 0 deletions Lagrange.OneBot/Core/Message/Entity/CommonResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace Lagrange.OneBot.Core.Message.Entity;

public static class CommonResolver
{
private static readonly HttpClient Client = new();

public static byte[]? Resolve(string url)
{
if (url.StartsWith("http"))
{
return Client.GetAsync(url).Result.Content.ReadAsByteArrayAsync().Result;
}

if (url.StartsWith("file"))
{
string path = new Uri(url).LocalPath;
return File.ReadAllBytes(path);
}

if (url.StartsWith("base64"))
{
string base64 = url.Replace("base64://", "");
return Convert.FromBase64String(base64);
}

return null;
}
}
23 changes: 3 additions & 20 deletions Lagrange.OneBot/Core/Message/Entity/ImageSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ namespace Lagrange.OneBot.Core.Message.Entity;
[Serializable]
public partial class ImageSegment(string url)
{
private readonly HttpClient _client = new();

public ImageSegment() : this("") => _client = new HttpClient();
public ImageSegment() : this("") { }

[JsonPropertyName("file")] public string Url { get; set; } = url;

Check warning on line 12 in Lagrange.OneBot/Core/Message/Entity/ImageSegment.cs

View workflow job for this annotation

GitHub Actions / Build (linux-arm64)

Parameter 'string url' is captured into the state of the enclosing type and its value is also used to initialize a field, property, or event.
}
Expand All @@ -21,24 +19,9 @@ public partial class ImageSegment : ISegment

public void Build(MessageBuilder builder, ISegment segment)
{
if (segment is ImageSegment imageSegment and not { Url: "" })
if (segment is ImageSegment imageSegment and not { Url: "" } && CommonResolver.Resolve(imageSegment.Url) is { } image)
{
if (imageSegment.Url.StartsWith("http"))
{
builder.Image(_client.GetAsync(imageSegment.Url).Result.Content.ReadAsByteArrayAsync().Result);
}

if (imageSegment.Url.StartsWith("file"))
{
string path = new Uri(imageSegment.Url).LocalPath;
builder.Image(File.ReadAllBytes(path));
}

if (imageSegment.Url.StartsWith("base64"))
{
string base64 = imageSegment.Url.Replace("base64://", "");
builder.Image(Convert.FromBase64String(base64));
}
builder.Image(image);
}
}

Expand Down
34 changes: 34 additions & 0 deletions Lagrange.OneBot/Core/Message/Entity/RecordSegment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Text.Json.Serialization;
using Lagrange.Core.Message;
using Lagrange.Core.Message.Entity;

namespace Lagrange.OneBot.Core.Message.Entity;

[Serializable]
public partial class RecordSegment(string url)
{
public RecordSegment() : this("") { }

[JsonPropertyName("file")] public string Url { get; set; } = url;

Check warning on line 12 in Lagrange.OneBot/Core/Message/Entity/RecordSegment.cs

View workflow job for this annotation

GitHub Actions / Build (linux-arm64)

Parameter 'string url' is captured into the state of the enclosing type and its value is also used to initialize a field, property, or event.
}

[SegmentSubscriber(typeof(RecordEntity), "record")]
public partial class RecordSegment : ISegment
{
public IMessageEntity ToEntity() => new ImageEntity(url);

public void Build(MessageBuilder builder, ISegment segment)
{
if (segment is RecordSegment recordSegment and not { Url: "" } && CommonResolver.Resolve(recordSegment.Url) is { } image)
{
builder.Record(image);

Check failure on line 24 in Lagrange.OneBot/Core/Message/Entity/RecordSegment.cs

View workflow job for this annotation

GitHub Actions / Build (linux-arm64)

'MessageBuilder' does not contain a definition for 'Record' and no accessible extension method 'Record' accepting a first argument of type 'MessageBuilder' could be found (are you missing a using directive or an assembly reference?)
}
}

public ISegment FromEntity(IMessageEntity entity)
{
if (entity is not RecordEntity recordEntity) throw new ArgumentException("Invalid entity type.");

return new RecordSegment(recordEntity.AudioUrl);
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Please use Lagrange.Core responsibly and in accordance with the law.
| [Text] | 🟢 |
| [Face] | 🟢 |
| [Image] | 🟢 |
| [Record] | 🔴 |
| [Record] | 🟡 |
| [Video] | 🔴 |
| [At] | 🟢 |
| [Rps] | 🔴 |
Expand Down

0 comments on commit d152345

Please sign in to comment.