2022-06-10 21:49:01 +08:00
|
|
|
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;
|
2022-08-30 20:59:49 +08:00
|
|
|
using NUglify;
|
2022-06-10 21:49:01 +08:00
|
|
|
|
|
|
|
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<IFileInfo> contents)
|
|
|
|
{
|
|
|
|
var options = new TemplateOptions();
|
|
|
|
options.MemberAccessStrategy.MemberNameStrategy = MemberNameStrategies.CamelCase;
|
|
|
|
options.MemberAccessStrategy.Register<IFileInfo>();
|
|
|
|
|
|
|
|
var tc = new TemplateContext(options);
|
|
|
|
tc.SetValue("path", (context.Request.PathBase + context.Request.Path).Value);
|
|
|
|
tc.SetValue("files", contents);
|
|
|
|
|
|
|
|
var result = template.Render(tc);
|
2022-08-30 20:59:49 +08:00
|
|
|
|
|
|
|
result = Uglify.Html(result).Code;
|
|
|
|
|
2022-06-10 21:49:01 +08:00
|
|
|
var bytes = Encoding.UTF8.GetBytes(result);
|
|
|
|
context.Response.ContentLength = bytes.Length;
|
|
|
|
return context.Response.Body.WriteAsync(bytes, 0, bytes.Length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|