-
Notifications
You must be signed in to change notification settings - Fork 51
[Core] Add FlashTransfer Logic, Fix Aot and mac sending image issue #59
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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,113 @@ | ||
| using System.Security.Cryptography; | ||
| using Lagrange.Core.Internal.Packets.Service; | ||
| using Lagrange.Core.Utility; | ||
| using Lagrange.Core.Utility.Cryptography; | ||
| using Lagrange.Core.Utility.Extension; | ||
|
|
||
| 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) | ||
| { | ||
| var sha1StateVs = new FlashTransferSha1StateV { State = [] }; | ||
| var chunkCount = (uint)((bodyStream.Length + ChunkSize - 1) / ChunkSize); | ||
|
|
||
| var sha1Stream = new Sha1Stream(); | ||
| for (uint i = 0; i < chunkCount; i++) | ||
| { | ||
| if (i != chunkCount - 1) | ||
| { | ||
| var accLength = (int)((i + 1) * ChunkSize); | ||
| var accBuffer = new byte[accLength]; | ||
|
|
||
| bodyStream.Position = 0; | ||
| await bodyStream.ReadExactlyAsync(accBuffer, 0, accLength); | ||
|
|
||
| var accSpan = accBuffer.AsSpan(); | ||
| var digest = new byte[20]; | ||
| sha1Stream.Update(accSpan); | ||
| sha1Stream.Hash(digest, false); | ||
| sha1Stream.Reset(); | ||
| sha1StateVs.State.Add(digest.ToArray()); | ||
| } | ||
| else | ||
| { | ||
| bodyStream.Position = 0; | ||
| sha1StateVs.State.Add(bodyStream.Sha1()); | ||
| } | ||
| } | ||
|
|
||
| for (uint i = 0; i < chunkCount; i++) | ||
| { | ||
| var chunkStart = (long)(i * ChunkSize); | ||
| var chunkLength = (int)Math.Min(ChunkSize, bodyStream.Length - chunkStart); | ||
|
|
||
| bodyStream.Position = chunkStart; | ||
| var uploadBuffer = new byte[chunkLength]; | ||
| await bodyStream.ReadExactlyAsync(uploadBuffer, 0, chunkLength); | ||
|
|
||
| var success = await UploadChunk(uKey, (uint)chunkStart, sha1StateVs, uploadBuffer); | ||
| 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; } | ||
| [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
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.
Uh oh!
There was an error while loading. Please reload this page.