using System; using System.Buffers; using System.Text; using System.Web; namespace CinemaWeb.Layouts; public abstract class BasicLayout { protected abstract string Title { get; } protected abstract Task RenderContentAsync(HttpRequest req, TextWriter w, CancellationToken cancel); public async Task ExecuteAsync(HttpRequest req) { int statusCode = 200; CancellationToken cancel = req.HttpContext.RequestAborted; StringBuilder content = new StringBuilder(); TextWriter w = new StringWriter(content); try { DateTime now = DateTime.UtcNow; var webshare = Program._webshare; string? userName; if (webshare != null) { string? token = await webshare.GetTokenAsync(cancel); if (token == null) { // Automatic logout Program._webshare = null; userName = null; } else { userName = webshare.UserName.Length != 0 ? HttpUtility.HtmlEncode(webshare.UserName) : "token"; } } else userName = null; RenderHeader(Title, userName, w); await RenderContentAsync(req, w, cancel); RenderFooter(w); } catch (Exception e) { content.Clear(); RenderHeader(Title + " - Error " + e.GetType().Name, null, w); w.WriteLine("

Error " + e.GetType().Name + "

" + HttpUtility.HtmlEncode(e.Message) + "
"); RenderFooter(w); statusCode = 500; } return Results.Content(content.ToString(), "text/html; charset=utf-8", null, statusCode); } private static void RenderHeader(string title, string? userName, TextWriter w) { w.WriteLine(""); w.WriteLine(""); w.WriteLine(""); w.WriteLine("" + HttpUtility.HtmlEncode(title) + ""); w.WriteLine(""); w.WriteLine(""); w.WriteLine("
Uživatel: [" + (userName != null ? userName : "přihlásit") + "]
"); w.WriteLine("
Menu: Hledání
"); } private static void RenderFooter(TextWriter w) { w.WriteLine(""); } }