1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-10 15:48:13 -05:00

cleanup html node refs

This commit is contained in:
Kyle Spearrin 2018-06-19 10:18:11 -04:00
parent 18b7bcb9e3
commit 145e4c69d4
2 changed files with 31 additions and 15 deletions

View File

@ -5,13 +5,12 @@ namespace Bit.Icons.Models
{
public class IconResult
{
public IconResult(string href, HtmlNode node)
public IconResult(string href, string sizes)
{
Path = href;
var sizesAttr = node.Attributes["sizes"];
if(!string.IsNullOrWhiteSpace(sizesAttr?.Value))
if(!string.IsNullOrWhiteSpace(sizes))
{
var sizeParts = sizesAttr.Value.Split('x');
var sizeParts = sizes.Split('x');
if(sizeParts.Length == 2 && int.TryParse(sizeParts[0].Trim(), out var width) &&
int.TryParse(sizeParts[1].Trim(), out var height))
{

View File

@ -61,23 +61,29 @@ namespace Bit.Icons.Services
uri = response.RequestMessage.RequestUri;
var doc = new HtmlDocument();
try
using(var htmlStream = await response.Content.ReadAsStreamAsync())
{
var html = await response.Content.ReadAsStringAsync();
if(html == null)
if(htmlStream == null)
{
doc = null;
return null;
}
doc.LoadHtml(html);
if(doc.DocumentNode == null)
try
{
doc.Load(htmlStream);
if(doc.DocumentNode == null)
{
doc = null;
return null;
}
}
catch
{
doc = null;
return null;
}
}
catch
{
return null;
}
var baseUrl = "/";
var baseUrlNode = doc.DocumentNode.SelectSingleNode(@"//head/base[@href]");
@ -88,10 +94,14 @@ namespace Bit.Icons.Services
{
baseUrl = hrefAttr.Value;
}
baseUrlNode = null;
hrefAttr = null;
}
var icons = new List<IconResult>();
var links = doc.DocumentNode.SelectNodes(@"//head/link[@href]");
doc = null;
if(links != null)
{
foreach(var link in links.Take(40))
@ -103,9 +113,10 @@ namespace Bit.Icons.Services
}
var relAttr = link.Attributes["rel"];
var sizesAttr = link.Attributes["sizes"];
if(relAttr != null && _iconRels.Contains(relAttr.Value.ToLower()))
{
icons.Add(new IconResult(hrefAttr.Value, link));
icons.Add(new IconResult(hrefAttr.Value, sizesAttr?.Value));
}
else
{
@ -114,12 +125,18 @@ namespace Bit.Icons.Services
var extension = Path.GetExtension(hrefAttr.Value);
if(_iconExtensions.Contains(extension.ToLower()))
{
icons.Add(new IconResult(hrefAttr.Value, link));
icons.Add(new IconResult(hrefAttr.Value, sizesAttr?.Value));
}
}
catch(ArgumentException) { }
}
sizesAttr = null;
relAttr = null;
hrefAttr = null;
}
links = null;
}
var iconResultTasks = new List<Task>();