tesses-cms/Tesses.CMS/AssetProvier.cs

53 lines
1.4 KiB
C#

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{
await ctx.SendStreamAsync(OpenRead(ctx.UrlPath),HeyRed.Mime.MimeTypesMap.GetMimeType(ctx.UrlPath));
}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();
}
}
}