Files
stream-cinema/CinemaWeb/Pages/LinkPage.cs
Roman Vaníček 0bcb6d571f
All checks were successful
continuous-integration/drone/push Build is passing
Rename to Cinema
2024-11-30 00:50:10 +01:00

81 lines
2.4 KiB
C#

using System;
using System.Web;
using Microsoft.Extensions.Primitives;
using Cinema.Webshare;
using CinemaLib.API;
using CinemaWeb.Layouts;
using LinkGenerator = Cinema.Webshare.LinkGenerator;
namespace CinemaWeb.Pages;
public class LinkPage : BasicLayout
{
private readonly string _provider;
private readonly string _ident;
private readonly string _name;
private readonly string _title;
public LinkPage(string provider, string ident, string name, string title)
{
this._provider = provider;
this._ident = ident;
this._name = name;
this._title = title;
}
protected override string Title => _title;
protected override async Task RenderContentAsync(HttpRequest req, TextWriter w)
{
w.WriteLine("<h1>" + HttpUtility.HtmlEncode(_title) + "</h1>");
Uri link;
switch (_provider)
{
case "webshare": link = await LinkGenerator.GenerateDownloadLinkAsync(_ident, _name, req.HttpContext.RequestAborted); break;
default:
w.WriteLine("<em>Provider '" + HttpUtility.HtmlEncode(_provider) + "' není aktuálně podporován.</em>");
return;
}
w.WriteLine("<div><a href=\"" + link.ToString() + "\">" + HttpUtility.HtmlEncode(link.ToString()) + "</a></div>");
// External subtitles
StringValues subs = req.Query["sub"];
if (subs.Count != 0) {
w.WriteLine("<h2>Externí titulky</h2>");
foreach (string i in subs) {
(string lang, Uri uri) = await ProcessSubtitleLinkAsync(i, req.HttpContext.RequestAborted);
w.WriteLine("<div><a href=\"" + uri.ToString() + "\">" + HttpUtility.HtmlEncode(lang) + "</a></div>");
}
}
}
private static async Task<(string, Uri)> ProcessSubtitleLinkAsync(string link, CancellationToken cancel) {
if (link == null)
throw new ArgumentNullException();
// Format: <lang>:<provider>:<id>
int a = link.IndexOf(':');
int b = link.IndexOf(':', a + 1);
if (a < 0 || b < 0)
throw new FormatException();
string lang = link.Substring(0, a);
string provider = link.Substring(a + 1, b - a - 1);
string rawId = link.Substring(b + 1);
Uri result;
switch (provider) {
case "webshare":
result = await LinkGenerator.GenerateDownloadLinkAsync(rawId, "", cancel);
break;
case "http":
result = new Uri(rawId);
break;
default:
throw new NotSupportedException();
}
return (lang, result);
}
}