Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Lagrange.Core/BotContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ internal BotContext(BotConfig config, BotKeystore keystore, BotAppInfo appInfo)
SocketContext = new SocketContext(this);
EventContext = new EventContext(this);
HighwayContext = new HighwayContext(this);
FlashTransferContext = new FlashTransferContext(this);
}

public BotConfig Config { get; }
Expand All @@ -41,6 +42,7 @@ internal BotContext(BotConfig config, BotKeystore keystore, BotAppInfo appInfo)
internal EventContext EventContext { get; }

internal HighwayContext HighwayContext { get; }
internal FlashTransferContext FlashTransferContext { get; }

#region Shortcut Methods

Expand Down
127 changes: 127 additions & 0 deletions Lagrange.Core/Internal/Context/FlashTransferContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
using System.Runtime.Intrinsics.Arm;
using System.Security.Cryptography;
using Lagrange.Core.Internal.Packets.Service;
using Lagrange.Core.Utility;
using Lagrange.Core.Utility.Cryptography;

namespace Lagrange.Core.Internal.Context;

public class FlashTransferContext
{
private const string Tag = nameof(FlashTransferContext);
private readonly BotContext _botContext;
private readonly HttpClient _client;
private readonly string? _url;
private const uint ChunkSize = 1024 * 1024;

internal FlashTransferContext(BotContext botContext)
{
_botContext = botContext;
_client = new HttpClient();
_client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip");
_url = "https://multimedia.qfile.qq.com/sliceupload";
}

public async Task<bool> UploadFile(string uKey, Stream bodyStream)
{
byte[] body;
if (bodyStream is MemoryStream ms)
{
body = ms.ToArray();
}
else
{
bodyStream.Position = 0;
var buffer = new byte[bodyStream.Length];
await bodyStream.ReadExactlyAsync(buffer, 0, buffer.Length);
body = buffer;
}

return await UploadFile(uKey, body);
}

public async Task<bool> UploadFile(string uKey, byte[] body)
{
var chunkSha1S = new FlashTransferSha1StateV { State = [] };
var chunkBuffers = new List<byte[]>();
var chunkCount = (uint)((body.Length + ChunkSize - 1) / ChunkSize);

using var accStream = new MemoryStream();
for (uint i = 0; i < chunkCount; i++)
{
var chunkBuffer = body.AsSpan((int)(i * ChunkSize), (int)Math.Min(ChunkSize, body.Length - i * ChunkSize))
.ToArray();
chunkBuffers.Add(chunkBuffer);

if (i != chunkCount - 1)
{
accStream.Write(chunkBuffer, 0, chunkBuffer.Length);
var accBytes = accStream.ToArray();
var sha1Stream = new Sha1Stream();
var digest = new byte[20];
sha1Stream.Update(accBytes);
sha1Stream.Hash(digest, false);
chunkSha1S.State.Add(digest);
}
else
{
chunkSha1S.State.Add(SHA1.HashData(body));
}
}

// cnm闹禅tx为什么不能并发,回答我
Copy link

Copilot AI Sep 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment contains inappropriate language and should be replaced with a professional technical comment explaining why sequential processing is required instead of concurrent uploads.

Suggested change
// cnm闹禅tx为什么不能并发,回答我
// Sequential processing is required because the server expects chunks to be uploaded in order. Concurrent uploads may cause data integrity issues or protocol violations.

Copilot uses AI. Check for mistakes.
foreach (var chunkBuffer in chunkBuffers)
{
var success = await UploadChunk(uKey, (uint)(chunkBuffers.IndexOf(chunkBuffer) * ChunkSize), chunkSha1S, chunkBuffer);
if (!success)
{
return false;
}
}

return true;
}

private async Task<bool> UploadChunk(string uKey, uint start, FlashTransferSha1StateV chunkSha1S, byte[] body)
{
var req = new FlashTransferUploadReq
{
FieId1 = 0,
AppId = 1407,
FileId3 = 2,
Body = new FlashTransferUploadBody
{
FieId1 = [],
UKey = uKey,
Start = start,
End = (uint)(start + body.Length - 1),
Sha1 = SHA1.HashData(body),
Sha1StateV = chunkSha1S,
Body = body
}
};
var payload = ProtoHelper.Serialize(req).ToArray();
var request = new HttpRequestMessage(HttpMethod.Post, _url)
{
Headers =
{
{ "Accept", "*/*" },
{ "Expect", "100-continue" },
{ "Connection", "Keep-Alive" }
},
Content = new ByteArrayContent(payload)
};
var response = await _client.SendAsync(request);
var responseBytes = await response.Content.ReadAsByteArrayAsync();
var resp = ProtoHelper.Deserialize<FlashTransferUploadResp>(responseBytes);

if (resp.Status != "success")
{
_botContext.LogError(Tag,
$"FlashTransfer Upload chunk {start} failed: {resp.Status}");
return false;
}

return true;
}
}
31 changes: 31 additions & 0 deletions Lagrange.Core/Internal/Packets/Service/FlashTransferUploadReq.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Lagrange.Proto;

#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑添加 'required' 修饰符或声明为可以为 null。
namespace Lagrange.Core.Internal.Packets.Service;

[ProtoPackable]
internal partial class FlashTransferUploadReq
{
[ProtoMember(1)] public uint FieId1 { get; set; } // 0
[ProtoMember(2)] public uint AppId { get; set; } // 1407
[ProtoMember(3)] public uint FileId3 { get; set; } // 0
[ProtoMember(107)] public FlashTransferUploadBody Body { get; set; }
}

[ProtoPackable]
internal partial class FlashTransferUploadBody
{
[ProtoMember(1)] public byte[] FieId1 { get; set; } // Empty
[ProtoMember(2)] public string UKey { get; set; }
[ProtoMember(3)] public uint Start { get; set; } // Start
[ProtoMember(4)] public uint End { get; set; } // Start + Size - 1
[ProtoMember(5)] public byte[] Sha1 { get; set; }
[ProtoMember(6)] public FlashTransferSha1StateV Sha1StateV { get; set; } // 累加Sha1 5寄存器和, 最后一个是完整文件的Sha1
[ProtoMember(7)] public byte[] Body { get; set; }
}

[ProtoPackable]
internal partial class FlashTransferSha1StateV
{
[ProtoMember(1)] public List<byte[]> State { get; set; }
}
10 changes: 10 additions & 0 deletions Lagrange.Core/Internal/Packets/Service/FlashTransferUploadResp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Lagrange.Proto;
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑添加 'required' 修饰符或声明为可以为 null。

namespace Lagrange.Core.Internal.Packets.Service;

[ProtoPackable]
internal partial class FlashTransferUploadResp
{
[ProtoMember(5)] public string Status { get; set; }
}
14 changes: 12 additions & 2 deletions Lagrange.Core/Message/Entities/ImageEntity.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
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;
Expand Down Expand Up @@ -43,7 +45,15 @@ public override async Task Preprocess(BotContext context, BotMessage message)

if (result.Ext != null)
{
await context.HighwayContext.UploadFile(Stream.Value, message.IsGroup() ? 1004 : 1003, ProtoHelper.Serialize(result.Ext));
// 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
Expand Down Expand Up @@ -119,4 +129,4 @@ internal override Elem[] Build()

return null;
}
}
}
Loading