Skip to content

Commit

Permalink
[Core] Implemented Receiving for VideoEntity.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan authored and Linwenxuan committed Feb 2, 2024
1 parent 47e3a5e commit e7d50c3
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,23 @@ private async Task ResolveAdditional(MessageChain chain)
record.AudioUrl = result.AudioUrl;
}
}

if (chain.HasTypeOf<VideoEntity>())
{
var video = chain.GetEntity<VideoEntity>();
if (video?.VideoUuid == null) return;

string uid = (chain.IsGroup
? await Collection.Business.CachingLogic.ResolveUid(chain.GroupUin, chain.FriendUin)
: chain.Uid) ?? "";
var @event = VideoDownloadEvent.Create(video.VideoUuid, uid, video.FilePath, "", "",chain.IsGroup);
var results = await Collection.Business.SendEvent(@event);
if (results.Count != 0)
{
var result = (VideoDownloadEvent)results[0];
video.VideoUrl = result.AudioUrl;
}
}

foreach (var mention in chain.OfType<MentionEntity>())
{
Expand Down
40 changes: 40 additions & 0 deletions Lagrange.Core/Internal/Event/Message/VideoDownloadEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace Lagrange.Core.Internal.Event.Message;

#pragma warning disable CS8618

internal class VideoDownloadEvent : ProtocolEvent
{
public string AudioUrl { get; }

public string Uuid { get; }

public string SelfUid { get; }

public string FileName { get; }

public string FileMd5 { get; }

public string? FileSha1 { get; }

public bool IsGroup { get; }

private VideoDownloadEvent(string uuid, string selfUid, string fileName, string fileMd5, string? fileSha1, bool isGroup) : base(true)
{
Uuid = uuid;
SelfUid = selfUid;
FileName = fileName;
FileSha1 = fileSha1;
IsGroup = isGroup;
FileMd5 = fileMd5;
}

private VideoDownloadEvent(int resultCode, string audioUrl) : base(resultCode)
{
AudioUrl = audioUrl;
}

public static VideoDownloadEvent Create(string uuid, string selfUid, string fileName, string fileMd5, string? fileSha1 = null, bool isGroup = false)
=> new(uuid, selfUid, fileName, fileMd5, fileSha1, isGroup);

public static VideoDownloadEvent Result(int resultCode, string url) => new(resultCode, url);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ namespace Lagrange.Core.Internal.Packets.Message.Element.Implementation;
[ProtoContract]
internal class VideoFile
{
[ProtoMember(1)] public byte[] FileUuid { get; set; }
[ProtoMember(1)] public string FileUuid { get; set; }

[ProtoMember(2)] public byte[] FileMd5 { get; set; }

[ProtoMember(3)] public byte[] FileName { get; set; }
[ProtoMember(3)] public string FileName { get; set; }

[ProtoMember(4)] public int FileFormat { get; set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using ProtoBuf;

namespace Lagrange.Core.Internal.Packets.Service.Oidb.Request;

// ReSharper disable InconsistentNaming
#pragma warning disable CS8618

/// <summary>
/// Video Download
/// </summary>
[ProtoContract]
[OidbSvcTrpcTcp(0x11e9, 200)]
internal class OidbSvcTrpcTcp0x11E9_200
{
[ProtoMember(1)] public OidbSvcTrpcTcp0x126D_200Field1 Field1 { get; set; }

[ProtoMember(3)] public OidbSvcTrpcTcp0x126D_200Field3 Field3 { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ namespace Lagrange.Core.Internal.Packets.Service.Oidb.Request;
// Resharper disable InconsistentNaming
#pragma warning disable CS8618

/// <summary>
/// Record Download
/// </summary>
[ProtoContract]
[OidbSvcTrpcTcp(0x126D, 200)]
internal class OidbSvcTrpcTcp0x126D_200
Expand Down
100 changes: 100 additions & 0 deletions Lagrange.Core/Internal/Service/Message/VideoDownloadService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using Lagrange.Core.Common;
using Lagrange.Core.Internal.Event;
using Lagrange.Core.Internal.Event.Message;
using Lagrange.Core.Internal.Packets.Service.Oidb;
using Lagrange.Core.Internal.Packets.Service.Oidb.Request;
using Lagrange.Core.Internal.Packets.Service.Oidb.Response;
using Lagrange.Core.Utility.Binary;
using Lagrange.Core.Utility.Extension;
using ProtoBuf;

namespace Lagrange.Core.Internal.Service.Message;

[EventSubscribe(typeof(VideoDownloadEvent))]
[Service("OidbSvcTrpcTcp.0x11e9_200")]
internal class VideoDownloadService : BaseService<VideoDownloadEvent>
{
protected override bool Build(VideoDownloadEvent input, BotKeystore keystore, BotAppInfo appInfo, BotDeviceInfo device,
out BinaryPacket output, out List<BinaryPacket>? extraPackets)
{
var packet = new OidbSvcTrpcTcpBase<OidbSvcTrpcTcp0x11E9_200>(new OidbSvcTrpcTcp0x11E9_200
{
Field1 = new OidbSvcTrpcTcp0x126D_200Field1
{
Field1 = new OidbSvcTrpcTcp0x126D_200Field1Field1
{
Field1 = input.IsGroup ? 3u : 34u,
Field2 = 200
},
Field2 = new OidbSvcTrpcTcp0x126D_200Field1Field2
{
Field1 = 2,
Field2 = 2,
Field3 = 1,
Field201 = new OidbSvcTrpcTcp0x126D_200Field1Field2Field201
{
Field1 = 2,
SelfUid = input.SelfUid
}
},
Field3 = new OidbSvcTrpcTcp0x126D_200Field1Field3
{
Field1 = 2
}
},
Field3 = new OidbSvcTrpcTcp0x126D_200Field3
{
Field1 = new OidbSvcTrpcTcp0x126D_200Field3Field1
{
Field1 = new OidbSvcTrpcTcp0x126D_200Field3Field1Field1
{
Field1 = 0,
FileHash = input.FileMd5,
FileSha1 = input.FileSha1 ?? "",
FileName = input.FileName,
Field5 = new OidbSvcTrpcTcp0x126D_200Field3Field1Field1Field5
{
Field1 = 2,
Field2 = 0,
Field3 = 0,
Field4 = 0
},
Field6 = 0,
Field7 = 0,
Field8 = 0,
Field9 = 0
},
FileUuid = input.Uuid,
Field3 = 0,
Field4 = 0,
Field5 = 0,
Field6 = 0
},
Field2 = new OidbSvcTrpcTcp0x126D_200Field3Field2
{
Field2 = new OidbSvcTrpcTcp0x126D_200Field3Field2Field2
{
Field1 = 0,
Field2 = 0
}
}
}
}, false, true);
output = packet.Serialize();
extraPackets = null;

return true;
}

protected override bool Parse(byte[] input, BotKeystore keystore, BotAppInfo appInfo, BotDeviceInfo device, out VideoDownloadEvent output,
out List<ProtocolEvent>? extraEvents)
{
var payload = Serializer.Deserialize<OidbSvcTrpcTcpResponse<OidbSvcTrpcTcp0x1026_200Response>>(input.AsSpan());
var body = payload.Body.Body;
string url = $"https://{body.Field3.Domain}{body.Field3.Suffix}{body.DownloadParams}";

output = VideoDownloadEvent.Result((int)payload.ErrorCode, url);
extraEvents = null;
return true;
}
}
4 changes: 2 additions & 2 deletions Lagrange.Core/Message/Entity/RecordEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class RecordEntity : IMessageEntity

internal string? FileSha1 { get; set; }

public RecordEntity() { }
internal RecordEntity() { }

public RecordEntity(string filePath)
{
Expand Down Expand Up @@ -76,5 +76,5 @@ IEnumerable<Elem> IMessageEntity.PackElement()
return null;
}

public string ToPreviewString() => $"[{nameof(RecordEntity)}]: {AudioUrl}";
public string ToPreviewString() => $"[{nameof(RecordEntity)}: {AudioUrl}]";
}
42 changes: 35 additions & 7 deletions Lagrange.Core/Message/Entity/VideoEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,71 @@
using System.Text;
using Lagrange.Core.Internal.Packets.Message.Element;
using Lagrange.Core.Internal.Packets.Message.Element.Implementation;
using Lagrange.Core.Utility.Extension;

namespace Lagrange.Core.Message.Entity;

[MessageElement(typeof(VideoFile))]
public class VideoEntity : IMessageEntity
{
public string FilePath { get; set; } = string.Empty;

public string VideoHash { get; set; } = string.Empty;

public Vector2 Size { get; }

public int VideoSize { get; }

public string VideoUrl { get; set; } = string.Empty;

internal Stream? VideoStream { get; set; }

internal string? VideoUuid { get; }

internal VideoEntity(Vector2 size, int videoSize, string filePath)
internal VideoEntity() { }

internal VideoEntity(Vector2 size, int videoSize, string filePath, string fileMd5, string videoUuid)
{
Size = size;
VideoSize = videoSize;
FilePath = filePath;
VideoHash = fileMd5;
VideoUuid = videoUuid;
}

public VideoEntity(string filePath)
{
FilePath = filePath;
VideoStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
}

internal VideoEntity() { }
public VideoEntity(byte[] file)
{
FilePath = string.Empty;
VideoStream = new MemoryStream(file);
}

IEnumerable<Elem> IMessageEntity.PackElement()
{
throw new NotImplementedException();
return new Elem[]
{
new()
{

}
};
}

IMessageEntity? IMessageEntity.UnpackElement(Elem elem)
{
if (elem.VideoFile is not { } videoFile) return null;

var size = new Vector2(videoFile.FileWidth, videoFile.FileHeight);

return new VideoEntity(size, elem.VideoFile.FileSize, Encoding.UTF8.GetString(elem.VideoFile.FileName));
var size = new Vector2(videoFile.ThumbWidth, videoFile.ThumbHeight);
return new VideoEntity(size, videoFile.FileSize, videoFile.FileName, videoFile.FileMd5.Hex(), videoFile.FileUuid);
}

public string ToPreviewString()
{
return $"[Video {Size.X}x{Size.Y}]: {VideoSize}";
return $"[Video {Size.X}x{Size.Y}]: {VideoSize} {VideoUrl}";
}
}
2 changes: 1 addition & 1 deletion Lagrange.Core/Message/MessagePacker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ private static MessageChain ParseChain(PushMsgBody message)
: new MessageChain(
message.ResponseHead.Grp.GroupUin,
message.ResponseHead.FromUin,
(uint)(message.ContentHead.Sequence ?? 0),
message.ContentHead.Sequence ?? 0,
message.ContentHead.NewId ?? 0);

chain.Time = DateTimeOffset.FromUnixTimeSeconds(message.ContentHead.Timestamp ?? 0).DateTime;
Expand Down

0 comments on commit e7d50c3

Please sign in to comment.