public static async Task DownloadDirectoryAsync(this IHosting hosting, UPath source, DirectoryInfo destination, IProgress <UPath> failures = null, IProgress <UPath> successes = null) { if (File.Exists(destination.FullName)) { throw new UnexpectedItemType("Destination should be a directory"); } if (destination.Parent == null || File.Exists(destination.Parent.FullName) || !destination.Parent.Exists) { throw new UnexpectedItemType("Problem with destination parent"); } if (!destination.Exists) { Directory.CreateDirectory(destination.FullName); } var tasks = new List <Task>(); var items = await hosting.GetDirectoryListAsync(source); foreach (var item in items) { var destFile = destination.GetSubFile(item.Name); var destDir = destination.GetSubDirectory(item.Name); var task = item.IsFile ? hosting.DownloadFileAsync(item.Path, destFile) : hosting.DownloadDirectoryAsync(item.Path, destDir, failures, successes); task = task.ContinueWith(t => (t.IsFaulted ? failures : successes).Report(item.Path)); tasks.Add(task); } await Task.WhenAll(tasks); }
private static async Task <Node <ItemInfo> > GetDirectoryItemsTreeAsync(this IHosting hosting, UPath path) { var nested = (await hosting.GetDirectoryListAsync(path)) .Select(async i => { return(i.IsDirectory ? await hosting.GetDirectoryItemsTreeAsync(i.Path) : new Node <ItemInfo>(await hosting.GetItemInfoAsync(i.Path))); }).ToList(); await Task.WhenAll(nested); var node = new Node <ItemInfo>(await hosting.GetItemInfoAsync(path)); node.Nested.AddRange(nested.Select(n => n.Result)); return(node); }