88 lines
3.4 KiB
C#
88 lines
3.4 KiB
C#
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<IResult> 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) : "<em>token</em>";
|
|
}
|
|
} 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("<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, string? userName, 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>");
|
|
w.WriteLine("<div style=\"float:right\">Uživatel: [<a href=\"/login\">" + (userName != null ? userName : "<em>přihlásit</em>") + "</a>]</div>");
|
|
w.WriteLine("<div style=\"font-weight:bold\">Menu: <a href=\"/\">Hledání</a></div>");
|
|
}
|
|
|
|
private static void RenderFooter(TextWriter w)
|
|
{
|
|
w.WriteLine("</body></html>");
|
|
}
|
|
}
|