Adds streaming to GetContents

This commit is contained in:
Jonathan Bourdon
2024-07-17 21:47:31 -04:00
parent 25ea9b50c7
commit bc87331903
12 changed files with 201 additions and 42 deletions

View File

@@ -2,8 +2,6 @@
namespace Hutopy.Web.Common;
public class Shared(string claimName) : Exception;
public static class ClaimsPrincipalExtensions
{
public static Guid GetUserId(this ClaimsPrincipal claims)
@@ -30,7 +28,7 @@ public static class ClaimsPrincipalExtensions
{
var claim = claims.FindFirst(key);
if (claim is null) throw new Shared(key);
if (claim is null) throw new MissingClaimException(key);
if (typeof(TValue) == typeof(Guid))
{

View File

@@ -0,0 +1,30 @@
using System.Buffers.Binary;
using System.Security.Cryptography;
namespace Hutopy.Web.Common;
public static class GuidHelper
{
// TODO: Delete when NET9 is release!
public static Guid GenerateUuidV7()
{
Span<byte> uuidv7 = stackalloc byte[16];
ulong unixTimeTicks = (ulong)DateTimeOffset.UtcNow.Subtract(DateTimeOffset.UnixEpoch).Ticks;
ulong unixTsMs = (unixTimeTicks & 0x0FFFFFFFFFFFF000) << 4;
ulong unixTsMsVer = unixTsMs | 0b0111UL << 12;
ulong randA = unixTimeTicks & 0x0000000000000FFF;
// merge "unix_ts_ms", "ver" and "rand_a"
ulong hi = unixTsMsVer | randA;
BinaryPrimitives.WriteUInt64BigEndian(uuidv7, hi);
// fill "rand_b" and "var"
RandomNumberGenerator.Fill(uuidv7[8..]);
// set "var"
byte varOctet = uuidv7[8];
varOctet = (byte)(varOctet & 0b00111111);
varOctet = (byte)(varOctet | 0b10111111);
uuidv7[8] = varOctet;
var value = Convert.ToHexString(uuidv7);
return Guid.Parse(value);
}
}

View File

@@ -0,0 +1,3 @@
namespace Hutopy.Web.Common;
public class MissingClaimException(string claimName) : Exception;