Initial Commit
This commit is contained in:
39
src/Helpers/RequestHelper.cs
Normal file
39
src/Helpers/RequestHelper.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using Exsersewo.Common.Extensions;
|
||||
using Kynareth.Models;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
|
||||
namespace Kynareth.Helpers;
|
||||
|
||||
public static class RequestHelper
|
||||
{
|
||||
public static string GetUrlHostname(this HttpContext context)
|
||||
{
|
||||
var origin = context.Request.Headers["X-Original-Host"];
|
||||
|
||||
return origin.Count > 0 ? origin[0] : context.Request.Host.Host;
|
||||
}
|
||||
|
||||
public static async Task<bool> IsRequestAuthenticatedAsync(this HttpRequest request)
|
||||
{
|
||||
string key = request.GetToken();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(key)) return false;
|
||||
|
||||
await using var database = request.HttpContext.RequestServices.GetRequiredService<KynarethDbContext>();
|
||||
|
||||
var tokenEntry = database.Tokens.FirstOrDefault(token => token.Token.Equals(key));
|
||||
|
||||
if (tokenEntry is not { IsValid: true }) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static string GetToken(this HttpRequest request)
|
||||
{
|
||||
if (!request.Headers.TryGetValue("Authorization", out StringValues authKey)) return string.Empty;
|
||||
|
||||
if (!authKey.ToArray().AnyStartWith("Bearer", out string key)) return string.Empty;
|
||||
|
||||
return key.Replace("Bearer ", "");
|
||||
}
|
||||
}
|
||||
34
src/Helpers/ResponseHelper.cs
Normal file
34
src/Helpers/ResponseHelper.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Exsersewo.Common.Converters;
|
||||
using Exsersewo.Common.Utilities;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Kynareth.Helpers;
|
||||
|
||||
public static class ResponseHelper
|
||||
{
|
||||
private static readonly JsonSerializerOptions SerializerOptions = new()
|
||||
{
|
||||
NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals,
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
||||
WriteIndented = true,
|
||||
Converters = { new LongToStringConverter() }
|
||||
};
|
||||
|
||||
public static object Send(this HttpContext Context, EventResult result, HttpStatusCode status = HttpStatusCode.OK)
|
||||
{
|
||||
Context.Response.ContentType = "application/json";
|
||||
Context.Response.StatusCode = (int)status;
|
||||
|
||||
return new JsonResult(result, SerializerOptions);
|
||||
}
|
||||
|
||||
public static object SendStream<T>(this HttpContext Context, T stream, string contentType, HttpStatusCode status = HttpStatusCode.OK) where T : Stream
|
||||
=> new StreamResult(stream)
|
||||
{
|
||||
ContentType = contentType,
|
||||
StatusCode = (int)status
|
||||
};
|
||||
}
|
||||
46
src/Helpers/StreamResult.cs
Normal file
46
src/Helpers/StreamResult.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Infrastructure;
|
||||
|
||||
namespace Kynareth.Helpers;
|
||||
|
||||
public class StreamResult : ActionResult, IStatusCodeActionResult, IActionResult
|
||||
{
|
||||
public string ContentType { get; set; }
|
||||
public int? StatusCode { get; set; }
|
||||
public Stream Value { get; set; }
|
||||
|
||||
public StreamResult(Stream data)
|
||||
{
|
||||
Value = data;
|
||||
}
|
||||
|
||||
public override void ExecuteResult(ActionContext context)
|
||||
=> ExecuteResultAsync(context).ConfigureAwait(false);
|
||||
|
||||
public override async Task ExecuteResultAsync(ActionContext context)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
var response = context.HttpContext.Response;
|
||||
|
||||
response.StatusCode = StatusCode ?? 200;
|
||||
|
||||
response.ContentType = !string.IsNullOrEmpty(ContentType)
|
||||
? ContentType
|
||||
: "image/png";
|
||||
|
||||
await Value.CopyToAsync(response.Body);
|
||||
|
||||
await Value.FlushAsync();
|
||||
|
||||
await Value.DisposeAsync();
|
||||
|
||||
|
||||
context.HttpContext.Response.RegisterForDisposeAsync(Value);
|
||||
|
||||
GC.Collect();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user