1
0
mirror of https://github.com/bitwarden/server.git synced 2025-05-25 13:24:50 -05: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 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 iconUrl = $"{_iconsSettings.BestIconBaseUrl}/icon?url={mappedDomain}&size=16..24..32";
var response = await _httpClient.GetAsync(iconUrl); var response = await _httpClient.GetAsync(iconUrl);
if(!response.IsSuccessStatusCode) 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 Format = response.Content.Headers.ContentType.MediaType
}; };
});
if(icon == null) // Only cache smaller images (<= 50kb)
{ if(image.Length <= 50012)
return new NotFoundResult(); {
_memoryCache.Set(mappedDomain, icon, new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = new TimeSpan(_iconsSettings.CacheHours, 0, 0)
});
}
} }
return new FileContentResult(icon.Image, icon.Format); return new FileContentResult(icon.Image, icon.Format);