public async Task <List <DownloadedGithubFile> > DownloadFolder(string owner, string repoName, string branch, List <GithubFile> folder) { List <DownloadedGithubFile> files = new List <DownloadedGithubFile>(); foreach (GithubFile file in folder) { if (file.IsDirectory) { string path = file.Path; var items = await GetRepositoryContent(owner, repoName, branch, path); List <GithubFile> innerFolder = items.ToGithubFiles(); List <DownloadedGithubFile> inner = await DownloadFolder(owner, repoName, branch, innerFolder); files.AddRange(inner); } else { if (FileDownloadStarted != null) { FileDownloadStarted(file.Name, EventArgs.Empty); } Console.WriteLine("Downloading: " + file.Name + " (" + file.DownloadUrl + ")"); DownloadedGithubFile downloaded = await DownloadFile(file.Name, file.DownloadUrl, file.Path); files.Add(downloaded); } } return(files); }
public async Task <DownloadedGithubFile> DownloadFile(string name, string url, string path) { DownloadedGithubFile result = new DownloadedGithubFile();; using (var client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync(url); result.Name = name; result.Path = path; result.Stream = await response.Content.ReadAsStreamAsync(); } return(result); }
async void OnSelectClick(object sender, EventArgs e) { if (ContentView.FileList.GithubFiles == null) { return; } Device.BeginInvokeOnMainThread(delegate { ContentView.FileList.Hide(); ContentView.ShowLoading(); }); List <GithubFile> folder = ContentView.FileList.GithubFiles; List <DownloadedGithubFile> files = await HubClient.Instance.DownloadFolder(GithubOwner, GithubRepo, CurrentBranch, folder); Toast.Show("Saving...", ContentView); /* * This is where we update pathing -> Shouldn't use repository folder hierarcy * We get the root of the style folder, and use that as the root folder for our folder hierarcy * * We can safely assume the root folder of the style, not the repository, * contains a config file (currently only supports project.json) * and therefore can remove any other folder hierarchy * * TODO What if it always isn't like that? && support other types of config files * */ DownloadedGithubFile projectFile = files.Find(file => file.IsProjectFile); string folderPath = projectFile.FolderPath; string[] split = folderPath.Split('/'); string rootFolder = split[split.Length - 1]; int length = folderPath.IndexOf(rootFolder, StringComparison.Ordinal); string repoRootFolder = folderPath.Substring(0, length); foreach (DownloadedGithubFile file in files) { file.Path = file.Path.Replace(repoRootFolder, ""); FileUtils.SaveFileToFolder(file.Stream, file.Path, file.Name); } string zipname = rootFolder + Parser.ZipExtension; string source = Path.Combine(Parser.ApplicationFolder, rootFolder); Toast.Show("Comperssing...", ContentView); string destination = Parser.Compress(source, zipname, MyStyleFolder); // Destination contains filename, just remove it destination = destination.Replace(zipname, ""); Toast.Show("Done!", ContentView); Device.BeginInvokeOnMainThread(delegate { ContentView.HideLoading(); }); RepositoryData item = new RepositoryData(); item.GithubId = GithubId; item.StyleName = rootFolder; item.Owner = GithubOwner; item.Name = GithubRepo; item.RepositoryPath = GithubPath; item.Branch = CurrentBranch; item.LocalPath = Path.Combine(MyStyleFolder, rootFolder); item.CanUploadToGithub = true; item.ConstructPrimaryKey(); bool inserted = LocalStorage.Instance.Insert(item); // TODO Theoretically should check this earlier to and then prompt // the user whether they wish to overwrite an existing style if (!inserted) { LocalStorage.Instance.Update(item); } ShowMyStyles(); }