Added proxy for favicone api

This commit is contained in:
Mia Rose Winter 2024-02-03 19:45:46 +01:00
parent ba31eeda04
commit e1412eef52
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E
3 changed files with 34 additions and 1 deletions

View file

@ -1,7 +1,8 @@
@using Wave.Data @using Wave.Data
@using System.Net
<a href="@Link.UrlString" rel="me" target="_blank" title="@Link.Url.Host" @attributes="AdditionalAttributes"> <a href="@Link.UrlString" rel="me" target="_blank" title="@Link.Url.Host" @attributes="AdditionalAttributes">
<img src="@("https://favicone.com/" + Link.Url.Host + "?s=32")" alt="" width="32" height="32" class="w-5 h-5" /> <img src="@("/api/proxy/favicon/" + WebUtility.UrlEncode(Link.Url.Host))" alt="" width="32" height="32" class="w-5 h-5" />
@(Link.Url.Host.LastIndexOf('.') > -1 ? Link.Url.Host[..Link.Url.Host.LastIndexOf('.')] : Link.Url.Host) @(Link.Url.Host.LastIndexOf('.') > -1 ? Link.Url.Host[..Link.Url.Host.LastIndexOf('.')] : Link.Url.Host)
</a> </a>

View file

@ -0,0 +1,31 @@
using Microsoft.AspNetCore.Mvc;
namespace Wave.Controllers;
[ApiController]
[Route("/api/proxy")]
public class ApiProxy(HttpClient client) : ControllerBase {
private HttpClient Client { get; } = client;
[Route("favicon/{host}")]
[Produces("image/x-icon")]
[ResponseCache(Duration = 60*60*24*30, Location = ResponseCacheLocation.Any)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task GetFavicon(string host, [FromQuery] int size = 32) {
var response = await DoProxy("https://favicone.com/" + host + "?s=" + size);
if (!response.IsSuccessStatusCode) {
Response.StatusCode = StatusCodes.Status404NotFound;
return;
}
byte[] data = await response.Content.ReadAsByteArrayAsync();
await Response.BodyWriter.WriteAsync(data);
}
private async Task<HttpResponseMessage> DoProxy(string url) {
return await Client.SendAsync(new HttpRequestMessage(HttpMethod.Get, url));
}
}

View file

@ -101,6 +101,7 @@
options.ResourcesPath = "Resources"; options.ResourcesPath = "Resources";
}); });
builder.Services.AddScoped<ImageService>(); builder.Services.AddScoped<ImageService>();
builder.Services.AddHttpClient();
builder.Services.Configure<Customization>(builder.Configuration.GetSection(nameof(Customization))); builder.Services.Configure<Customization>(builder.Configuration.GetSection(nameof(Customization)));
builder.Services.AddCascadingValue("TitlePrefix", builder.Services.AddCascadingValue("TitlePrefix",