From 843396f7c4340e04e2658ea9d1615ab988e9f1f2 Mon Sep 17 00:00:00 2001 From: erri120 Date: Fri, 10 Jul 2020 13:55:53 +0200 Subject: [PATCH 1/2] Cache favicons --- Wabbajack.Common/Consts.cs | 1 + .../View Models/Settings/LoginManagerVM.cs | 59 ++++++++++++++----- 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/Wabbajack.Common/Consts.cs b/Wabbajack.Common/Consts.cs index 3ebb91f0..eae4b28e 100644 --- a/Wabbajack.Common/Consts.cs +++ b/Wabbajack.Common/Consts.cs @@ -102,6 +102,7 @@ namespace Wabbajack.Common public const string ModListExtensionString = ".wabbajack"; public static Extension ModListExtension = new Extension(ModListExtensionString); public static AbsolutePath LocalAppDataPath => new AbsolutePath(Path.Combine(KnownFolders.LocalAppData.Path, "Wabbajack")); + public static AbsolutePath FaviconCacheFolderPath => LocalAppDataPath.Combine("favicons"); public static string MetricsKeyHeader => "x-metrics-key"; public static string WabbajackCacheLocation = "http://build.wabbajack.org/nexus_api_cache/"; diff --git a/Wabbajack/View Models/Settings/LoginManagerVM.cs b/Wabbajack/View Models/Settings/LoginManagerVM.cs index 66af9521..d8d84224 100644 --- a/Wabbajack/View Models/Settings/LoginManagerVM.cs +++ b/Wabbajack/View Models/Settings/LoginManagerVM.cs @@ -15,6 +15,7 @@ using System.Windows.Threading; using Microsoft.WindowsAPICodePack.Shell.PropertySystem; using ReactiveUI; using ReactiveUI.Fody.Helpers; +using Wabbajack.Common; using Wabbajack.Lib; using Wabbajack.Lib.Downloaders; @@ -84,22 +85,50 @@ namespace Wabbajack { if (Login.IconUri == null) return; - using var img = await new Wabbajack.Lib.Http.Client().GetAsync(Login.IconUri, errorsAsExceptions:false); - if (!img.IsSuccessStatusCode) return; + if(!Consts.FaviconCacheFolderPath.Exists) + Consts.FaviconCacheFolderPath.CreateDirectory(); - var icoData = new MemoryStream(await img.Content.ReadAsByteArrayAsync()); - - var data = new Icon(icoData); - var ms = new MemoryStream(); - data.ToBitmap().Save(ms, ImageFormat.Png); - ms.Position = 0; - - var source = new BitmapImage(); - source.BeginInit(); - source.StreamSource = ms; - source.EndInit(); - source.Freeze(); - Favicon = source; + var faviconIcon = Consts.FaviconCacheFolderPath.Combine($"{Login.SiteName}.ico"); + if (faviconIcon.Exists) + { + await using var fs = await faviconIcon.OpenRead(); + + var ms = new MemoryStream((int)fs.Length); + await fs.CopyToAsync(ms); + ms.Position = 0; + + var source = new BitmapImage(); + source.BeginInit(); + source.StreamSource = ms; + source.EndInit(); + source.Freeze(); + Favicon = source; + } + else + { + using var img = await new Lib.Http.Client().GetAsync(Login.IconUri, errorsAsExceptions: false); + if (!img.IsSuccessStatusCode) return; + + var icoData = new MemoryStream(await img.Content.ReadAsByteArrayAsync()); + + var data = new Icon(icoData); + var ms = new MemoryStream(); + data.ToBitmap().Save(ms, ImageFormat.Png); + ms.Position = 0; + + await using (var fs = await faviconIcon.Create()) + { + await ms.CopyToAsync(fs); + ms.Position = 0; + } + + var source = new BitmapImage(); + source.BeginInit(); + source.StreamSource = ms; + source.EndInit(); + source.Freeze(); + Favicon = source; + } }); } } From 49eb75c5dcfec6be0f6dde78b62166e009d7552b Mon Sep 17 00:00:00 2001 From: erri120 Date: Fri, 10 Jul 2020 14:03:07 +0200 Subject: [PATCH 2/2] Delete favicons that are older than 10 days --- Wabbajack/View Models/Settings/LoginManagerVM.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Wabbajack/View Models/Settings/LoginManagerVM.cs b/Wabbajack/View Models/Settings/LoginManagerVM.cs index d8d84224..725e0fbf 100644 --- a/Wabbajack/View Models/Settings/LoginManagerVM.cs +++ b/Wabbajack/View Models/Settings/LoginManagerVM.cs @@ -89,6 +89,18 @@ namespace Wabbajack Consts.FaviconCacheFolderPath.CreateDirectory(); var faviconIcon = Consts.FaviconCacheFolderPath.Combine($"{Login.SiteName}.ico"); + if (faviconIcon.Exists) + { + var fsi = new FileInfo(faviconIcon.ToString()); + var creationDate = fsi.CreationTimeUtc; + var now = DateTime.UtcNow; + + //delete favicons older than 10 days + + if ((now - creationDate).TotalDays > 10) + await faviconIcon.DeleteAsync(); + } + if (faviconIcon.Exists) { await using var fs = await faviconIcon.OpenRead();