diff --git a/Lagrange.Core/Message/Entity/FileEntity.cs b/Lagrange.Core/Message/Entity/FileEntity.cs index acad46b2c..c7b8b662c 100644 --- a/Lagrange.Core/Message/Entity/FileEntity.cs +++ b/Lagrange.Core/Message/Entity/FileEntity.cs @@ -24,9 +24,9 @@ public class FileEntity : IMessageEntity /// public string? FileId { get; set; } - internal string? FileUuid { get; set; } + public string? FileUuid { get; set; } - internal string? FileHash { get; set; } + public string? FileHash { get; set; } internal Stream? FileStream { get; set; } diff --git a/Lagrange.OneBot/Message/Entity/FileSegment.cs b/Lagrange.OneBot/Message/Entity/FileSegment.cs new file mode 100644 index 000000000..b56ad87a1 --- /dev/null +++ b/Lagrange.OneBot/Message/Entity/FileSegment.cs @@ -0,0 +1,45 @@ +using System.Text.Json.Serialization; +using Lagrange.Core.Message; +using Lagrange.Core.Message.Entity; + +namespace Lagrange.OneBot.Message.Entity; + +[Serializable] +public partial class FileSegment(string fileName, string fileHash, string fileId, string url) +{ + public FileSegment() : this("", "", "", "") { } + + [JsonPropertyName("filename")] public string Filename { get; set; } = fileName; + + [JsonPropertyName("filehash")] public string Filehash { get; set; } = fileHash; + + [JsonPropertyName("id")] public string Fileid { get; set; } = fileId; + + [JsonPropertyName("url")] public string Url { get; set; } = url; + +} + +[SegmentSubscriber(typeof(FileEntity), "file")] +public partial class FileSegment : SegmentBase +{ + public override void Build(MessageBuilder builder, SegmentBase segment) + { + if (segment is FileSegment fileSegment and not { Fileid: "" }) + { + builder.Add(new FileEntity + { + FileName = fileSegment.Filename, + FileUrl = fileSegment.Url, + FileId = fileSegment.Fileid, + FileHash = fileSegment.Filehash + }); + } + } + + public override SegmentBase FromEntity(MessageChain chain, IMessageEntity entity) + { + if (entity is not FileEntity fileEntity) throw new ArgumentException("Invalid entity type."); + + return new FileSegment(fileEntity.FileName, fileEntity.FileHash ?? "", fileEntity.FileId ?? fileEntity.FileUuid ?? "", fileEntity.FileUrl ?? ""); + } +} \ No newline at end of file