55using Lagrange . Core . Internal . Events . Message ;
66using Lagrange . Core . Internal . Events . System ;
77using Lagrange . Core . Internal . Packets . Service ;
8+ using Lagrange . Core . Message ;
9+ using Lagrange . Core . Message . Entities ;
810using Lagrange . Core . Utility ;
911using Lagrange . Core . Utility . Cryptography ;
1012using Lagrange . Core . Utility . Extension ;
13+ using Lagrange . Proto . Primitives ;
14+ using Lagrange . Proto . Serialization ;
15+ using FileInfo = Lagrange . Core . Internal . Packets . Service . FileInfo ;
1116
1217namespace Lagrange . Core . Internal . Logic ;
1318
@@ -284,4 +289,93 @@ public async Task SetGroupReaction(long groupUin, ulong sequence, string code, b
284289 new ReduceGroupReactionEventReq ( groupUin , sequence , code )
285290 ) ;
286291 }
292+
293+ public async Task < string > GetNTV2RichMediaUrl ( string uuid )
294+ {
295+ int remainder = uuid . Length % 4 ;
296+ int length = remainder == 0 ? uuid . Length : uuid . Length + ( 4 - remainder ) ;
297+ string base64 = uuid . Replace ( '-' , '+' ) . Replace ( '_' , '/' ) . PadRight ( length , '=' ) ;
298+
299+ ulong appId = 0 ;
300+ uint ttl = 0 ;
301+ var reader = new ProtoReader ( Convert . FromBase64String ( base64 ) ) ;
302+ while ( ! reader . IsCompleted )
303+ {
304+ uint tag = reader . DecodeVarIntUnsafe < uint > ( ) ;
305+ switch ( tag >>> 3 )
306+ {
307+ case 4 :
308+ {
309+ appId = reader . DecodeVarInt < ulong > ( ) ;
310+ break ;
311+ }
312+ case 10 :
313+ {
314+ ttl = reader . DecodeVarInt < uint > ( ) ;
315+ break ;
316+ }
317+ default :
318+ {
319+ reader . SkipField ( ( WireType ) ( tag & 0b111 ) ) ;
320+ break ;
321+ }
322+ }
323+ }
324+
325+ NTV2RichMediaDownloadEventResp response = appId switch
326+ {
327+ // Record
328+ 1402 => await context . EventContext . SendEvent < RecordDownloadEventResp > ( new RecordDownloadEventReq (
329+ CreateFakeFriendMessage ( context . BotUin ) ,
330+ CreateFakeEntity < RecordEntity > ( uuid , ttl )
331+ ) ) ,
332+ 1403 => await context . EventContext . SendEvent < RecordGroupDownloadEventResp > ( new RecordGroupDownloadEventReq (
333+ CreateFakeGroupMessage ( context . BotUin ) ,
334+ CreateFakeEntity < RecordEntity > ( uuid , ttl )
335+ ) ) ,
336+ // Video
337+ 1413 => await context . EventContext . SendEvent < VideoDownloadEventResp > ( new VideoDownloadEventReq (
338+ CreateFakeFriendMessage ( context . BotUin ) ,
339+ CreateFakeEntity < VideoEntity > ( uuid , ttl )
340+ ) ) ,
341+ 1415 => await context . EventContext . SendEvent < VideoGroupDownloadEventResp > ( new VideoGroupDownloadEventReq (
342+ CreateFakeGroupMessage ( context . BotUin ) ,
343+ CreateFakeEntity < VideoEntity > ( uuid , ttl )
344+ ) ) ,
345+ // Image
346+ 1406 => await context . EventContext . SendEvent < ImageDownloadEventResp > ( new ImageDownloadEventReq (
347+ CreateFakeFriendMessage ( context . BotUin ) ,
348+ CreateFakeEntity < ImageEntity > ( uuid , ttl )
349+ ) ) ,
350+ 1407 => await context . EventContext . SendEvent < ImageGroupDownloadEventResp > ( new ImageGroupDownloadEventReq (
351+ CreateFakeGroupMessage ( context . BotUin ) ,
352+ CreateFakeEntity < ImageEntity > ( uuid , ttl )
353+ ) ) ,
354+ _ => throw new NotSupportedException ( $ "Unsupported AppId: { appId } ") ,
355+ } ;
356+
357+ return response . Url ;
358+
359+ static BotMessage CreateFakeFriendMessage ( long uin ) => BotMessage . CreateCustomFriend ( uin , string . Empty , uin , string . Empty , DateTime . Now , [ ] ) ;
360+ static BotMessage CreateFakeGroupMessage ( long uin ) => BotMessage . CreateCustomGroup ( uin , 0 , string . Empty , DateTime . Now , [ ] ) ;
361+ static RichMediaEntityBase CreateFakeEntity < T > ( string fileUuid , uint ttl ) where T : RichMediaEntityBase , new ( )
362+ {
363+ return new T
364+ {
365+ MsgInfo = new MsgInfo
366+ {
367+ MsgInfoBody = [ new MsgInfoBody {
368+ Index = new IndexNode {
369+ Info = new FileInfo { } ,
370+ FileUuid = fileUuid ,
371+ StoreId = 1 ,
372+ UploadTime = 0 ,
373+ Ttl = ttl ,
374+ SubType = 0 ,
375+ }
376+ } ]
377+ }
378+ } ;
379+ }
380+ }
287381}
0 commit comments