Files
server/src/Icons/Controllers/IconsController.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

110 lines
4.2 KiB
C#
Raw Normal View History

using System;
using System.Threading.Tasks;
2017-10-09 13:35:07 -04:00
using Bit.Icons.Models;
using Bit.Icons.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
2018-08-25 16:41:26 -04:00
using Microsoft.Extensions.Logging;
2017-10-09 13:35:07 -04:00
namespace Bit.Icons.Controllers
{
2017-10-09 13:35:07 -04:00
[Route("")]
2017-10-09 14:02:57 -04:00
public class IconsController : Controller
{
// Basic fa-globe icon
private static readonly byte[] _notFoundImage = Convert.FromBase64String("iVBORw0KGgoAAAANSUhE" +
"UgAAABEAAAARCAQAAACRZI9xAAABH0lEQVQoz2WQv0sDQRCF351C4g9QkXCgIkJAEREExUpJbyFoK6nSRLT/GgtLOxE" +
"sgoX/QLAL2Alin0YkICJicQhHCrEIEsJYZLnsxVfNzH779s1KToTsUqNFzAOnRBoWyyS0+aSO0cEwrhnxgUOMMjOMk2" +
"eVOwzDeCE/cDDWXD2B0aFAnR7GPUE/Q8KJ5zjHDYHEFr8YB5IoYbymlpIIJaap0sVICMQthpEbil9xeYxIxBjGQgY4S" +
"wFjW27FD6bccUCBbw8piWdXNliRuKRLzQOMdXGeNj+E7FDOAMakKHptk30iHr3JU//1RuZWiysWWXKRi30kT5KBviSJ" +
"MWKOB0vO8uYhVUJKtCH7VaNcpEhMj3c29F/k2OSICnvM+/M/XGfnuYOrfEAAAAAASUVORK5CYII=");
2017-10-09 13:35:07 -04:00
private readonly IMemoryCache _memoryCache;
private readonly IDomainMappingService _domainMappingService;
private readonly IIconFetchingService _iconFetchingService;
2018-08-25 16:41:26 -04:00
private readonly ILogger<IconsController> _logger;
2017-10-09 14:02:57 -04:00
private readonly IconsSettings _iconsSettings;
2017-10-09 14:02:57 -04:00
public IconsController(
IMemoryCache memoryCache,
IDomainMappingService domainMappingService,
IIconFetchingService iconFetchingService,
2018-08-25 16:41:26 -04:00
ILogger<IconsController> logger,
2017-10-09 14:54:32 -04:00
IconsSettings iconsSettings)
{
2017-10-09 13:35:07 -04:00
_memoryCache = memoryCache;
_domainMappingService = domainMappingService;
_iconFetchingService = iconFetchingService;
2018-08-25 16:41:26 -04:00
_logger = logger;
2017-10-09 14:54:32 -04:00
_iconsSettings = iconsSettings;
}
[HttpGet("~/config")]
public IActionResult GetConfig()
{
return new JsonResult(new
{
CacheEnabled = _iconsSettings.CacheEnabled,
CacheHours = _iconsSettings.CacheHours,
CacheSizeLimit = _iconsSettings.CacheSizeLimit
});
}
2017-10-12 10:00:44 -04:00
[HttpGet("{hostname}/icon.png")]
public async Task<IActionResult> Get(string hostname)
{
if (string.IsNullOrWhiteSpace(hostname) || !hostname.Contains("."))
2017-10-09 14:34:44 -04:00
{
return new BadRequestResult();
}
2017-10-12 10:00:44 -04:00
var url = $"http://{hostname}";
if (!Uri.TryCreate(url, UriKind.Absolute, out var uri))
2017-10-09 13:35:07 -04:00
{
return new BadRequestResult();
}
2018-05-25 12:50:21 -04:00
var domain = uri.Host;
2018-07-30 21:22:50 -04:00
// Convert sub.domain.com => domain.com
//if(DomainName.TryParseBaseDomain(domain, out var baseDomain))
//{
// domain = baseDomain;
//}
2018-05-25 12:50:21 -04:00
var mappedDomain = _domainMappingService.MapDomain(domain);
if (!_iconsSettings.CacheEnabled || !_memoryCache.TryGetValue(mappedDomain, out Icon icon))
{
2018-05-25 12:50:21 -04:00
var result = await _iconFetchingService.GetIconAsync(domain);
if (result == null)
{
2018-08-25 16:59:54 -04:00
_logger.LogWarning("Null result returned for {0}.", domain);
2018-05-24 17:18:33 -04:00
icon = null;
}
else
{
icon = result.Icon;
}
2018-05-24 17:18:33 -04:00
// Only cache not found and smaller images (<= 50kb)
if (_iconsSettings.CacheEnabled && (icon == null || icon.Image.Length <= 50012))
2017-10-12 10:41:13 -04:00
{
2018-08-25 17:30:45 -04:00
_logger.LogInformation("Cache icon for {0}.", domain);
2017-10-12 10:41:13 -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
});
}
}
if (icon == null)
2018-05-24 17:18:33 -04:00
{
return new FileContentResult(_notFoundImage, "image/png");
2018-05-24 17:18:33 -04:00
}
return new FileContentResult(icon.Image, icon.Format);
}
}
}