@using EasyCaching.Core
@using System.Net.Http
@using AngleSharp
@using AngleSharp.Html.Parser
@inject IHybridCachingProvider EasyCachingProvider
@inject IHttpClientFactory HttpClientFactory
@{
Layout = null;
const string membleKey = "memble-version:1.0.1";
string content;
var cache = await EasyCachingProvider.GetAsync<string>(membleKey);
if (!cache.IsNull && cache.HasValue)
{
content = cache.Value;
}
else
{
content = await GetMembleContent();
await EasyCachingProvider.SetAsync(membleKey, content,
TimeSpan.FromDays(30));
}
async Task<string> GetMembleContent()
{
var httpClient = HttpClientFactory.CreateClient("edge");
var request = new HttpRequestMessage(HttpMethod.Get,
$"{Program.CdnBaseAddress}/../memble/index.html");
var response = await httpClient.SendAsync(request);
var htmlContent = await response.Content.ReadAsStringAsync();
var htmlParse = new HtmlParser();
var document = await htmlParse.ParseDocumentAsync(htmlContent);
foreach (var item in document.Scripts)
{
if (!string.IsNullOrEmpty(item.Source))
{
item.Source = $"{Program.CdnBaseAddress}/../memble{item.Source}";
}
}
document.GetElementsByTagName("link").ForEach(x =>
{
var src = x.GetAttribute("href");
if (!string.IsNullOrEmpty(src))
{
x.SetAttribute("href", $"{Program.CdnBaseAddress}/../memble{src}");
}
});
var membleContent = document.ToHtml();
return membleContent;
}
}
@Html.Raw(content)