private async Task DownloadSingle(HttpClient client, YoloFile file) { var progress = new Progress <double>(); var cancellationTokenSource = new CancellationTokenSource(); try { var response = await client.GetAsync(file.OnlineUrl, HttpCompletionOption.ResponseHeadersRead, cancellationTokenSource.Token); var size = response.Content.Headers.ContentLength; if (size == null) { throw new Exception("Couldn't get content length"); } using var httpStream = new StreamProgressTracker(await response.Content.ReadAsStreamAsync(), progress, size !.Value); using var fileStream = new FileStream(file.ExpectedPath, FileMode.Create); _progressOverlay.DisplayProgress($"Downloading Yolo files", cancellationTokenSource, new ProgressStep(file.Name, progress)); await httpStream.CopyToAsync(fileStream); } catch (Exception e) { _logger.LogError(e, "Failed to download Yolo File {file}", file.Name); cancellationTokenSource.Cancel(); } }
private async Task <YoloWrapper> InitYolo() { _logger.LogInformation("Init, Yolo"); var currentDir = Directory.GetCurrentDirectory(); var targetDir = Path.Combine(currentDir, "yolo"); var cfg = new YoloFile(targetDir, "yolo-manga.cfg"); var weights = new YoloFile(targetDir, "yolo-manga.weights"); var names = new YoloFile(targetDir, "yolo-manga.names"); var files = new[] { cfg, weights, names }; var haveAllFiles = Directory.Exists(targetDir) && files.All(x => File.Exists(x.ExpectedPath)); if (haveAllFiles) { return(new YoloWrapper(cfg.ExpectedPath, weights.ExpectedPath, names.ExpectedPath)); } foreach (var yoloFile in files) { if (!Directory.Exists(targetDir)) { Directory.CreateDirectory(targetDir); } IfExistsDelete(yoloFile.ExpectedPath); } await DownloadAll(files); return(new YoloWrapper(cfg.ExpectedPath, weights.ExpectedPath, names.ExpectedPath)); }