Skip to content

Commit 41dbd97

Browse files
committed
Adds Custom Download Provider option.
Co-authored-by: Henri J. Norden <55378880+Henri-J-Norden@users.noreply.github.com>
1 parent 7994689 commit 41dbd97

10 files changed

Lines changed: 618 additions & 9 deletions

YAFD/Configuration.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ public sealed class Configuration
3535
private const string maximumIconSize = pluginName + "MaximumIconSize";
3636
private int? m_maximumIconSize = null;
3737

38+
/// <summary>
39+
/// Custom download provider
40+
/// </summary>
41+
private const string customDownloadProvider = pluginName + "CustomDownloadProvider";
42+
private string m_customDownloadProvider = null;
43+
3844
public Configuration(AceCustomConfig aceCustomConfig)
3945
{
4046
config = aceCustomConfig;
@@ -104,5 +110,21 @@ public void SetMaximumIconSize(int value)
104110
m_maximumIconSize = value;
105111
config.SetLong(maximumIconSize, value);
106112
}
113+
114+
public string GetCustomDownloadProvider()
115+
{
116+
if (string.IsNullOrEmpty(m_customDownloadProvider))
117+
{
118+
m_customDownloadProvider = config.GetString(customDownloadProvider, "");
119+
}
120+
121+
return m_customDownloadProvider;
122+
}
123+
124+
public void SetCustomDownloadProvider(string value)
125+
{
126+
m_customDownloadProvider = value;
127+
config.SetString(customDownloadProvider, value);
128+
}
107129
}
108130
}

YAFD/FaviconDialog.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,17 @@ public FaviconDialog(IPluginHost host)
6060
pluginHost.MainWindow.UIBlockInteraction(true);
6161
}
6262

63-
public void Run(PwEntry[] entries)
63+
public void Run(PwEntry[] entries, bool customProvider)
6464
{
6565
this.entries = entries;
66-
bgWorker.RunWorkerAsync();
66+
bgWorker.RunWorkerAsync(customProvider);
6767
}
6868

6969
private void BgWorker_DoWork(object sender, DoWorkEventArgs e)
7070
{
71+
// Custom provider download (argument)
72+
bool customProvider = (bool)e.Argument;
73+
7174
// Progress information
7275
ProgressInfo progress = new ProgressInfo(entries.Length);
7376

@@ -119,8 +122,16 @@ private void BgWorker_DoWork(object sender, DoWorkEventArgs e)
119122
{
120123
try
121124
{
125+
byte[] data = null;
122126
// Download favicon
123-
byte[] data = fd.GetIcon(url);
127+
if (customProvider)
128+
{
129+
data = fd.GetIconCustomProvider(url);
130+
}
131+
else
132+
{
133+
data = fd.GetIcon(url);
134+
}
124135
Util.Log("Icon downloaded with success");
125136

126137
// Hash icon data (avoid duplicates)

YAFD/FaviconDownloader.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,55 @@ public byte[] GetIcon(string url)
204204
// If there is no file available
205205
throw new FaviconDownloaderException(FaviconDownloaderExceptionStatus.NotFound);
206206
}
207+
208+
public byte[] GetIconCustomProvider(string url)
209+
{
210+
// Get the hostname from the requested URL
211+
var hostname = GetValidHost(url);
212+
213+
// Custom provider settings
214+
var providerURL = YetAnotherFaviconDownloaderExt.Config.GetCustomDownloadProvider();
215+
var iconSize = YetAnotherFaviconDownloaderExt.Config.GetMaximumIconSize().ToString();
216+
217+
// Follows KeePass placeholders convention
218+
// https://keepass.info/help/base/placeholders.html
219+
220+
// Maybe in the future we can give full/proper support, well, not today, for now it's enough
221+
providerURL = Regex.Replace(providerURL, "{URL:HOST}", hostname, RegexOptions.IgnoreCase);
222+
providerURL = Regex.Replace(providerURL, "{YAFD:ICON_SIZE}", iconSize, RegexOptions.IgnoreCase);
223+
224+
Uri address = new Uri(providerURL);
225+
226+
Util.Log("CustomProvider: {0} => {1}", url, providerURL);
227+
228+
try
229+
{
230+
// Download file
231+
byte[] data = DownloadData(address);
232+
233+
// Check if the data is a valid image and then try to resize it
234+
if (ResizeImage(ref data))
235+
{
236+
return data;
237+
}
238+
}
239+
catch (WebException ex)
240+
{
241+
HttpWebResponse response = ex.Response as HttpWebResponse;
242+
if (response != null && response.StatusCode == HttpStatusCode.NotFound)
243+
{
244+
throw new FaviconDownloaderException(FaviconDownloaderExceptionStatus.NotFound);
245+
}
246+
else
247+
{
248+
throw new FaviconDownloaderException(ex);
249+
}
250+
}
251+
252+
// If there is no file available
253+
throw new FaviconDownloaderException(FaviconDownloaderExceptionStatus.NotFound);
254+
}
255+
207256
public string GetValidHost(string url)
208257
{
209258
if (!httpSchema.IsMatch(url))

YAFD/Provider.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Collections.Generic;
2+
3+
namespace YetAnotherFaviconDownloader
4+
{
5+
public class Provider
6+
{
7+
public string Name { get; set; }
8+
public string URL { get; set; }
9+
10+
public Provider(string name, string url)
11+
{
12+
Name = name;
13+
URL = url;
14+
}
15+
16+
public override bool Equals(object obj)
17+
{
18+
Provider provider = obj as Provider;
19+
return provider != null &&
20+
URL == provider.URL;
21+
}
22+
23+
public override int GetHashCode()
24+
{
25+
return -1251312914 + EqualityComparer<string>.Default.GetHashCode(URL);
26+
}
27+
28+
public override string ToString()
29+
{
30+
return Name;
31+
}
32+
}
33+
}

YAFD/ProviderList.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text.RegularExpressions;
4+
5+
namespace YetAnotherFaviconDownloader
6+
{
7+
static class ProviderList
8+
{
9+
public static readonly string CustomURLName = "Custom URL";
10+
11+
private static readonly List<Provider> providers = new List<Provider>();
12+
private static readonly Provider customProvider = new Provider(CustomURLName, null);
13+
14+
static ProviderList()
15+
{
16+
providers.Add(new Provider("None (Default)", null));
17+
providers.Add(new Provider("DuckDuckGo", "https://icons.duckduckgo.com/ip3/{URL:HOST}.ico"));
18+
providers.Add(new Provider("Favicon Kit", "https://api.faviconkit.com/{URL:HOST}/{YAFD:ICON_SIZE}"));
19+
providers.Add(new Provider("Google", "https://www.google.com/s2/favicons?domain={URL:HOST}&sz={YAFD:ICON_SIZE}"));
20+
providers.Add(new Provider("Yandex", "https://favicon.yandex.net/favicon/{URL:HOST}"));
21+
providers.Add(customProvider);
22+
}
23+
24+
public static Provider[] GetDefaultList()
25+
{
26+
return providers.ToArray();
27+
}
28+
29+
public static Provider FindByURL(string url)
30+
{
31+
return providers.Find(x => x.URL == url);
32+
}
33+
34+
public static void SetCustomProviderURL(string url)
35+
{
36+
customProvider.URL = url;
37+
}
38+
39+
public static bool IsValidURL(string url)
40+
{
41+
// HTTP URI schema
42+
var httpSchema = new Regex(@"^http(s)?://.+", RegexOptions.IgnoreCase | RegexOptions.Compiled);
43+
44+
if (!httpSchema.IsMatch(url))
45+
{
46+
return false;
47+
}
48+
49+
Uri result;
50+
return Uri.TryCreate(url, UriKind.Absolute, out result);
51+
}
52+
}
53+
}

YAFD/UI/DownloadProvider.Designer.cs

Lines changed: 145 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)