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

Commit 97484c1

Browse files
authored
[All] Add reasons for failure to upload files (#791)
1 parent 88f1e64 commit 97484c1

File tree

11 files changed

+112
-57
lines changed

11 files changed

+112
-57
lines changed

Lagrange.Core/Common/Interface/Api/GroupExt.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ public static Task<bool> GroupSetMessageReaction(this BotContext bot, uint group
107107

108108
public static Task<bool> GroupSetMessageReaction(this BotContext bot, uint groupUin, uint sequence, string code, bool isSet)
109109
=> bot.ContextCollection.Business.OperationLogic.SetMessageReaction(groupUin, sequence, code, isSet);
110-
110+
111111
public static Task<bool> GroupSetAvatar(this BotContext bot, uint groupUin, ImageEntity imageEntity)
112112
=> bot.ContextCollection.Business.OperationLogic.GroupSetAvatar(groupUin, imageEntity);
113-
113+
114114
public static Task<(uint, uint)> GroupRemainAtAll(this BotContext bot, uint groupUin)
115115
=> bot.ContextCollection.Business.OperationLogic.GroupRemainAtAll(groupUin);
116116

@@ -143,8 +143,11 @@ public static Task<string> FetchGroupFSDownload(this BotContext bot, uint groupU
143143
public static Task<(int RetCode, string RetMsg)> GroupFSRenameFolder(this BotContext bot, uint groupUin, string folderId, string newFolderName)
144144
=> bot.ContextCollection.Business.OperationLogic.GroupFSRenameFolder(groupUin, folderId, newFolderName);
145145

146-
public static Task<bool> GroupFSUpload(this BotContext bot, uint groupUin, FileEntity fileEntity, string targetDirectory = "/")
147-
=> bot.ContextCollection.Business.OperationLogic.GroupFSUpload(groupUin, fileEntity, targetDirectory);
146+
public static async Task<bool> GroupFSUpload(this BotContext bot, uint groupUin, FileEntity fileEntity, string targetDirectory = "/")
147+
=> (await GroupFSUploadWithResult(bot, groupUin, fileEntity, targetDirectory)).IsSuccess;
148+
149+
public static Task<OperationResult<object>> GroupFSUploadWithResult(this BotContext bot, uint groupUin, FileEntity fileEntity, string targetDirectory = "/")
150+
=> bot.ContextCollection.Business.OperationLogic.GroupFSUploadWithResult(groupUin, fileEntity, targetDirectory);
148151

149152
#endregion
150153
}

Lagrange.Core/Common/Interface/Api/OperationExt.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,11 @@ public static Task<bool> MarkAsRead(this BotContext bot, MessageChain targetChai
225225
targetChain.Sequence, timestamp);
226226
}
227227

228-
public static Task<bool> UploadFriendFile(this BotContext bot, uint targetUin, FileEntity fileEntity)
229-
=> bot.ContextCollection.Business.OperationLogic.UploadFriendFile(targetUin, fileEntity);
228+
public static async Task<bool> UploadFriendFile(this BotContext bot, uint targetUin, FileEntity fileEntity)
229+
=> (await UploadFriendFileWithResult(bot, targetUin, fileEntity)).IsSuccess;
230+
231+
public static Task<OperationResult<object>> UploadFriendFileWithResult(this BotContext bot, uint targetUin, FileEntity fileEntity)
232+
=> bot.ContextCollection.Business.OperationLogic.UploadFriendFileWithResult(targetUin, fileEntity);
230233

231234
public static Task<bool> FriendPoke(this BotContext bot, uint friendUin)
232235
=> bot.ContextCollection.Business.OperationLogic.FriendPoke(friendUin);

Lagrange.Core/Internal/Context/Logic/Implementation/OperationLogic.cs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -280,30 +280,48 @@ public async Task<string> FetchPrivateFSDownload(string fileId, string fileHash,
280280
return (retCode, retMsg);
281281
}
282282

283-
public Task<bool> GroupFSUpload(uint groupUin, FileEntity fileEntity, string targetDirectory)
283+
public async Task<OperationResult<object>> GroupFSUploadWithResult(uint groupUin, FileEntity fileEntity, string targetDirectory)
284284
{
285285
try
286286
{
287-
return FileUploader.UploadGroup(Collection, MessageBuilder.Group(groupUin).Build(), fileEntity, targetDirectory);
287+
(int retcode, string message) = await FileUploader.UploadGroup(Collection, MessageBuilder.Group(groupUin).Build(), fileEntity, targetDirectory);
288+
return new OperationResult<object>
289+
{
290+
Retcode = retcode,
291+
Message = message
292+
};
288293
}
289-
catch
294+
catch (Exception e)
290295
{
291-
return Task.FromResult(false);
296+
return new OperationResult<object>
297+
{
298+
Retcode = -99999,
299+
Message = e.Message
300+
};
292301
}
293302
}
294303

295-
public async Task<bool> UploadFriendFile(uint targetUin, FileEntity fileEntity)
304+
public async Task<OperationResult<object>> UploadFriendFileWithResult(uint targetUin, FileEntity fileEntity)
296305
{
297306
string? uid = await Collection.Business.CachingLogic.ResolveUid(null, targetUin);
298307
var chain = new MessageChain(targetUin, Collection.Keystore.Uid ?? "", uid ?? "") { fileEntity };
299308

300309
try
301310
{
302-
return await FileUploader.UploadPrivate(Collection, chain, fileEntity);
311+
(int retcode, string message) = await FileUploader.UploadPrivate(Collection, chain, fileEntity);
312+
return new OperationResult<object>
313+
{
314+
Retcode = retcode,
315+
Message = message
316+
};
303317
}
304-
catch
318+
catch (Exception e)
305319
{
306-
return false;
320+
return new OperationResult<object>
321+
{
322+
Retcode = -99999,
323+
Message = e.Message
324+
};
307325
}
308326
}
309327

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ namespace Lagrange.Core.Internal.Context.Uploader;
1111
/// </summary>
1212
internal static class FileUploader
1313
{
14-
public static async Task<bool> UploadPrivate(ContextCollection context, MessageChain chain, IMessageEntity entity)
14+
public static async Task<(int Retcode, string Message)> UploadPrivate(ContextCollection context, MessageChain chain, IMessageEntity entity)
1515
{
16-
if (entity is not FileEntity { FileStream: not null } file) return false;
17-
16+
if (entity is not FileEntity { FileStream: not null } file) return (-91000, "Not FileEntity");
17+
1818
var uploadEvent = FileUploadEvent.Create(chain.Uid ?? "", file);
1919
var result = await context.Business.SendEvent(uploadEvent);
20+
if (result.Count == 0) return (-90000, "(FileUploadEvent) No Event");
2021
var uploadResp = (FileUploadEvent)result[0];
21-
22+
if (!uploadResp.IsSuccess) return (uploadResp.ResultCode, $"(FileUploadEvent) {uploadResp.Message}");
23+
2224
if (!uploadResp.IsExist)
2325
{
2426
var ext = new FileUploadExt
@@ -75,23 +77,27 @@ public static async Task<bool> UploadPrivate(ContextCollection context, MessageC
7577
file.FileUuid = uploadResp.FileId;
7678

7779
bool hwSuccess = await context.Highway.UploadSrcByStreamAsync(95, file.FileStream, await Common.GetTicket(context), file.FileMd5, ext.Serialize().ToArray());
78-
if (!hwSuccess) return false;
80+
if (!hwSuccess) return (-91001, "Highway Failed");
7981
}
8082

8183
await file.FileStream.DisposeAsync();
8284
var sendEvent = SendMessageEvent.Create(chain);
8385
var sendResult = await context.Business.SendEvent(sendEvent);
84-
return sendResult.Count != 0 && ((SendMessageEvent)sendResult[0]).MsgResult.Result == 0;
86+
if (sendResult.Count == 0) return (-90000, "(SendMessageEvent) No Event");
87+
var sendResp = (SendMessageEvent)sendResult[0];
88+
if (sendResp.ResultCode != 0) return (sendResp.ResultCode, $"(SendMessageEvent) Failed!");
89+
return (0, "ok");
8590
}
8691

87-
public static async Task<bool> UploadGroup(ContextCollection context, MessageChain chain, IMessageEntity entity, string targetDirectory)
92+
public static async Task<(int Retcode, string Message)> UploadGroup(ContextCollection context, MessageChain chain, IMessageEntity entity, string targetDirectory)
8893
{
89-
if (entity is not FileEntity { FileStream: not null } file) return false;
90-
94+
if (entity is not FileEntity { FileStream: not null } file) return (-91000, "Not FileEntity");
95+
9196
var uploadEvent = GroupFSUploadEvent.Create(chain.GroupUin ?? 0, targetDirectory, file);
9297
var result = await context.Business.SendEvent(uploadEvent);
98+
if (result.Count == 0) return (-90000, "(GroupFSUploadEvent) No Event");
9399
var uploadResp = (GroupFSUploadEvent)result[0];
94-
100+
if (!uploadResp.IsSuccess) return (uploadResp.ResultCode, $"(GroupFSUploadEvent) {uploadResp.Message}");
95101

96102
if (!uploadResp.IsExist)
97103
{
@@ -147,12 +153,16 @@ public static async Task<bool> UploadGroup(ContextCollection context, MessageCha
147153
};
148154

149155
bool hwSuccess = await context.Highway.UploadSrcByStreamAsync(71, file.FileStream, await Common.GetTicket(context), file.FileMd5, ext.Serialize().ToArray());
150-
if (!hwSuccess) return false;
156+
if (!hwSuccess) return (-91001, "Highway Failed");
151157
}
152-
158+
153159
await file.FileStream.DisposeAsync();
154160
var sendEvent = GroupSendFileEvent.Create(chain.GroupUin ?? 0, uploadResp.FileId);
155161
var sendResult = await context.Business.SendEvent(sendEvent);
156-
return sendResult.Count != 0 && ((GroupSendFileEvent)sendResult[0]).ResultCode == 0;
162+
if (sendResult.Count == 0) return (-90000, "(GroupSendFileEvent) No Event");
163+
var sendResp = (GroupSendFileEvent)sendResult[0];
164+
if (!sendResp.IsSuccess) return (sendResp.ResultCode, $"(GroupSendFileEvent) {sendResp.Message}");
165+
166+
return (0, "ok");
157167
}
158168
}
Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics.CodeAnalysis;
12
using Lagrange.Core.Message.Entity;
23

34
namespace Lagrange.Core.Internal.Event.Message;
@@ -7,29 +8,35 @@ namespace Lagrange.Core.Internal.Event.Message;
78
internal class FileUploadEvent : ProtocolEvent
89
{
910
public string TargetUid { get; }
10-
11+
1112
public FileEntity Entity { get; }
1213

1314
public bool IsExist { get; }
14-
15+
1516
public string FileId { get; }
16-
17+
1718
public byte[] UploadKey { get; }
18-
19+
1920
public string Ip { get; }
20-
21+
2122
public uint Port { get; }
22-
23+
2324
public string Addon { get; }
2425

26+
public string? Message { get; }
27+
28+
[MemberNotNullWhen(false, nameof(Message))]
29+
public bool IsSuccess => ResultCode == 0;
30+
2531
private FileUploadEvent(string targetUid, FileEntity entity) : base(true)
2632
{
2733
TargetUid = targetUid;
2834
Entity = entity;
2935
}
3036

31-
private FileUploadEvent(int resultCode, bool isExist, string fileId, byte[] uploadKey, string ip, uint port, string addon) : base(resultCode)
37+
private FileUploadEvent(int resultCode, string? message, bool isExist, string fileId, byte[] uploadKey, string ip, uint port, string addon) : base(resultCode)
3238
{
39+
Message = message;
3340
IsExist = isExist;
3441
FileId = fileId;
3542
UploadKey = uploadKey;
@@ -40,6 +47,6 @@ private FileUploadEvent(int resultCode, bool isExist, string fileId, byte[] uplo
4047

4148
public static FileUploadEvent Create(string targetUid, FileEntity entity) => new(targetUid, entity);
4249

43-
public static FileUploadEvent Result(int resultCode, bool isExist, string fileId, byte[] uploadKey, string ip, uint port, string addon)
44-
=> new(resultCode, isExist, fileId, uploadKey, ip, port, addon);
50+
public static FileUploadEvent Result(int resultCode, string? message, bool isExist, string fileId, byte[] uploadKey, string ip, uint port, string addon)
51+
=> new(resultCode, message, isExist, fileId, uploadKey, ip, port, addon);
4552
}
Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics.CodeAnalysis;
12
using Lagrange.Core.Message.Entity;
23

34
#pragma warning disable CS8618
@@ -9,30 +10,36 @@ internal class GroupFSUploadEvent : ProtocolEvent
910
public uint GroupUin { get; }
1011

1112
public string TargetDirectory { get; }
12-
13+
1314
public FileEntity Entity { get; }
1415

1516
public bool IsExist { get; }
16-
17+
1718
public string FileId { get; }
18-
19+
1920
public byte[] UploadKey { get; }
20-
21+
2122
public byte[] CheckKey { get; }
22-
23+
2324
public string Ip { get; }
24-
25+
2526
public uint Port { get; }
2627

28+
public string? Message { get; }
29+
30+
[MemberNotNullWhen(false, nameof(Message))]
31+
public bool IsSuccess => ResultCode == 0;
32+
2733
private GroupFSUploadEvent(uint groupUin, string targetDirectory, FileEntity entity) : base(true)
2834
{
2935
GroupUin = groupUin;
3036
TargetDirectory = targetDirectory;
3137
Entity = entity;
3238
}
3339

34-
private GroupFSUploadEvent(int resultCode, bool isExist, string fileId, byte[] uploadKey, byte[] checkKey, string ip, uint port) : base(resultCode)
40+
private GroupFSUploadEvent(int resultCode, string? message, bool isExist, string fileId, byte[] uploadKey, byte[] checkKey, string ip, uint port) : base(resultCode)
3541
{
42+
Message = message;
3643
IsExist = isExist;
3744
FileId = fileId;
3845
UploadKey = uploadKey;
@@ -41,9 +48,9 @@ private GroupFSUploadEvent(int resultCode, bool isExist, string fileId, byte[] u
4148
Port = port;
4249
}
4350

44-
public static GroupFSUploadEvent Create(uint groupUin, string targetDirectory, FileEntity entity)
51+
public static GroupFSUploadEvent Create(uint groupUin, string targetDirectory, FileEntity entity)
4552
=> new(groupUin, targetDirectory, entity);
4653

47-
public static GroupFSUploadEvent Result(int resultCode, bool isExist, string fileId, byte[] uploadKey, byte[] checkKey, string ip, uint port)
48-
=> new(resultCode, isExist, fileId, uploadKey, checkKey, ip, port);
54+
public static GroupFSUploadEvent Result(int resultCode, string? message, bool isExist, string fileId, byte[] uploadKey, byte[] checkKey, string ip, uint port)
55+
=> new(resultCode, message, isExist, fileId, uploadKey, checkKey, ip, port);
4956
}

Lagrange.Core/Internal/Event/Message/GroupSendFileEvent.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
13
namespace Lagrange.Core.Internal.Event.Message;
24

35
internal class GroupSendFileEvent : ProtocolEvent
46
{
57
public uint GroupUin { get; }
6-
8+
79
public string FileKey { get; } = string.Empty;
810

11+
public string? Message { get; }
12+
13+
[MemberNotNullWhen(false, nameof(Message))]
14+
public bool IsSuccess => ResultCode == 0;
15+
916
private GroupSendFileEvent(uint groupUin, string fileKey) : base(true)
1017
{
1118
GroupUin = groupUin;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected override bool Parse(Span<byte> input, BotKeystore keystore, BotAppInfo
5050
var payload = Serializer.Deserialize<OidbSvcTrpcTcpBase<OidbSvcTrpcTcp0xE37Response>>(input);
5151
var upload = payload.Body.Upload;
5252

53-
output = FileUploadEvent.Result((int)payload.ErrorCode, upload.BoolFileExist, upload.Uuid, upload.MediaPlatformUploadKey, upload.UploadIp, upload.UploadPort, upload.FileAddon);
53+
output = FileUploadEvent.Result((int)payload.ErrorCode, payload.ErrorMsg, upload.BoolFileExist, upload.Uuid, upload.MediaPlatformUploadKey, upload.UploadIp, upload.UploadPort, upload.FileAddon);
5454
extraEvents = null;
5555
return true;
5656
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ protected override bool Build(GroupFSUploadEvent input, BotKeystore keystore, Bo
4242
return true;
4343
}
4444

45-
protected override bool Parse(Span<byte> input, BotKeystore keystore, BotAppInfo appInfo, BotDeviceInfo device,
45+
protected override bool Parse(Span<byte> input, BotKeystore keystore, BotAppInfo appInfo, BotDeviceInfo device,
4646
out GroupFSUploadEvent output, out List<ProtocolEvent>? extraEvents)
4747
{
4848
var payload = Serializer.Deserialize<OidbSvcTrpcTcpBase<OidbSvcTrpcTcp0x6D6Response>>(input);
4949
var upload = payload.Body.Upload;
50-
51-
output = GroupFSUploadEvent.Result(upload.RetCode, upload.BoolFileExist, upload.FileId, upload.FileKey, upload.CheckKey, upload.UploadIp, upload.UploadPort);
50+
51+
output = GroupFSUploadEvent.Result(upload.RetCode, upload.RetMsg, upload.BoolFileExist, upload.FileId, upload.FileKey, upload.CheckKey, upload.UploadIp, upload.UploadPort);
5252
extraEvents = null;
5353
return true;
5454
}

Lagrange.OneBot/Core/Operation/File/UploadGroupFileOperation.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ public async Task<OneBotResult> HandleOperation(BotContext context, JsonNode? pa
1818
var entity = new FileEntity(file.File);
1919
if (file.Name != null) entity.FileName = file.Name;
2020

21-
bool result = await context.GroupFSUpload(file.GroupId, entity, file.Folder ?? "/");
22-
return new OneBotResult(null, result ? 0 : 1, result ? "ok" : "failed");
21+
var result = await context.GroupFSUploadWithResult(file.GroupId, entity, file.Folder ?? "/");
22+
return new OneBotResult(null, result.Retcode, result.Message ?? "ok");
2323
}
24-
24+
2525
throw new Exception();
2626
}
2727
}

0 commit comments

Comments
 (0)