Skip to content

Commit

Permalink
[All] feat: implemented get_private_file_url (#703)
Browse files Browse the repository at this point in the history
* [All] feat: implemented `get_private_file_url`

Signed-off-by: ishkong <[email protected]>

* Uniform variable name styles

Signed-off-by: ishkong <[email protected]>

* 这玩意儿居然不是必须的?

Signed-off-by: ishkong <[email protected]>

---------

Signed-off-by: ishkong <[email protected]>
  • Loading branch information
ishkong authored Dec 6, 2024
1 parent 07cab5b commit 967c232
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Lagrange.Core/Common/Interface/Api/OperationExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,7 @@ public static Task<string> UploadImage(this BotContext bot, ImageEntity entity)

public static Task<(int Retcode, string Message)> SetPinGroup(this BotContext bot, uint uin, bool isPin)
=> bot.ContextCollection.Business.OperationLogic.SetPinGroup(uin, isPin);

public static Task<string> FetchPrivateFSDownload(this BotContext bot, string fileId, string fileHash, uint userId)
=> bot.ContextCollection.Business.OperationLogic.FetchPrivateFSDownload(fileId, fileHash, userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,15 @@ public async Task<string> FetchGroupFSDownload(uint groupUin, string fileId)
var events = await Collection.Business.SendEvent(groupFSDownloadEvent);
return $"{((GroupFSDownloadEvent)events[0]).FileUrl}{fileId}";
}

public async Task<string> FetchPrivateFSDownload(string fileId, string fileHash, uint userId)
{
var uid = await Collection.Business.CachingLogic.ResolveUid(null, userId);
if (uid == null) return "false";
var privateFSDownloadEvent = FileDownloadEvent.Create(fileId, fileHash, uid, uid);
var events = await Collection.Business.SendEvent(privateFSDownloadEvent);
return $"{((FileDownloadEvent)events[0]).FileUrl}";
}

public async Task<(int, string)> GroupFSMove(uint groupUin, string fileId, string parentDirectory, string targetDirectory)
{
Expand Down
13 changes: 13 additions & 0 deletions Lagrange.OneBot/Core/Entity/Action/OneBotGetPrivateFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Text.Json.Serialization;

namespace Lagrange.OneBot.Core.Entity.Action;

[Serializable]
public class OneBotGetPrivateFile
{
[JsonPropertyName("user_id")] public uint UserId { get; set; }

[JsonPropertyName("file_hash")] public string FileHash { get; set; } = string.Empty;

[JsonPropertyName("file_id")] public required string FileId { get; set; }
}
21 changes: 19 additions & 2 deletions Lagrange.OneBot/Core/Entity/Notify/OneBotPrivateFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,26 @@
namespace Lagrange.OneBot.Core.Entity.Notify;

[Serializable]
public class OneBotPrivateFile(uint selfId, uint userId, OneBotFileInfo fileInfo) : OneBotNotify(selfId, "offline_file")
public class OneBotPrivateFile(uint selfId, uint userId, OneBotPrivateFileInfo fileInfo) : OneBotNotify(selfId, "offline_file")
{
[JsonPropertyName("user_id")] public uint UserId { get; set; } = userId;

[JsonPropertyName("file")] public OneBotFileInfo Info { get; set; } = fileInfo;
[JsonPropertyName("file")] public OneBotPrivateFileInfo Info { get; set; } = fileInfo;
}



[Serializable]

public class OneBotPrivateFileInfo(string id, string name, ulong size, string url, string hash)
{
[JsonPropertyName("id")] public string Id { get; set; } = id;

[JsonPropertyName("name")] public string Name { get; set; } = name;

[JsonPropertyName("size")] public ulong Size { get; set; } = size;

[JsonPropertyName("url")] public string Url { get; set; } = url;

[JsonPropertyName("hash")] public string FileHash { get; set; } = hash;
}
2 changes: 1 addition & 1 deletion Lagrange.OneBot/Core/Notify/NotifyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void RegisterEvents()
{
if (@event.Chain.GetEntity<FileEntity>() is { FileUrl: { } url } file)
{
var fileInfo = new OneBotFileInfo(file.FileId ?? "", file.FileName, (ulong)file.FileSize, url);
var fileInfo = new OneBotPrivateFileInfo(file.FileUuid ?? "", file.FileName, (ulong)file.FileSize, url, file.FileHash ?? "");
await service.SendJsonAsync(new OneBotPrivateFile(bot.BotUin, @event.Chain.FriendUin, fileInfo));
}
};
Expand Down
23 changes: 23 additions & 0 deletions Lagrange.OneBot/Core/Operation/File/PrivateFSOperations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using Lagrange.Core;
using Lagrange.Core.Common.Interface.Api;
using Lagrange.OneBot.Core.Entity.Action;
using Lagrange.OneBot.Core.Operation.Converters;

namespace Lagrange.OneBot.Core.Operation.File;

[Operation("get_private_file_url")]
public class GetPrivateFileUrlOperation : IOperation
{
public async Task<OneBotResult> HandleOperation(BotContext context, JsonNode? payload)
{
if (payload.Deserialize<OneBotGetPrivateFile>(SerializerOptions.DefaultOptions) is { } url)
{
string raw = await context.FetchPrivateFSDownload(url.FileId, url.FileHash, url.UserId);
return new OneBotResult(new JsonObject { { "url", raw } }, 0, "ok");
}

throw new Exception();
}
}

0 comments on commit 967c232

Please sign in to comment.