| | | 1 | | using Anichron.Core.Data.Repository; |
| | | 2 | | using NodaTime.Text; |
| | | 3 | | using System.Buffers.Text; |
| | | 4 | | using System.Text; |
| | | 5 | | |
| | | 6 | | namespace Anichron.API.Endpoints; |
| | | 7 | | |
| | | 8 | | internal static class HiddenCursors |
| | | 9 | | { |
| | | 10 | | private const char separator = ' '; |
| | | 11 | | |
| | 122 | 12 | | internal static string FormatHiddenAt(Instant hiddenAt) => InstantPattern.ExtendedIso.Format(hiddenAt); |
| | | 13 | | |
| | | 14 | | internal static string Format(AssetView last) |
| | 1 | 15 | | => Base64Url.EncodeToString(Encoding.UTF8.GetBytes($"{FormatHiddenAt(last.HiddenAt!.Value)}{separator}{last.Id:N |
| | | 16 | | |
| | | 17 | | internal static HiddenCursor? Parse(string text) |
| | 6 | 18 | | { |
| | 6 | 19 | | if (!Base64Url.IsValid(text)) |
| | 1 | 20 | | return null; |
| | | 21 | | |
| | 5 | 22 | | var parts = Encoding.UTF8.GetString(Base64Url.DecodeFromChars(text)).Split(separator); |
| | 5 | 23 | | return parts is [var hiddenAt, var id] |
| | 5 | 24 | | && InstantPattern.ExtendedIso.Parse(hiddenAt) is { Success: true } parsed |
| | 5 | 25 | | && Guid.TryParseExact(id, "N", out var assetId) |
| | 5 | 26 | | ? new HiddenCursor(parsed.Value, assetId) |
| | 5 | 27 | | : null; |
| | 6 | 28 | | } |
| | | 29 | | } |