1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-08 06:28:14 -05:00
bitwarden/src/Icons/Controllers/IconsController.cs
Kyle Spearrin 309c7f81ba formats
2017-10-09 14:34:44 -04:00

80 lines
2.6 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",
[FromQuery]string formats = null)
{
if(string.IsNullOrWhiteSpace(domain))
{
return new BadRequestResult();
}
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}_{formats}";
var icon = await _memoryCache.GetOrCreateAsync(cacheKey, async entry =>
{
entry.AbsoluteExpiration = DateTime.UtcNow.AddHours(_iconsSettings.CacheHours);
var iconUrl = $"{_iconsSettings.BestIconBaseUrl}/icon?url={mappedDomain}&size={size}&formats={formats}";
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);
}
}
}