can download items

This commit is contained in:
Lars Brubaker 2020-07-31 18:27:09 -07:00
parent db4d440c55
commit 333ef89a9f
2 changed files with 28 additions and 50 deletions

View file

@ -41,32 +41,18 @@ namespace MatterHackers.MatterControl.Library
{
public class GitHubContainer : LibraryContainer
{
public struct Directory
{
public List<FileData> Files;
public string Name;
public List<Directory> SubDirs;
}
// Used to hold file data
public struct FileData
{
public string Contents;
public string Name;
}
internal struct FileInfo
{
public LinkFields _links;
public string DownloadUrl;
public string Name;
public string Type;
public string download_url;
public string name;
public string type;
}
// JSON parsing methods
internal struct LinkFields
{
public string Self;
public string self;
}
private PrinterConfig printer;
@ -103,17 +89,16 @@ namespace MatterHackers.MatterControl.Library
}
// Get all files from a repo
public async Task<Directory> GetRepo()
public async Task GetRepo()
{
HttpClient client = new HttpClient();
Directory root = await ReadDirectory("root",
await ReadDirectory("root",
client,
$"https://api.github.com/repos/{Account}/{Repository}/contents/{RepoDirectory}");
client.Dispose();
return root;
}
private async Task<Directory> ReadDirectory(string name, HttpClient client, string uri)
private async Task ReadDirectory(string name, HttpClient client, string uri)
{
// get the directory contents
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, uri);
@ -126,20 +111,16 @@ namespace MatterHackers.MatterControl.Library
FileInfo[] dirContents = JsonConvert.DeserializeObject<FileInfo[]>(jsonStr);
// read in data
Directory result;
result.Name = name;
result.SubDirs = new List<Directory>();
result.Files = new List<FileData>();
foreach (FileInfo file in dirContents)
{
if (file.Type == "dir")
if (file.type == "dir")
{
this.ChildContainers.Add(
new DynamicContainerLink(
() => file.Name,
() => file.name,
AggContext.StaticData.LoadIcon(Path.Combine("Library", "folder_20x20.png")),
AggContext.StaticData.LoadIcon(Path.Combine("Library", "calibration_library_folder.png")),
() => new GitHubContainer(printer, file.Name, Account, Repository, file.Name),
() => new GitHubContainer(printer, file.name, Account, Repository, RepoDirectory + "/" + file.name),
() =>
{
return true;
@ -152,13 +133,11 @@ namespace MatterHackers.MatterControl.Library
// Directory sub = await ReadDirectory(file.name, client, file._links.self, access_token);
// result.subDirs.Add(sub);
}
else if (file.Type == "file")
else if (file.type == "file")
{
this.Items.Add(new GitHubLibraryItem(file.Name, file.DownloadUrl));
this.Items.Add(new GitHubLibraryItem(file.name, file.download_url));
}
}
return result;
}
public static void AddCromeHeaders(HttpRequestMessage request)

View file

@ -27,15 +27,12 @@ of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
using MatterHackers.Localizations;
using MatterHackers.MatterControl.DataStorage;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using MatterHackers.Agg;
using MatterHackers.MatterControl.DataStorage;
namespace MatterHackers.MatterControl.Library
{
@ -51,7 +48,7 @@ namespace MatterHackers.MatterControl.Library
public string Category { get; set; }
public string ContentType => Path.GetExtension(Url);
public string ContentType => FileExtension.ToLower();
public DateTime DateCreated { get; } = DateTime.Now;
@ -72,18 +69,20 @@ namespace MatterHackers.MatterControl.Library
public virtual bool LocalContentExists => File.Exists(CachePath);
public string Name { get; set; }
public string Url { get; }
public string ThumbnailUrl { get; internal set; }
private string CachePath => GetLibraryPath(FileKey, FileExtension);
public string FileKey { get; private set; }
public string FileKey => Url.GetLongHashCode().ToString();
public string FileExtension { get; private set; }
public string FileExtension => Path.GetExtension(Name).Substring(1);
public static string GetLibraryPath(string fileKey, string fileExtension)
{
return Path.Combine(ApplicationDataStorage.Instance.CloudLibraryPath, $"{fileKey}.{fileExtension}");
return Path.Combine(ApplicationDataStorage.Instance.LibraryAssetsPath, $"{fileKey}.{fileExtension}");
}
public async Task<StreamAndLength> GetStream(Action<double, string> reportProgress)
@ -110,8 +109,6 @@ namespace MatterHackers.MatterControl.Library
private async Task<string> DownloadDigitalItem(Action<double, string> reportProgress, string libraryFilePath)
{
string url = "";
// Check for library cache file and download if missing
if (!File.Exists(libraryFilePath))
{
@ -120,7 +117,7 @@ namespace MatterHackers.MatterControl.Library
using (var writeStream = File.Create(tempFilePath))
{
await DownloadWithProgress(url, writeStream, reportProgress);
await DownloadWithProgress(writeStream, reportProgress);
}
reportProgress?.Invoke(0, "");
@ -142,20 +139,22 @@ namespace MatterHackers.MatterControl.Library
return libraryFilePath;
}
private async Task DownloadWithProgress(string url, FileStream fileStream, Action<double, string> reportProgress)
private async Task DownloadWithProgress(FileStream fileStream, Action<double, string> reportProgress)
{
try
{
// get the file contents;
HttpRequestMessage downLoadUrl = new HttpRequestMessage(HttpMethod.Get, url);
HttpRequestMessage downLoadUrl = new HttpRequestMessage(HttpMethod.Get, Url);
GitHubContainer.AddCromeHeaders(downLoadUrl);
string content = "";
using (HttpClient client = new HttpClient())
using (var client = new HttpClient())
{
using (HttpResponseMessage contentResponse = await client.SendAsync(downLoadUrl))
{
content = await contentResponse.Content.ReadAsStringAsync();
using (var readData = await contentResponse.Content.ReadAsStreamAsync())
{
await readData.CopyToAsync(fileStream);
}
}
}
}