forked from mtusi2026/mtusi2026lab4
Реализовать лабораторную работу 4
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Messenger.Api.Contracts;
|
||||
|
||||
namespace Messenger.Api.Services;
|
||||
|
||||
public static class CursorService
|
||||
{
|
||||
public static string EncodeChatCursor(ChatCursor cursor)
|
||||
{
|
||||
var json = JsonSerializer.Serialize(cursor);
|
||||
|
||||
return Convert.ToBase64String(Encoding.UTF8.GetBytes(json))
|
||||
.TrimEnd('=')
|
||||
.Replace('+', '-')
|
||||
.Replace('/', '_');
|
||||
}
|
||||
|
||||
public static ChatCursor? DecodeChatCursor(string cursor)
|
||||
{
|
||||
try
|
||||
{
|
||||
var base64 = cursor
|
||||
.Replace('-', '+')
|
||||
.Replace('_', '/');
|
||||
|
||||
switch (base64.Length % 4)
|
||||
{
|
||||
case 2:
|
||||
base64 += "==";
|
||||
break;
|
||||
case 3:
|
||||
base64 += "=";
|
||||
break;
|
||||
case 1:
|
||||
return null;
|
||||
}
|
||||
|
||||
var bytes = Convert.FromBase64String(base64);
|
||||
var json = Encoding.UTF8.GetString(bytes);
|
||||
|
||||
return JsonSerializer.Deserialize<ChatCursor>(json);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user