tesses-cms/Tesses.CMS/AssetProvier.cs

53 lines
1.4 KiB
C#
Raw Permalink Normal View History

2023-12-16 01:40:05 +00:00
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Threading.Tasks;
using Tesses.WebServer;
namespace Tesses.CMS
{
public class AssetProvider : Server
{
static Assembly asm=typeof(AssetProvider).Assembly;
public AssetProvider()
{
}
public static Stream OpenRead(string path)
{
return asm.GetManifestResourceStream($"Tesses.CMS.Assets{path.Replace("/",".")}");
}
public override async Task GetAsync(ServerContext ctx)
{
try{
2024-07-28 22:59:28 +00:00
await ctx.SendStreamAsync(OpenRead(ctx.UrlPath),HeyRed.Mime.MimeTypesMap.GetMimeType(ctx.UrlPath));
2023-12-16 01:40:05 +00:00
}catch(ArgumentNullException ex)
{
_=ex;
await NotFoundServer.ServerNull.GetAsync(ctx);
}
}
public static async Task<string> ReadAllTextAsync(string path)
{
using(var s = OpenRead(path))
using(var ms = new StreamReader(s))
{
return await ms.ReadToEndAsync();
}
}
public static string ReadAllText(string path)
{
using(var s = OpenRead(path))
using(var ms = new StreamReader(s))
return ms.ReadToEnd();
}
}
}