Files
Wino-Mail/Wino.Core.UWP/Services/ThumbnailService.cs

64 lines
1.6 KiB
C#
Raw Normal View History

2024-04-18 01:44:37 +02:00
using System;
using System.Linq;
using System.Net.Mail;
2025-02-16 11:54:23 +01:00
namespace Wino.Core.UWP.Services;
public static class ThumbnailService
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
private static string[] knownCompanies = new string[]
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
"microsoft.com", "apple.com", "google.com", "steampowered.com", "airbnb.com", "youtube.com", "uber.com"
};
public static bool IsKnown(string mailHost) => !string.IsNullOrEmpty(mailHost) && knownCompanies.Contains(mailHost);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public static string GetHost(string address)
{
if (string.IsNullOrEmpty(address))
return string.Empty;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (address.Contains('@'))
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
var splitted = address.Split('@');
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (splitted.Length >= 2 && !string.IsNullOrEmpty(splitted[1]))
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
try
2025-02-16 11:35:43 +01:00
{
2025-02-16 11:54:23 +01:00
return new MailAddress(address).Host;
}
catch (Exception)
{
// TODO: Exceptions are ignored for now.
2024-04-18 01:44:37 +02:00
}
}
}
2025-02-16 11:54:23 +01:00
return string.Empty;
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public static Tuple<bool, string> CheckIsKnown(string host)
{
// Check known hosts.
// Apply company logo if available.
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
try
{
var last = host.Split('.');
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (last.Length > 2)
host = $"{last[last.Length - 2]}.{last[last.Length - 1]}";
}
catch (Exception)
{
return new Tuple<bool, string>(false, host);
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:54:23 +01:00
return new Tuple<bool, string>(IsKnown(host), host);
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:54:23 +01:00
public static string GetKnownHostImage(string host)
=> $"ms-appx:///Assets/Thumbnails/{host}.png";
2024-04-18 01:44:37 +02:00
}