2017-10-08 22:23:17 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2017-10-09 13:35:07 -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
|
2017-10-08 22:23:17 +02:00
|
|
|
|
{
|
2017-10-09 13:35:07 -04:00
|
|
|
|
[Route("")]
|
2017-10-09 14:02:57 -04:00
|
|
|
|
public class IconsController : Controller
|
2017-10-08 22:23:17 +02:00
|
|
|
|
{
|
2017-10-09 14:21:20 -04:00
|
|
|
|
private static readonly HttpClient _httpClient = new HttpClient();
|
2017-10-09 13:35:07 -04:00
|
|
|
|
private readonly IMemoryCache _memoryCache;
|
2017-10-09 14:21:20 -04:00
|
|
|
|
private readonly IDomainMappingService _domainMappingService;
|
2017-10-09 14:02:57 -04:00
|
|
|
|
private readonly IconsSettings _iconsSettings;
|
2017-10-08 22:23:17 +02:00
|
|
|
|
|
2017-10-09 14:02:57 -04:00
|
|
|
|
public IconsController(
|
|
|
|
|
|
IMemoryCache memoryCache,
|
2017-10-09 14:21:20 -04:00
|
|
|
|
IDomainMappingService domainMappingService,
|
2017-10-09 14:54:32 -04:00
|
|
|
|
IconsSettings iconsSettings)
|
2017-10-08 22:23:17 +02:00
|
|
|
|
{
|
2017-10-09 13:35:07 -04:00
|
|
|
|
_memoryCache = memoryCache;
|
2017-10-09 14:21:20 -04:00
|
|
|
|
_domainMappingService = domainMappingService;
|
2017-10-09 14:54:32 -04:00
|
|
|
|
_iconsSettings = iconsSettings;
|
2017-10-08 22:23:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-12 10:00:44 -04:00
|
|
|
|
[HttpGet("{hostname}/icon.png")]
|
|
|
|
|
|
[ResponseCache(Duration = 86400 /*24 hours*/)]
|
|
|
|
|
|
public async Task<IActionResult> Get(string hostname)
|
2017-10-08 22:23:17 +02:00
|
|
|
|
{
|
2017-10-12 10:00:44 -04:00
|
|
|
|
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}";
|
2017-10-09 16:21:37 -04:00
|
|
|
|
if(!Uri.TryCreate(url, UriKind.Absolute, out Uri uri))
|
2017-10-09 13:35:07 -04:00
|
|
|
|
{
|
|
|
|
|
|
return new BadRequestResult();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-09 14:21:20 -04:00
|
|
|
|
var mappedDomain = _domainMappingService.MapDomain(uri.Host);
|
2017-10-12 10:41:13 -04:00
|
|
|
|
if(!_memoryCache.TryGetValue(mappedDomain, out Icon icon))
|
2017-10-08 22:23:17 +02:00
|
|
|
|
{
|
2017-10-12 23:56:34 -04:00
|
|
|
|
var iconUrl = $"{_iconsSettings.BestIconBaseUrl}/icon?url={mappedDomain}&size=16..24..32" +
|
2017-10-20 22:04:10 -04:00
|
|
|
|
$"&fallback_icon_url=https://raw.githubusercontent.com/bitwarden/mobile/master/src/Android/Resources/" +
|
|
|
|
|
|
"drawable-xxxhdpi/login.png";
|
2017-10-09 14:21:20 -04:00
|
|
|
|
var response = await _httpClient.GetAsync(iconUrl);
|
2017-10-09 13:35:07 -04:00
|
|
|
|
if(!response.IsSuccessStatusCode)
|
2017-10-09 18:58:59 +02:00
|
|
|
|
{
|
2017-10-12 10:41:13 -04:00
|
|
|
|
return new NotFoundResult();
|
2017-10-09 18:58:59 +02:00
|
|
|
|
}
|
2017-10-08 22:23:17 +02:00
|
|
|
|
|
2017-10-12 10:41:13 -04:00
|
|
|
|
var image = await response.Content.ReadAsByteArrayAsync();
|
|
|
|
|
|
icon = new Icon
|
2017-10-09 13:35:07 -04:00
|
|
|
|
{
|
2017-10-12 10:41:13 -04:00
|
|
|
|
Image = image,
|
2017-10-09 13:35:07 -04:00
|
|
|
|
Format = response.Content.Headers.ContentType.MediaType
|
|
|
|
|
|
};
|
2017-10-08 22:23:17 +02:00
|
|
|
|
|
2017-10-12 10:41:13 -04:00
|
|
|
|
// Only cache smaller images (<= 50kb)
|
|
|
|
|
|
if(image.Length <= 50012)
|
|
|
|
|
|
{
|
|
|
|
|
|
_memoryCache.Set(mappedDomain, icon, new MemoryCacheEntryOptions
|
|
|
|
|
|
{
|
|
|
|
|
|
AbsoluteExpirationRelativeToNow = new TimeSpan(_iconsSettings.CacheHours, 0, 0)
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2017-10-08 22:23:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-09 18:58:59 +02:00
|
|
|
|
return new FileContentResult(icon.Image, icon.Format);
|
2017-10-08 22:23:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|