1
0
mirror of https://github.com/bitwarden/server synced 2025-12-20 10:13:39 +00:00
Files
server/src/Icons/Controllers/IconsController.cs
2017-10-09 14:25:59 -04:00

72 lines
2.4 KiB
C#

using System;
using System.Net.Http;
using System.Threading.Tasks;
using Bit.Icons.Models;
using Bit.Icons.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
namespace Bit.Icons.Controllers
{
[Route("")]
public class IconsController : Controller
{
private static readonly HttpClient _httpClient = new HttpClient();
private readonly IMemoryCache _memoryCache;
private readonly IDomainMappingService _domainMappingService;
private readonly IconsSettings _iconsSettings;
public IconsController(
IMemoryCache memoryCache,
IDomainMappingService domainMappingService,
IOptions<IconsSettings> iconsSettingsOptions)
{
_memoryCache = memoryCache;
_domainMappingService = domainMappingService;
_iconsSettings = iconsSettingsOptions.Value;
}
[HttpGet("")]
public async Task<IActionResult> Get([FromQuery]string domain, [FromQuery]string size = "16..24..200")
{
if(!domain.StartsWith("http://") || !domain.StartsWith("https://"))
{
domain = "http://" + domain;
}
if(!Uri.TryCreate(domain, UriKind.Absolute, out Uri uri))
{
return new BadRequestResult();
}
var mappedDomain = _domainMappingService.MapDomain(uri.Host);
var cacheKey = $"{mappedDomain}_{size}";
var icon = await _memoryCache.GetOrCreateAsync(cacheKey, async entry =>
{
entry.AbsoluteExpiration = DateTime.UtcNow.AddHours(_iconsSettings.CacheHours);
var iconUrl = $"{_iconsSettings.BestIconBaseUrl}/icon?url={mappedDomain}&size={size}";
var response = await _httpClient.GetAsync(iconUrl);
if(!response.IsSuccessStatusCode)
{
return null;
}
return new Icon
{
Image = await response.Content.ReadAsByteArrayAsync(),
Format = response.Content.Headers.ContentType.MediaType
};
});
if(icon == null)
{
return new NotFoundResult();
}
return new FileContentResult(icon.Image, icon.Format);
}
}
}