| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.IO; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using MediaBrowser.Model.IO; |
| | | 7 | | using Microsoft.Extensions.Logging; |
| | | 8 | | using Morestachio; |
| | | 9 | | using Morestachio.Framework.IO.SingleStream; |
| | | 10 | | using Morestachio.Rendering; |
| | | 11 | | |
| | | 12 | | namespace Jellyfin.Server.ServerSetupApp; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Compiles and renders the startup UI Morestachio template. |
| | | 16 | | /// Shared by the live <see cref="SetupServer"/> and the standalone startup UI preview tool so both |
| | | 17 | | /// exercise the exact same template and formatters. |
| | | 18 | | /// </summary> |
| | | 19 | | public sealed class StartupUiRenderer |
| | | 20 | | { |
| | | 21 | | private readonly IRenderer _renderer; |
| | | 22 | | |
| | | 23 | | private StartupUiRenderer(IRenderer renderer) |
| | | 24 | | { |
| | 0 | 25 | | _renderer = renderer; |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Compiles the startup UI template located at <paramref name="templatePath"/>. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="templatePath">The full path to the <c>index.mstemplate.html</c> template.</param> |
| | | 32 | | /// <returns>A ready to use <see cref="StartupUiRenderer"/>.</returns> |
| | | 33 | | public static async Task<StartupUiRenderer> CreateAsync(string templatePath) |
| | | 34 | | { |
| | 0 | 35 | | var fileTemplate = await File.ReadAllTextAsync(templatePath).ConfigureAwait(false); |
| | 0 | 36 | | var renderer = (await ParserOptionsBuilder.New() |
| | 0 | 37 | | .WithTemplate(fileTemplate) |
| | 0 | 38 | | .WithFormatter( |
| | 0 | 39 | | (Version version, int arg) => |
| | 0 | 40 | | { |
| | 0 | 41 | | // version type does not for some stupid reason implement IFormattable which morestachio relies on for T |
| | 0 | 42 | | return version.ToString(arg); |
| | 0 | 43 | | }, |
| | 0 | 44 | | "ToString") |
| | 0 | 45 | | .WithFormatter( |
| | 0 | 46 | | (StartupLogTopic logEntry, IEnumerable<StartupLogTopic> children) => |
| | 0 | 47 | | { |
| | 0 | 48 | | if (children.Any()) |
| | 0 | 49 | | { |
| | 0 | 50 | | var maxLevel = logEntry.LogLevel; |
| | 0 | 51 | | var stack = new Stack<StartupLogTopic>(children); |
| | 0 | 52 | | |
| | 0 | 53 | | while (maxLevel != LogLevel.Error && stack.Count > 0 && (logEntry = stack.Pop()) is not null) // |
| | 0 | 54 | | { |
| | 0 | 55 | | maxLevel = maxLevel < logEntry.LogLevel ? logEntry.LogLevel : maxLevel; |
| | 0 | 56 | | foreach (var child in logEntry.Children) |
| | 0 | 57 | | { |
| | 0 | 58 | | stack.Push(child); |
| | 0 | 59 | | } |
| | 0 | 60 | | } |
| | 0 | 61 | | |
| | 0 | 62 | | return maxLevel; |
| | 0 | 63 | | } |
| | 0 | 64 | | |
| | 0 | 65 | | return logEntry.LogLevel; |
| | 0 | 66 | | }, |
| | 0 | 67 | | "FormatLogLevel") |
| | 0 | 68 | | .WithFormatter( |
| | 0 | 69 | | (LogLevel logLevel) => |
| | 0 | 70 | | { |
| | 0 | 71 | | switch (logLevel) |
| | 0 | 72 | | { |
| | 0 | 73 | | case LogLevel.Trace: |
| | 0 | 74 | | case LogLevel.Debug: |
| | 0 | 75 | | case LogLevel.None: |
| | 0 | 76 | | return "success"; |
| | 0 | 77 | | case LogLevel.Information: |
| | 0 | 78 | | return "info"; |
| | 0 | 79 | | case LogLevel.Warning: |
| | 0 | 80 | | return "warn"; |
| | 0 | 81 | | case LogLevel.Error: |
| | 0 | 82 | | return "danger"; |
| | 0 | 83 | | case LogLevel.Critical: |
| | 0 | 84 | | return "danger-strong"; |
| | 0 | 85 | | } |
| | 0 | 86 | | |
| | 0 | 87 | | return string.Empty; |
| | 0 | 88 | | }, |
| | 0 | 89 | | "ToString") |
| | 0 | 90 | | .BuildAndParseAsync() |
| | 0 | 91 | | .ConfigureAwait(false)) |
| | 0 | 92 | | .CreateCompiledRenderer(); |
| | | 93 | | |
| | 0 | 94 | | return new StartupUiRenderer(renderer); |
| | 0 | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Renders the template with the provided model into the target stream. |
| | | 99 | | /// </summary> |
| | | 100 | | /// <param name="model">The values made available to the template.</param> |
| | | 101 | | /// <param name="output">The stream the rendered HTML is written to.</param> |
| | | 102 | | /// <returns>A Task.</returns> |
| | | 103 | | public Task RenderAsync(IDictionary<string, object> model, Stream output) |
| | | 104 | | { |
| | 0 | 105 | | return _renderer.RenderAsync( |
| | 0 | 106 | | model, |
| | 0 | 107 | | new ByteCounterStream(output, IODefaults.FileStreamBufferSize, true, _renderer.ParserOptions)); |
| | | 108 | | } |
| | | 109 | | } |