Files
stream-cinema/StreamCinemaWeb/Layouts/BasicLayout.cs

64 lines
2.6 KiB
C#

using System;
using System.Buffers;
using System.Text;
using System.Web;
namespace StreamCinemaWeb.Layouts;
public abstract class BasicLayout
{
protected abstract string Title { get; }
protected abstract Task RenderContentAsync(HttpRequest req, TextWriter w);
public async Task<IResult> ExecuteAsync(HttpRequest req)
{
int statusCode = 200;
StringBuilder content = new StringBuilder();
TextWriter w = new StringWriter(content);
try
{
RenderHeader(Title, w);
await RenderContentAsync(req, w);
RenderFooter(w);
}
catch (Exception e)
{
content.Clear();
RenderHeader(Title + " - Error " + e.GetType().Name, w);
w.WriteLine("<h1>Error " + e.GetType().Name + "</h1><div><pre>" + HttpUtility.HtmlEncode(e.Message) + "</pre></div>");
RenderFooter(w);
statusCode = 500;
}
return Results.Content(content.ToString(), "text/html; charset=utf-8", null, statusCode);
}
private static void RenderHeader(string title, TextWriter w) {
w.WriteLine("<!doctype html>");
w.WriteLine("<html><head>");
w.WriteLine("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">");
w.WriteLine("<title>" + HttpUtility.HtmlEncode(title) + "</title>");
w.WriteLine("<style>");
w.WriteLine(".search-results { display: flex; flex-wrap: wrap; }");
w.WriteLine(".media-episodes, .media-streams { display: flex; flex-direction: column; gap: 1em; }");
w.WriteLine(".search-results > .media-info { width: 25%; }");
w.WriteLine(".media-episodes > .media-info { height: 12vh; display: flex; align-items: center; }");
w.WriteLine(".media-streams > .media-stream:nth-child(even) { background-color: #CCC; }");
w.WriteLine(".media-streams > .media-stream > a { display: flex; gap: 1em; align-items: center; }");
w.WriteLine(".search-results .media-art { margin: 1em 1em 0em 1em }");
w.WriteLine(".media-episodes .media-art { margin: 1em; width: 12vh; height: 12vh; }");
w.WriteLine(".search-results .media-art img { width: 100%; }");
w.WriteLine(".media-episodes .media-art img { height: 100%; }");
w.WriteLine(".media-title { margin: 0em 1em 0em 1em; font-weight: bold; font-size: larger;}");
w.WriteLine(".search-results .media-plot { margin: 0em 1em 1em 1em; height: 4.5em; overflow-y: hidden; color: #999;}");
w.WriteLine(".media-episodes .media-plot { margin: 1em; overflow-y: hidden; color: #999;}");
w.WriteLine("</style>");
w.WriteLine("</head><body>");
}
private static void RenderFooter(TextWriter w) {
w.WriteLine("</body></html>");
}
}