Skip to content
This repository was archived by the owner on Oct 12, 2025. It is now read-only.

Commit e55ed79

Browse files
authored
[Core] lazy initialization of streams (#308)
1 parent 5369aaa commit e55ed79

File tree

13 files changed

+57
-62
lines changed

13 files changed

+57
-62
lines changed

Lagrange.Core/Internal/Context/Uploader/ImageUploader.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ public async Task UploadPrivate(ContextCollection context, MessageChain chain, I
3232
};
3333
var extStream = extend.Serialize();
3434

35-
bool hwSuccess = await context.Highway.UploadSrcByStreamAsync(1003, image.ImageStream, await Common.GetTicket(context), index.Info.FileHash.UnHex(), extStream.ToArray());
35+
bool hwSuccess = await context.Highway.UploadSrcByStreamAsync(1003, image.ImageStream.Value, await Common.GetTicket(context), index.Info.FileHash.UnHex(), extStream.ToArray());
3636
if (!hwSuccess)
3737
{
38-
await image.ImageStream.DisposeAsync();
38+
await image.ImageStream.Value.DisposeAsync();
3939
throw new Exception();
4040
}
4141
}
4242

4343
image.MsgInfo = metaResult.MsgInfo; // directly constructed by Tencent's BDH Server
4444
image.CompatImage = metaResult.Compat; // for legacy QQ
45-
await image.ImageStream.DisposeAsync();
45+
await image.ImageStream.Value.DisposeAsync();
4646
}
4747
}
4848

@@ -68,17 +68,17 @@ public async Task UploadGroup(ContextCollection context, MessageChain chain, IMe
6868
};
6969
var extStream = extend.Serialize();
7070

71-
bool hwSuccess = await context.Highway.UploadSrcByStreamAsync(1004, image.ImageStream, await Common.GetTicket(context), index.Info.FileHash.UnHex(), extStream.ToArray());
71+
bool hwSuccess = await context.Highway.UploadSrcByStreamAsync(1004, image.ImageStream.Value, await Common.GetTicket(context), index.Info.FileHash.UnHex(), extStream.ToArray());
7272
if (!hwSuccess)
7373
{
74-
await image.ImageStream.DisposeAsync();
74+
await image.ImageStream.Value.DisposeAsync();
7575
throw new Exception();
7676
}
7777
}
7878

7979
image.MsgInfo = metaResult.MsgInfo; // directly constructed by Tencent's BDH Server
8080
image.CompatFace = metaResult.Compat; // for legacy QQ
81-
await image.ImageStream.DisposeAsync();
81+
await image.ImageStream.Value.DisposeAsync();
8282
}
8383
}
8484
}

Lagrange.Core/Internal/Context/Uploader/PttUploader.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ public async Task UploadPrivate(ContextCollection context, MessageChain chain, I
3333
};
3434
var extStream = extend.Serialize();
3535

36-
bool hwSuccess = await context.Highway.UploadSrcByStreamAsync(1007, record.AudioStream, await Common.GetTicket(context), index.Info.FileHash.UnHex(), extStream.ToArray());
36+
bool hwSuccess = await context.Highway.UploadSrcByStreamAsync(1007, record.AudioStream.Value, await Common.GetTicket(context), index.Info.FileHash.UnHex(), extStream.ToArray());
3737
if (!hwSuccess)
3838
{
39-
await record.AudioStream.DisposeAsync();
39+
await record.AudioStream.Value.DisposeAsync();
4040
throw new Exception();
4141
}
4242
}
4343

4444
record.MsgInfo = metaResult.MsgInfo; // directly constructed by Tencent's BDH Server
4545
record.Compat = metaResult.Compat; // for legacy QQ
46-
await record.AudioStream.DisposeAsync();
46+
await record.AudioStream.Value.DisposeAsync();
4747
}
4848
}
4949

@@ -69,17 +69,17 @@ public async Task UploadGroup(ContextCollection context, MessageChain chain, IMe
6969
};
7070
var extStream = extend.Serialize();
7171

72-
bool hwSuccess = await context.Highway.UploadSrcByStreamAsync(1008, record.AudioStream, await Common.GetTicket(context), index.Info.FileHash.UnHex(), extStream.ToArray());
72+
bool hwSuccess = await context.Highway.UploadSrcByStreamAsync(1008, record.AudioStream.Value, await Common.GetTicket(context), index.Info.FileHash.UnHex(), extStream.ToArray());
7373
if (!hwSuccess)
7474
{
75-
await record.AudioStream.DisposeAsync();
75+
await record.AudioStream.Value.DisposeAsync();
7676
throw new Exception();
7777
}
7878
}
7979

8080
record.MsgInfo = metaResult.MsgInfo; // directly constructed by Tencent's BDH Server
8181
record.Compat = metaResult.Compat; // for legacy QQ
82-
await record.AudioStream.DisposeAsync();
82+
await record.AudioStream.Value.DisposeAsync();
8383
}
8484
}
8585
}

Lagrange.Core/Internal/Context/Uploader/VideoUploader.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ public async Task UploadPrivate(ContextCollection context, MessageChain chain, I
3131
};
3232
var extStream = extend.Serialize();
3333

34-
bool hwSuccess = await context.Highway.UploadSrcByStreamAsync(1001, stream, await Common.GetTicket(context), index.Info.FileHash.UnHex(), extStream.ToArray());
34+
bool hwSuccess = await context.Highway.UploadSrcByStreamAsync(1001, stream.Value, await Common.GetTicket(context), index.Info.FileHash.UnHex(), extStream.ToArray());
3535
if (!hwSuccess)
3636
{
37-
await stream.DisposeAsync();
37+
await stream.Value.DisposeAsync();
3838
throw new Exception();
3939
}
4040
}
4141

4242
video.MsgInfo = metaResult.MsgInfo; // directly constructed by Tencent's BDH Server
4343
video.Compat = metaResult.Compat; // for legacy QQ
44-
await stream.DisposeAsync();
44+
await stream.Value.DisposeAsync();
4545
}
4646
}
4747

@@ -63,21 +63,21 @@ public async Task UploadGroup(ContextCollection context, MessageChain chain, IMe
6363
Network = Common.Convert(metaResult.Network),
6464
MsgInfoBody = metaResult.MsgInfo.MsgInfoBody,
6565
BlockSize = 1024 * 1024,
66-
Hash = new NTHighwayHash { FileSha1 = Common.CalculateStreamBytes(stream) }
66+
Hash = new NTHighwayHash { FileSha1 = Common.CalculateStreamBytes(stream.Value) }
6767
};
6868
var extStream = extend.Serialize();
6969

70-
bool hwSuccess = await context.Highway.UploadSrcByStreamAsync(1005, stream, await Common.GetTicket(context), index.Info.FileHash.UnHex(), extStream.ToArray());
70+
bool hwSuccess = await context.Highway.UploadSrcByStreamAsync(1005, stream.Value, await Common.GetTicket(context), index.Info.FileHash.UnHex(), extStream.ToArray());
7171
if (!hwSuccess)
7272
{
73-
await stream.DisposeAsync();
73+
await stream.Value.DisposeAsync();
7474
throw new Exception();
7575
}
7676
}
7777

7878
video.MsgInfo = metaResult.MsgInfo; // directly constructed by Tencent's BDH Server
7979
video.Compat = metaResult.Compat; // for legacy QQ
80-
await stream.DisposeAsync();
80+
await stream.Value.DisposeAsync();
8181
}
8282
}
8383
}

Lagrange.Core/Internal/Service/Message/ImageGroupUploadService.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ protected override bool Build(ImageGroupUploadEvent input, BotKeystore keystore,
2222
{
2323
if (input.Entity.ImageStream is null) throw new Exception();
2424

25-
string md5 = input.Entity.ImageStream.Md5(true);
26-
string sha1 = input.Entity.ImageStream.Sha1(true);
25+
string md5 = input.Entity.ImageStream.Value.Md5(true);
26+
string sha1 = input.Entity.ImageStream.Value.Sha1(true);
2727

2828
var buffer = new byte[1024]; // parse image header
29-
int _ = input.Entity.ImageStream.Read(buffer.AsSpan());
29+
int _ = input.Entity.ImageStream.Value.Read(buffer.AsSpan());
3030
var type = ImageResolver.Resolve(buffer, out var size);
3131
string imageExt = type switch
3232
{
@@ -38,7 +38,7 @@ protected override bool Build(ImageGroupUploadEvent input, BotKeystore keystore,
3838
ImageFormat.Tiff => ".tiff",
3939
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
4040
};
41-
input.Entity.ImageStream.Position = 0;
41+
input.Entity.ImageStream.Value.Position = 0;
4242

4343
var packet = new OidbSvcTrpcTcpBase<NTV2RichMediaReq>(new NTV2RichMediaReq
4444
{
@@ -66,7 +66,7 @@ protected override bool Build(ImageGroupUploadEvent input, BotKeystore keystore,
6666
{
6767
FileInfo = new FileInfo
6868
{
69-
FileSize = (uint)input.Entity.ImageStream.Length,
69+
FileSize = (uint)input.Entity.ImageStream.Value.Length,
7070
FileHash = md5,
7171
FileSha1 = sha1,
7272
FileName = md5 + imageExt,

Lagrange.Core/Internal/Service/Message/ImageUploadService.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ protected override bool Build(ImageUploadEvent input, BotKeystore keystore, BotA
2222
{
2323
if (input.Entity.ImageStream is null) throw new Exception();
2424

25-
string md5 = input.Entity.ImageStream.Md5(true);
26-
string sha1 = input.Entity.ImageStream.Sha1(true);
25+
string md5 = input.Entity.ImageStream.Value.Md5(true);
26+
string sha1 = input.Entity.ImageStream.Value.Sha1(true);
2727

2828
var buffer = new byte[1024]; // parse image header
29-
int _ = input.Entity.ImageStream.Read(buffer.AsSpan());
29+
int _ = input.Entity.ImageStream.Value.Read(buffer.AsSpan());
3030
var type = ImageResolver.Resolve(buffer, out var size);
3131
string imageExt = type switch
3232
{
@@ -38,7 +38,7 @@ protected override bool Build(ImageUploadEvent input, BotKeystore keystore, BotA
3838
ImageFormat.Tiff => ".tiff",
3939
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
4040
};
41-
input.Entity.ImageStream.Position = 0;
41+
input.Entity.ImageStream.Value.Position = 0;
4242

4343
string uid = (string.IsNullOrEmpty(input.TargetUid) ? keystore.Uid : input.TargetUid) ?? "";
4444

@@ -72,7 +72,7 @@ protected override bool Build(ImageUploadEvent input, BotKeystore keystore, BotA
7272
{
7373
FileInfo = new FileInfo
7474
{
75-
FileSize = (uint)input.Entity.ImageStream.Length,
75+
FileSize = (uint)input.Entity.ImageStream.Value.Length,
7676
FileHash = md5,
7777
FileSha1 = sha1,
7878
FileName = md5 + imageExt,

Lagrange.Core/Internal/Service/Message/RecordGroupUploadService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ protected override bool Build(RecordGroupUploadEvent input, BotKeystore keystore
2121
{
2222
if (input.Entity.AudioStream is null) throw new Exception();
2323

24-
string md5 = input.Entity.AudioStream.Md5(true);
25-
string sha1 = input.Entity.AudioStream.Sha1(true);
24+
string md5 = input.Entity.AudioStream.Value.Md5(true);
25+
string sha1 = input.Entity.AudioStream.Value.Sha1(true);
2626

2727
var packet = new OidbSvcTrpcTcpBase<NTV2RichMediaReq>(new NTV2RichMediaReq
2828
{
@@ -50,7 +50,7 @@ protected override bool Build(RecordGroupUploadEvent input, BotKeystore keystore
5050
{
5151
FileInfo = new FileInfo
5252
{
53-
FileSize = (uint)input.Entity.AudioStream.Length,
53+
FileSize = (uint)input.Entity.AudioStream.Value.Length,
5454
FileHash = md5,
5555
FileSha1 = sha1,
5656
FileName = md5 + ".amr",

Lagrange.Core/Internal/Service/Message/RecordUploadService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ protected override bool Build(RecordUploadEvent input, BotKeystore keystore, Bot
2020
{
2121
if (input.Entity.AudioStream is null) throw new Exception();
2222

23-
string md5 = input.Entity.AudioStream.Md5(true);
24-
string sha1 = input.Entity.AudioStream.Sha1(true);
23+
string md5 = input.Entity.AudioStream.Value.Md5(true);
24+
string sha1 = input.Entity.AudioStream.Value.Sha1(true);
2525

2626
var packet = new OidbSvcTrpcTcpBase<NTV2RichMediaReq>(new NTV2RichMediaReq
2727
{
@@ -53,7 +53,7 @@ protected override bool Build(RecordUploadEvent input, BotKeystore keystore, Bot
5353
{
5454
FileInfo = new FileInfo
5555
{
56-
FileSize = (uint)input.Entity.AudioStream.Length,
56+
FileSize = (uint)input.Entity.AudioStream.Value.Length,
5757
FileHash = md5,
5858
FileSha1 = sha1,
5959
FileName = md5 + ".amr",

Lagrange.Core/Internal/Service/Message/VideoGroupUploadService.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ protected override bool Build(VideoGroupUploadEvent input, BotKeystore keystore,
2121
{
2222
if (input.Entity.VideoStream is null) throw new Exception();
2323

24-
string md5 = input.Entity.VideoStream.Md5(true);
25-
string sha1 = input.Entity.VideoStream.Sha1(true);
24+
string md5 = input.Entity.VideoStream.Value.Md5(true);
25+
string sha1 = input.Entity.VideoStream.Value.Sha1(true);
2626

2727
var packet = new OidbSvcTrpcTcpBase<NTV2RichMediaReq>(new NTV2RichMediaReq
2828
{
@@ -50,7 +50,7 @@ protected override bool Build(VideoGroupUploadEvent input, BotKeystore keystore,
5050
{
5151
FileInfo = new FileInfo
5252
{
53-
FileSize = (uint)input.Entity.VideoStream.Length,
53+
FileSize = (uint)input.Entity.VideoStream.Value.Length,
5454
FileHash = md5,
5555
FileSha1 = sha1,
5656
FileName = "video.mp4",
@@ -72,7 +72,7 @@ protected override bool Build(VideoGroupUploadEvent input, BotKeystore keystore,
7272
{
7373
FileInfo = new FileInfo // dummy images
7474
{
75-
FileSize = (uint)input.Entity.VideoStream.Length - 1200,
75+
FileSize = (uint)input.Entity.VideoStream.Value.Length - 1200,
7676
FileHash = md5,
7777
FileSha1 = sha1,
7878
FileName = "video.jpg",

Lagrange.Core/Internal/Service/Message/VideoUploadService.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ protected override bool Build(VideoUploadEvent input, BotKeystore keystore, BotA
2121
{
2222
if (input.Entity.VideoStream is null) throw new Exception();
2323

24-
string md5 = input.Entity.VideoStream.Md5(true);
25-
string sha1 = input.Entity.VideoStream.Sha1(true);
24+
string md5 = input.Entity.VideoStream.Value.Md5(true);
25+
string sha1 = input.Entity.VideoStream.Value.Sha1(true);
2626

2727
var packet = new OidbSvcTrpcTcpBase<NTV2RichMediaReq>(new NTV2RichMediaReq
2828
{
@@ -54,7 +54,7 @@ protected override bool Build(VideoUploadEvent input, BotKeystore keystore, BotA
5454
{
5555
FileInfo = new FileInfo
5656
{
57-
FileSize = (uint)input.Entity.VideoStream.Length,
57+
FileSize = (uint)input.Entity.VideoStream.Value.Length,
5858
FileHash = md5,
5959
FileSha1 = sha1,
6060
FileName = "video.mp4",
@@ -76,7 +76,7 @@ protected override bool Build(VideoUploadEvent input, BotKeystore keystore, BotA
7676
{
7777
FileInfo = new FileInfo // dummy images
7878
{
79-
FileSize = (uint)input.Entity.VideoStream.Length - 1200,
79+
FileSize = (uint)input.Entity.VideoStream.Value.Length - 1200,
8080
FileHash = md5,
8181
FileSha1 = sha1,
8282
FileName = "video.jpg",

Lagrange.Core/Message/Entity/ImageEntity.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class ImageEntity : IMessageEntity
2525

2626
public string ImageUrl { get; set; } = string.Empty;
2727

28-
internal Stream? ImageStream { get; set; }
28+
internal Lazy<Stream>? ImageStream { get; set; }
2929

3030
internal string? Path { get; set; }
3131

@@ -44,19 +44,19 @@ public ImageEntity() { }
4444
public ImageEntity(string filePath)
4545
{
4646
FilePath = filePath;
47-
ImageStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
47+
ImageStream = new Lazy<Stream>(() => new FileStream(filePath, FileMode.Open, FileAccess.Read));
4848
}
4949

5050
public ImageEntity(byte[] file)
5151
{
5252
FilePath = "";
53-
ImageStream = new MemoryStream(file);
53+
ImageStream = new Lazy<Stream>(() => new MemoryStream(file));
5454
}
5555

5656
public ImageEntity(Stream stream)
5757
{
5858
FilePath = "";
59-
ImageStream = stream;
59+
ImageStream = new Lazy<Stream>(stream);
6060
}
6161

6262
IEnumerable<Elem> IMessageEntity.PackElement()

0 commit comments

Comments
 (0)