mirror of
https://github.com/BililiveRecorder/BililiveRecorder.git
synced 2024-12-26 12:15:42 +08:00
46 lines
1.6 KiB
C#
46 lines
1.6 KiB
C#
|
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<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);
|
||
|
var bytes = Encoding.UTF8.GetBytes(result);
|
||
|
context.Response.ContentLength = bytes.Length;
|
||
|
return context.Response.Body.WriteAsync(bytes, 0, bytes.Length);
|
||
|
}
|
||
|
}
|
||
|
}
|