-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathImageEntity.cs
More file actions
132 lines (114 loc) · 4.58 KB
/
ImageEntity.cs
File metadata and controls
132 lines (114 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Lagrange.Core.Internal.Events.Message;
using Lagrange.Core.Internal.Packets.Message;
using Lagrange.Core.Internal.Packets.Service;
using Lagrange.Core.Utility;
using Lagrange.Core.Utility.Extension;
namespace Lagrange.Core.Message.Entities;
public class ImageEntity : RichMediaEntityBase
{
internal override Lazy<Stream>? Stream { get; }
public Vector2 ImageSize { get; set; }
public int SubType { get; init; }
public string Summary { get; init; } = "[图片]";
public ImageEntity() { }
public ImageEntity(Stream stream, string? summary = "[图片]", int subType = 0, bool disposeOnCompletion = false)
{
Stream = new Lazy<Stream>(() => stream);
Summary = summary ?? "[图片]";
SubType = subType;
DisposeOnCompletion = disposeOnCompletion;
}
public override async Task Preprocess(BotContext context, BotMessage message)
{
ArgumentNullException.ThrowIfNull(Stream);
try
{
IsGroup = message.IsGroup();
NTV2RichMediaUploadEventResp result = IsGroup
? await context.EventContext.SendEvent<ImageGroupUploadEventResp>(new ImageGroupUploadEventReq(message, this))
: await context.EventContext.SendEvent<ImageUploadEventResp>(new ImageUploadEventReq(message, this));
_compat = result.Compat;
MsgInfo = result.Info;
if (result.Ext != null)
{
// Aot 和 MacOS 下使用 FlashTransfer 上传
if (RuntimeFeature.IsDynamicCodeCompiled && !RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
await context.HighwayContext.UploadFile(Stream.Value, message.IsGroup() ? 1004 : 1003, ProtoHelper.Serialize(result.Ext));
}
else
{
await context.FlashTransferContext.UploadFile(result.Ext.UKey, Stream.Value);
}
}
}
finally
{
if (DisposeOnCompletion) await Stream.Value.DisposeAsync();
}
}
public override async Task Postprocess(BotContext context, BotMessage message)
{
NTV2RichMediaDownloadEventResp result = message.IsGroup()
? await context.EventContext.SendEvent<ImageGroupDownloadEventResp>(new ImageGroupDownloadEventReq(message, this))
: await context.EventContext.SendEvent<ImageDownloadEventResp>(new ImageDownloadEventReq(message, this));
FileUrl = result.Url;
}
public override string ToPreviewString() => Summary;
internal override Elem[] Build()
{
if (_compat != null)
{
var compatElem = IsGroup
? new Elem { CustomFace = ProtoHelper.Deserialize<CustomFace>(_compat) }
: new Elem { NotOnlineImage = ProtoHelper.Deserialize<NotOnlineImage>(_compat) };
return
[
compatElem,
new Elem()
{
CommonElem = new CommonElem
{
ServiceType = 48,
PbElem = ProtoHelper.Serialize(MsgInfo ?? throw new ArgumentNullException(nameof(MsgInfo))),
BusinessType = IsGroup ? 20u : 10u,
}
}
];
}
else
{
return
[
new Elem()
{
CommonElem = new CommonElem
{
ServiceType = 48,
PbElem = ProtoHelper.Serialize(MsgInfo ?? throw new ArgumentNullException(nameof(MsgInfo))),
BusinessType = IsGroup ? 20u : 10u,
}
}
];
}
}
internal override IMessageEntity? Parse(List<Elem> elements, Elem target)
{
if (target.CommonElem is { BusinessType: 10 or 20 } commonElem)
{
var msgInfo = ProtoHelper.Deserialize<MsgInfo>(commonElem.PbElem.Span);
var info = msgInfo.MsgInfoBody[0].Index.Info;
return new ImageEntity
{
MsgInfo = msgInfo,
ImageSize = new Vector2(info.Width, info.Height),
SubType = (int)msgInfo.ExtBizInfo.Pic.BizType,
Summary = string.IsNullOrEmpty(msgInfo.ExtBizInfo.Pic.TextSummary) ? "[图片]" : msgInfo.ExtBizInfo.Pic.TextSummary,
};
}
return null;
}
}