-
Notifications
You must be signed in to change notification settings - Fork 51
[Core] Add FlashTransfer Logic, Fix Aot and mac sending image issue #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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为什么不能并发,回答我 | ||
| 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
31
Lagrange.Core/Internal/Packets/Service/FlashTransferUploadReq.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
10
Lagrange.Core/Internal/Packets/Service/FlashTransferUploadResp.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.