1
0
mirror of https://github.com/bitwarden/server.git synced 2025-05-07 04:32:20 -05:00

icon server logging

This commit is contained in:
Kyle Spearrin 2018-08-25 16:41:26 -04:00
parent 6fa49ff65b
commit 0956374a06
2 changed files with 14 additions and 1 deletions

View File

@ -4,6 +4,7 @@ using Bit.Icons.Models;
using Bit.Icons.Services; using Bit.Icons.Services;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
namespace Bit.Icons.Controllers namespace Bit.Icons.Controllers
{ {
@ -13,17 +14,20 @@ namespace Bit.Icons.Controllers
private readonly IMemoryCache _memoryCache; private readonly IMemoryCache _memoryCache;
private readonly IDomainMappingService _domainMappingService; private readonly IDomainMappingService _domainMappingService;
private readonly IIconFetchingService _iconFetchingService; private readonly IIconFetchingService _iconFetchingService;
private readonly ILogger<IconsController> _logger;
private readonly IconsSettings _iconsSettings; private readonly IconsSettings _iconsSettings;
public IconsController( public IconsController(
IMemoryCache memoryCache, IMemoryCache memoryCache,
IDomainMappingService domainMappingService, IDomainMappingService domainMappingService,
IIconFetchingService iconFetchingService, IIconFetchingService iconFetchingService,
ILogger<IconsController> logger,
IconsSettings iconsSettings) IconsSettings iconsSettings)
{ {
_memoryCache = memoryCache; _memoryCache = memoryCache;
_domainMappingService = domainMappingService; _domainMappingService = domainMappingService;
_iconFetchingService = iconFetchingService; _iconFetchingService = iconFetchingService;
_logger = logger;
_iconsSettings = iconsSettings; _iconsSettings = iconsSettings;
} }
@ -54,6 +58,7 @@ namespace Bit.Icons.Controllers
var result = await _iconFetchingService.GetIconAsync(domain); var result = await _iconFetchingService.GetIconAsync(domain);
if(result == null) if(result == null)
{ {
_logger.LogInformation("Null result returned.");
icon = null; icon = null;
} }
else else
@ -64,6 +69,7 @@ namespace Bit.Icons.Controllers
// Only cache not found and smaller images (<= 50kb) // Only cache not found and smaller images (<= 50kb)
if(_iconsSettings.CacheEnabled && (icon == null || icon.Image.Length <= 50012)) if(_iconsSettings.CacheEnabled && (icon == null || icon.Image.Length <= 50012))
{ {
_logger.LogInformation("Cache the icon.");
_memoryCache.Set(mappedDomain, icon, new MemoryCacheEntryOptions _memoryCache.Set(mappedDomain, icon, new MemoryCacheEntryOptions
{ {
AbsoluteExpirationRelativeToNow = new TimeSpan(_iconsSettings.CacheHours, 0, 0), AbsoluteExpirationRelativeToNow = new TimeSpan(_iconsSettings.CacheHours, 0, 0),

View File

@ -7,6 +7,7 @@ using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using Bit.Icons.Models; using Bit.Icons.Models;
using AngleSharp.Parser.Html; using AngleSharp.Parser.Html;
using Microsoft.Extensions.Logging;
namespace Bit.Icons.Services namespace Bit.Icons.Services
{ {
@ -29,9 +30,11 @@ namespace Bit.Icons.Services
private readonly HashSet<string> _allowedMediaTypes; private readonly HashSet<string> _allowedMediaTypes;
private readonly HttpClient _httpClient; private readonly HttpClient _httpClient;
private readonly ILogger<IIconFetchingService> _logger;
public IconFetchingService() public IconFetchingService(ILogger<IIconFetchingService> logger)
{ {
_logger = logger;
_allowedMediaTypes = new HashSet<string> _allowedMediaTypes = new HashSet<string>
{ {
_pngMediaType, _pngMediaType,
@ -52,6 +55,7 @@ namespace Bit.Icons.Services
{ {
if(!Uri.TryCreate($"https://{domain}", UriKind.Absolute, out var parsedHttpsUri)) if(!Uri.TryCreate($"https://{domain}", UriKind.Absolute, out var parsedHttpsUri))
{ {
_logger.LogInformation("Bad domain.");
return null; return null;
} }
@ -87,6 +91,7 @@ namespace Bit.Icons.Services
if(response?.Content == null || !response.IsSuccessStatusCode) if(response?.Content == null || !response.IsSuccessStatusCode)
{ {
Cleanup(response); Cleanup(response);
_logger.LogInformation("Couldn't load a website: {0}.", response.StatusCode);
return null; return null;
} }
@ -98,6 +103,7 @@ namespace Bit.Icons.Services
uri = response.RequestMessage.RequestUri; uri = response.RequestMessage.RequestUri;
if(document.DocumentElement == null) if(document.DocumentElement == null)
{ {
_logger.LogInformation("No DocumentElement.");
return null; return null;
} }
@ -198,6 +204,7 @@ namespace Bit.Icons.Services
} }
else else
{ {
_logger.LogInformation("No favicon.ico found.");
return null; return null;
} }
} }