From e52a97f2da0b1f71d60912b59249bc4475c75cd3 Mon Sep 17 00:00:00 2001 From: Lars Brubaker Date: Fri, 4 Sep 2020 18:16:20 -0700 Subject: [PATCH] working to download images --- .../Providers/GitHub/GitHubContainer.cs | 55 ++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/MatterControlLib/Library/Providers/GitHub/GitHubContainer.cs b/MatterControlLib/Library/Providers/GitHub/GitHubContainer.cs index 606de6558..a2008af05 100644 --- a/MatterControlLib/Library/Providers/GitHub/GitHubContainer.cs +++ b/MatterControlLib/Library/Providers/GitHub/GitHubContainer.cs @@ -33,6 +33,7 @@ using System.IO; using System.Net.Http; using System.Threading.Tasks; using MatterHackers.Agg; +using MatterHackers.Agg.Image; using MatterHackers.Agg.Platform; using Newtonsoft.Json; @@ -118,7 +119,6 @@ namespace MatterHackers.MatterControl.Library } else if (file.name.ToLower() == "index.md") { - } else { @@ -130,6 +130,59 @@ namespace MatterHackers.MatterControl.Library OnContentChanged(); } + private List<(string name, string url)> imageCache; + + public override Task GetThumbnail(ILibraryItem item, int width, int height) + { + var existingThumbnail = base.GetThumbnail(item, width, height); + + if (existingThumbnail.Result == null) + { + LoadImageCache(); + + foreach (var entry in imageCache) + { + if (entry.name.Contains(item.ID)) + { + // download the image and cache it + } + } + } + + return existingThumbnail; + } + + private void LoadImageCache() + { + if (imageCache == null) + { + imageCache = new List<(string name, string url)>(); + + // Check if we can find the thumbnail in the GitHub .images directory + var uri = $"https://api.github.com/repos/{Account}/{Repository}/contents/.images"; + // get the directory contents + WebCache.RetrieveText(uri, + (content) => + { + lock (locker) + { + FileInfo[] dirContents = JsonConvert.DeserializeObject(content); + + // read in data + foreach (FileInfo file in dirContents) + { + if (file.type == "file") + { + imageCache.Add((file.name, file.download_url)); + } + } + } + }, + false, + AddCromeHeaders); + } + } + public static void AddCromeHeaders(HttpRequestMessage request) { request.Headers.Add("Connection", "keep-alive");