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-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-08 22:23:17 +02:00
|
|
|
|
public class IconController : Controller
|
|
|
|
|
|
{
|
2017-10-09 13:35:07 -04:00
|
|
|
|
private readonly IMemoryCache _memoryCache;
|
2017-10-08 22:23:17 +02:00
|
|
|
|
|
2017-10-09 18:58:59 +02:00
|
|
|
|
public IconController(IMemoryCache memoryCache)
|
2017-10-08 22:23:17 +02:00
|
|
|
|
{
|
2017-10-09 13:35:07 -04:00
|
|
|
|
_memoryCache = memoryCache;
|
2017-10-08 22:23:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-09 13:35:07 -04:00
|
|
|
|
[HttpGet("")]
|
2017-10-09 18:58:59 +02:00
|
|
|
|
public async Task<IActionResult> Get([FromQuery] string domain)
|
2017-10-08 22:23:17 +02:00
|
|
|
|
{
|
2017-10-09 13:35:07 -04:00
|
|
|
|
if(!domain.StartsWith("http://") || !domain.StartsWith("https://"))
|
|
|
|
|
|
{
|
|
|
|
|
|
domain = "http://" + domain;
|
|
|
|
|
|
}
|
2017-10-08 22:23:17 +02:00
|
|
|
|
|
2017-10-09 13:35:07 -04:00
|
|
|
|
if(!Uri.TryCreate(domain, UriKind.Absolute, out Uri uri))
|
|
|
|
|
|
{
|
|
|
|
|
|
return new BadRequestResult();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var iconUrl = BuildIconUrl(uri);
|
|
|
|
|
|
var icon = await _memoryCache.GetOrCreateAsync(domain, async entry =>
|
2017-10-08 22:23:17 +02:00
|
|
|
|
{
|
2017-10-09 18:58:59 +02:00
|
|
|
|
entry.AbsoluteExpiration = DateTime.Now.AddDays(1);
|
2017-10-08 22:23:17 +02:00
|
|
|
|
|
2017-10-09 18:58:59 +02:00
|
|
|
|
var httpClient = new HttpClient();
|
2017-10-09 13:35:07 -04:00
|
|
|
|
var response = await httpClient.GetAsync(iconUrl);
|
|
|
|
|
|
if(!response.IsSuccessStatusCode)
|
2017-10-09 18:58:59 +02:00
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2017-10-08 22:23:17 +02:00
|
|
|
|
|
2017-10-09 13:35:07 -04:00
|
|
|
|
return new Icon
|
|
|
|
|
|
{
|
|
|
|
|
|
Image = await response.Content.ReadAsByteArrayAsync(),
|
|
|
|
|
|
Format = response.Content.Headers.ContentType.MediaType
|
|
|
|
|
|
};
|
2017-10-09 18:58:59 +02:00
|
|
|
|
});
|
2017-10-08 22:23:17 +02:00
|
|
|
|
|
2017-10-09 13:35:07 -04:00
|
|
|
|
if(icon == null)
|
2017-10-09 18:58:59 +02:00
|
|
|
|
{
|
2017-10-09 13:35:07 -04:00
|
|
|
|
return new NotFoundResult();
|
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
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-09 13:35:07 -04:00
|
|
|
|
private static string BuildIconUrl(Uri uri)
|
2017-10-08 22:23:17 +02:00
|
|
|
|
{
|
2017-10-09 13:35:07 -04:00
|
|
|
|
return $"https://icons.bitwarden.com/icon?url={uri.Host}&size=16..24..200";
|
2017-10-08 22:23:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|