2022-06-29 19:46:41 -04:00
|
|
|
|
using Bit.Icons.Models;
|
2017-10-09 14:21:20 -04:00
|
|
|
|
using Bit.Icons.Services;
|
2017-10-08 22:23:17 +02:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2017-10-09 18:58:59 +02:00
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
2017-10-08 22:23:17 +02:00
|
|
|
|
|
2017-10-09 13:35:07 -04:00
|
|
|
|
namespace Bit.Icons.Controllers;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2017-10-09 13:35:07 -04:00
|
|
|
|
[Route("")]
|
|
|
|
|
|
public class IconsController : Controller
|
2017-10-08 22:23:17 +02:00
|
|
|
|
{
|
2022-02-01 23:09:24 -06:00
|
|
|
|
// Basic bwi-globe icon
|
2017-10-09 13:35:07 -04:00
|
|
|
|
private static readonly byte[] _notFoundImage = Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUg" +
|
|
|
|
|
|
"AAABMAAAATCAQAAADYWf5HAAABu0lEQVR42nXSvWuTURTH8R+t0heI9Y04aJycdBLNJNrBFBU7OFgUER3q21I0bXK+JwZ" +
|
2022-02-01 23:09:24 -06:00
|
|
|
|
"pXISm/QdcRB3EgqBBsNihsUbbgODQQSKCuKSDOApJuuhj8tCYQj/jvYfD795z1MZ+nBKrNKhSwrMxbZTrtRnqlEjZkB/x" +
|
2017-10-09 14:02:57 -04:00
|
|
|
|
"C/xmhZrlc71qS0Up8yVzTCGucFNKD1JhORVd70SZNU4okNx5d4+U2UXRIpJFWLClsR79YzN88wQvLWNzzPKEeS/wkQGpW" +
|
2018-05-22 12:49:34 -04:00
|
|
|
|
"VhhqhW8TtDJD3Mm1x/23zLSrZCdpBY8BueTNjHSbc+8wC9HlHgU5Aj5AW5zPdcVdpq0UcknWBSr/pjixO4gfp899Kd23p" +
|
|
|
|
|
|
"M2qQCH7LkCnqAqGh73OK/8NPOcaibr90LrW/yWAnaUhqjaOSl9nFR2r5rsqo22ypn1B5IN8VOUMHVgOnNQIX+d62plcz6" +
|
|
|
|
|
|
"rg1/jskK8CMb4we4pG6OWHtR/LBJkC2E4a7ZPkuX5ntumAOM2xxveclEhLvGH6XCmLPs735Eetrw63NnOgr9P9q1viC3x" +
|
2017-10-09 14:02:57 -04:00
|
|
|
|
"lRUGOjImqFDuOBvrYYoaZU9z1uPpYae5NfdvbNVG2ZjDIlXq/oMi46lo++4vjjPBl2Dlg00AAAAASUVORK5CYII=");
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2018-05-22 12:49:34 -04:00
|
|
|
|
private readonly IMemoryCache _memoryCache;
|
|
|
|
|
|
private readonly IDomainMappingService _domainMappingService;
|
|
|
|
|
|
private readonly IIconFetchingService _iconFetchingService;
|
2017-10-09 14:02:57 -04:00
|
|
|
|
private readonly ILogger<IconsController> _logger;
|
|
|
|
|
|
private readonly IconsSettings _iconsSettings;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2017-10-09 14:02:57 -04:00
|
|
|
|
public IconsController(
|
2017-10-09 14:54:32 -04:00
|
|
|
|
IMemoryCache memoryCache,
|
|
|
|
|
|
IDomainMappingService domainMappingService,
|
|
|
|
|
|
IIconFetchingService iconFetchingService,
|
|
|
|
|
|
ILogger<IconsController> logger,
|
|
|
|
|
|
IconsSettings iconsSettings)
|
2017-10-08 22:23:17 +02:00
|
|
|
|
{
|
2022-02-01 23:09:24 -06:00
|
|
|
|
_memoryCache = memoryCache;
|
|
|
|
|
|
_domainMappingService = domainMappingService;
|
2018-05-22 12:49:34 -04:00
|
|
|
|
_iconFetchingService = iconFetchingService;
|
2022-02-01 23:09:24 -06:00
|
|
|
|
_logger = logger;
|
|
|
|
|
|
_iconsSettings = iconsSettings;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2021-02-18 12:18:50 -05:00
|
|
|
|
|
2018-05-22 12:49:34 -04:00
|
|
|
|
[HttpGet("~/config")]
|
|
|
|
|
|
public IActionResult GetConfig()
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2018-05-22 12:49:34 -04:00
|
|
|
|
return new JsonResult(new
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2018-05-22 12:49:34 -04:00
|
|
|
|
CacheEnabled = _iconsSettings.CacheEnabled,
|
2017-10-09 14:02:57 -04:00
|
|
|
|
CacheHours = _iconsSettings.CacheHours,
|
|
|
|
|
|
CacheSizeLimit = _iconsSettings.CacheSizeLimit
|
|
|
|
|
|
});
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2017-10-08 22:23:17 +02:00
|
|
|
|
|
2017-10-09 14:02:57 -04:00
|
|
|
|
[HttpGet("{hostname}/icon.png")]
|
|
|
|
|
|
public async Task<IActionResult> Get(string hostname)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2017-10-09 14:54:32 -04:00
|
|
|
|
if (string.IsNullOrWhiteSpace(hostname) || !hostname.Contains("."))
|
2017-10-08 22:23:17 +02:00
|
|
|
|
{
|
2018-05-22 12:49:34 -04:00
|
|
|
|
return new BadRequestResult();
|
2017-10-08 22:23:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-02-18 15:15:08 -05:00
|
|
|
|
var url = $"http://{hostname}";
|
|
|
|
|
|
if (!Uri.TryCreate(url, UriKind.Absolute, out var uri))
|
|
|
|
|
|
{
|
|
|
|
|
|
return new BadRequestResult();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-12 10:00:44 -04:00
|
|
|
|
var domain = uri.Host;
|
|
|
|
|
|
// Convert sub.domain.com => domain.com
|
|
|
|
|
|
//if(DomainName.TryParseBaseDomain(domain, out var baseDomain))
|
2022-08-29 16:06:55 -04:00
|
|
|
|
//{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
// domain = baseDomain;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
//}
|
|
|
|
|
|
|
2017-10-12 10:00:44 -04:00
|
|
|
|
var mappedDomain = _domainMappingService.MapDomain(domain);
|
|
|
|
|
|
if (!_iconsSettings.CacheEnabled || !_memoryCache.TryGetValue(mappedDomain, out Icon icon))
|
2017-10-08 22:23:17 +02:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
var result = await _iconFetchingService.GetIconAsync(domain);
|
|
|
|
|
|
if (result == null)
|
2017-10-09 14:34:44 -04:00
|
|
|
|
{
|
|
|
|
|
|
_logger.LogWarning("Null result returned for {0}.", domain);
|
|
|
|
|
|
icon = null;
|
|
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
else
|
2017-10-09 13:35:07 -04:00
|
|
|
|
{
|
2023-08-08 15:29:40 -04:00
|
|
|
|
icon = result;
|
2017-10-09 13:35:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-07-30 21:22:50 -04:00
|
|
|
|
// Only cache not found and smaller images (<= 50kb)
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (_iconsSettings.CacheEnabled && (icon == null || icon.Image.Length <= 50012))
|
2017-10-08 22:23:17 +02:00
|
|
|
|
{
|
2018-08-25 16:59:54 -04:00
|
|
|
|
_logger.LogInformation("Cache icon for {0}.", domain);
|
2018-05-24 17:18:33 -04:00
|
|
|
|
_memoryCache.Set(mappedDomain, icon, new MemoryCacheEntryOptions
|
|
|
|
|
|
{
|
2018-05-29 22:36:32 -04:00
|
|
|
|
AbsoluteExpirationRelativeToNow = new TimeSpan(_iconsSettings.CacheHours, 0, 0),
|
2018-05-29 22:38:29 -04:00
|
|
|
|
Size = icon?.Image.Length ?? 0,
|
|
|
|
|
|
Priority = icon == null ? CacheItemPriority.High : CacheItemPriority.Normal
|
2017-10-12 10:41:13 -04:00
|
|
|
|
});
|
2017-10-08 22:23:17 +02:00
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2022-08-29 14:53:16 -04:00
|
|
|
|
|
2021-02-18 12:18:50 -05:00
|
|
|
|
if (icon == null)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2021-02-18 12:18:50 -05:00
|
|
|
|
return new FileContentResult(_notFoundImage, "image/png");
|
2022-08-29 15:53:48 -04:00
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2017-10-09 18:58:59 +02:00
|
|
|
|
return new FileContentResult(icon.Image, icon.Format);
|
2017-10-08 22:23:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|