JJ Colocate
This commit is contained in:
@@ -1,47 +1,47 @@
|
||||
using AobaCore.Models;
|
||||
|
||||
using AobaServer.Models;
|
||||
|
||||
using Grpc.Core;
|
||||
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace AobaServer.Utils;
|
||||
|
||||
public static class Extensions
|
||||
{
|
||||
public static ObjectId ToObjectId(this string? value)
|
||||
{
|
||||
if(value == null)
|
||||
return ObjectId.Empty;
|
||||
if(ObjectId.TryParse(value, out ObjectId result))
|
||||
return result;
|
||||
return ObjectId.Empty;
|
||||
}
|
||||
|
||||
public static string GetToken(this User user, AuthInfo authInfo)
|
||||
{
|
||||
var handler = new JwtSecurityTokenHandler();
|
||||
var signCreds = new SigningCredentials(new SymmetricSecurityKey(authInfo.SecureKey), SecurityAlgorithms.HmacSha256);
|
||||
var identity = user.GetIdentity();
|
||||
var token = handler.CreateEncodedJwt(authInfo.Issuer, authInfo.Audience, identity, notBefore: DateTime.Now, expires: null, issuedAt: DateTime.Now, signCreds);
|
||||
return token;
|
||||
}
|
||||
|
||||
|
||||
public static ObjectId GetId(this ClaimsPrincipal user)
|
||||
{
|
||||
return user.FindFirstValue(ClaimTypes.NameIdentifier).ToObjectId();
|
||||
}
|
||||
|
||||
public static ObjectId GetUserId(this ServerCallContext context)
|
||||
{
|
||||
return context.GetHttpContext().User.GetId();
|
||||
}
|
||||
}
|
||||
using AobaCore.Models;
|
||||
|
||||
using AobaServer.Models;
|
||||
|
||||
using Grpc.Core;
|
||||
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace AobaServer.Utils;
|
||||
|
||||
public static class Extensions
|
||||
{
|
||||
public static ObjectId ToObjectId(this string? value)
|
||||
{
|
||||
if(value == null)
|
||||
return ObjectId.Empty;
|
||||
if(ObjectId.TryParse(value, out ObjectId result))
|
||||
return result;
|
||||
return ObjectId.Empty;
|
||||
}
|
||||
|
||||
public static string GetToken(this User user, AuthInfo authInfo)
|
||||
{
|
||||
var handler = new JwtSecurityTokenHandler();
|
||||
var signCreds = new SigningCredentials(new SymmetricSecurityKey(authInfo.SecureKey), SecurityAlgorithms.HmacSha256);
|
||||
var identity = user.GetIdentity();
|
||||
var token = handler.CreateEncodedJwt(authInfo.Issuer, authInfo.Audience, identity, notBefore: DateTime.Now, expires: null, issuedAt: DateTime.Now, signCreds);
|
||||
return token;
|
||||
}
|
||||
|
||||
|
||||
public static ObjectId GetId(this ClaimsPrincipal user)
|
||||
{
|
||||
return user.FindFirstValue(ClaimTypes.NameIdentifier).ToObjectId();
|
||||
}
|
||||
|
||||
public static ObjectId GetUserId(this ServerCallContext context)
|
||||
{
|
||||
return context.GetHttpContext().User.GetId();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,68 +1,68 @@
|
||||
using AobaCore.Models;
|
||||
using Aoba.RPC;
|
||||
|
||||
using MongoDB.Bson;
|
||||
using Google.Protobuf.WellKnownTypes;
|
||||
|
||||
namespace AobaServer.Utils;
|
||||
|
||||
public static class ProtoExtensions
|
||||
{
|
||||
public static ListResponse ToResponse(this PagedResult<Media> result)
|
||||
{
|
||||
var res = new ListResponse()
|
||||
{
|
||||
Pagination = result.ToPagination(),
|
||||
};
|
||||
res.Items.AddRange(result.Items.Select(i => i.ToMediaModel()));
|
||||
return res;
|
||||
}
|
||||
|
||||
public static Pagination ToPagination<T>(this PagedResult<T> result)
|
||||
{
|
||||
var p =new Pagination()
|
||||
{
|
||||
Page = result.Page,
|
||||
PageSize = result.PageSize,
|
||||
TotalItems = result.TotalItems,
|
||||
TotalPages = result.TotalPages,
|
||||
};
|
||||
if(result.Query != null)
|
||||
p.Query = result.Query;
|
||||
return p;
|
||||
}
|
||||
|
||||
public static MediaResponse ToResponse(this Media? media)
|
||||
{
|
||||
if(media == null)
|
||||
return new MediaResponse() { Empty = new Empty() };
|
||||
return new MediaResponse()
|
||||
{
|
||||
Value = media.ToMediaModel()
|
||||
};
|
||||
}
|
||||
|
||||
public static MediaModel ToMediaModel(this Media media)
|
||||
{
|
||||
return new MediaModel()
|
||||
{
|
||||
Ext = media.Ext,
|
||||
FileName = media.Filename,
|
||||
Id = media.Id.ToId(),
|
||||
MediaId = media.MediaId.ToId(),
|
||||
MediaType = (Aoba.RPC.MediaType)media.MediaType,
|
||||
Owner = media.Owner.ToId(),
|
||||
ViewCount = media.ViewCount,
|
||||
};
|
||||
}
|
||||
|
||||
public static Id ToId(this ObjectId id)
|
||||
{
|
||||
return new Id() { Value = id.ToString() };
|
||||
}
|
||||
|
||||
public static ObjectId ToObjectId(this Id id)
|
||||
{
|
||||
return id.Value.ToObjectId();
|
||||
}
|
||||
}
|
||||
using AobaCore.Models;
|
||||
using Aoba.RPC;
|
||||
|
||||
using MongoDB.Bson;
|
||||
using Google.Protobuf.WellKnownTypes;
|
||||
|
||||
namespace AobaServer.Utils;
|
||||
|
||||
public static class ProtoExtensions
|
||||
{
|
||||
public static ListResponse ToResponse(this PagedResult<Media> result)
|
||||
{
|
||||
var res = new ListResponse()
|
||||
{
|
||||
Pagination = result.ToPagination(),
|
||||
};
|
||||
res.Items.AddRange(result.Items.Select(i => i.ToMediaModel()));
|
||||
return res;
|
||||
}
|
||||
|
||||
public static Pagination ToPagination<T>(this PagedResult<T> result)
|
||||
{
|
||||
var p =new Pagination()
|
||||
{
|
||||
Page = result.Page,
|
||||
PageSize = result.PageSize,
|
||||
TotalItems = result.TotalItems,
|
||||
TotalPages = result.TotalPages,
|
||||
};
|
||||
if(result.Query != null)
|
||||
p.Query = result.Query;
|
||||
return p;
|
||||
}
|
||||
|
||||
public static MediaResponse ToResponse(this Media? media)
|
||||
{
|
||||
if(media == null)
|
||||
return new MediaResponse() { Empty = new Empty() };
|
||||
return new MediaResponse()
|
||||
{
|
||||
Value = media.ToMediaModel()
|
||||
};
|
||||
}
|
||||
|
||||
public static MediaModel ToMediaModel(this Media media)
|
||||
{
|
||||
return new MediaModel()
|
||||
{
|
||||
Ext = media.Ext,
|
||||
FileName = media.Filename,
|
||||
Id = media.Id.ToId(),
|
||||
MediaId = media.MediaId.ToId(),
|
||||
MediaType = (Aoba.RPC.MediaType)media.MediaType,
|
||||
Owner = media.Owner.ToId(),
|
||||
ViewCount = media.ViewCount,
|
||||
};
|
||||
}
|
||||
|
||||
public static Id ToId(this ObjectId id)
|
||||
{
|
||||
return new Id() { Value = id.ToString() };
|
||||
}
|
||||
|
||||
public static ObjectId ToObjectId(this Id id)
|
||||
{
|
||||
return id.Value.ToObjectId();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user