1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-22 12:15:36 +01:00

do not cache large images or nulls

This commit is contained in:
Kyle Spearrin 2017-10-12 10:41:13 -04:00
parent 8b8f1fb294
commit a35055b66d

View File

@ -42,27 +42,30 @@ namespace Bit.Icons.Controllers
}
var mappedDomain = _domainMappingService.MapDomain(uri.Host);
var icon = await _memoryCache.GetOrCreateAsync(mappedDomain, async entry =>
if(!_memoryCache.TryGetValue(mappedDomain, out Icon icon))
{
entry.AbsoluteExpirationRelativeToNow = new TimeSpan(_iconsSettings.CacheHours, 0, 0);
var iconUrl = $"{_iconsSettings.BestIconBaseUrl}/icon?url={mappedDomain}&size=16..24..32";
var response = await _httpClient.GetAsync(iconUrl);
if(!response.IsSuccessStatusCode)
{
return null;
return new NotFoundResult();
}
return new Icon
var image = await response.Content.ReadAsByteArrayAsync();
icon = new Icon
{
Image = await response.Content.ReadAsByteArrayAsync(),
Image = image,
Format = response.Content.Headers.ContentType.MediaType
};
});
if(icon == null)
{
return new NotFoundResult();
// Only cache smaller images (<= 50kb)
if(image.Length <= 50012)
{
_memoryCache.Set(mappedDomain, icon, new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = new TimeSpan(_iconsSettings.CacheHours, 0, 0)
});
}
}
return new FileContentResult(icon.Image, icon.Format);