using System.Collections.Generic; using System.IO; using System.Text; using System.Threading.Tasks; using Fluid; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.StaticFiles; using Microsoft.Extensions.FileProviders; namespace BililiveRecorder.Web { public class BililiveRecorderDirectoryFormatter : IDirectoryFormatter { private static readonly IFluidTemplate template; static BililiveRecorderDirectoryFormatter() { var fileProvider = new ManifestEmbeddedFileProvider(typeof(BililiveRecorderDirectoryFormatter).Assembly); using var file = fileProvider.GetFileInfo(".file_template.html").CreateReadStream(); using var reader = new StreamReader(file); template = new FluidParser().Parse(reader.ReadToEnd()); } public BililiveRecorderDirectoryFormatter() { } public Task GenerateContentAsync(HttpContext context, IEnumerable contents) { var options = new TemplateOptions(); options.MemberAccessStrategy.MemberNameStrategy = MemberNameStrategies.CamelCase; options.MemberAccessStrategy.Register(); var tc = new TemplateContext(options); tc.SetValue("path", (context.Request.PathBase + context.Request.Path).Value); tc.SetValue("files", contents); var result = template.Render(tc); var bytes = Encoding.UTF8.GetBytes(result); context.Response.ContentLength = bytes.Length; return context.Response.Body.WriteAsync(bytes, 0, bytes.Length); } } }