public void StartDownload(string saveLocation) { try { PrepareToDownload(); if (!IsStatusCorrect()) { AfterDownload.Execute(); return; } ProcessFileLocation(saveLocation); _time = DateTime.Now; using (var wc = new WebClient()) { Status = DownloadStatus.Downloading; wc.DownloadProgressChanged += OnDownloadProgressChanged; wc.DownloadFileCompleted += OnDownloadFileCompleted; wc.DownloadFileAsync(new Uri(DownloadLink), FileLocation); wc.Proxy = null; OnPropertyChanged(nameof(Status)); } } catch (Exception ex) { Status = DownloadStatus.Error; Log.Debug("Error while starting a download process.", ex); Log.Debug(ToString()); } }
public FFMpegDownloadViewModel(Settings Settings, LanguageManager LanguageManager) : base(Settings, LanguageManager) { StartCommand = new DelegateCommand(async() => { var result = await Start(); AfterDownload?.Invoke(result); }); SelectFolderCommand = new DelegateCommand(() => { using (var dlg = new VistaFolderBrowserDialog { SelectedPath = TargetFolder, UseDescriptionForTitle = true, Description = LanguageManager.SelectFFMpegFolder }) { if (dlg.ShowDialog() == DialogResult.OK) { TargetFolder = dlg.SelectedPath; } } }); }
private void OnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { if (e.Error != null) { Status = DownloadStatus.Error; DownloadPercent = 0; Log.Debug("Error while downloading", e.Error); Log.Debug(ToString()); } else if (e.Cancelled) { Status = DownloadStatus.Canceled; DownloadPercent = 0; Log.Debug("Download has been canceld."); Log.Debug(ToString()); } else { CheckIfFileIsDownloadedSuccesful(); } OnPropertyChanged(nameof(DownloadPercent)); OnPropertyChanged(nameof(Status)); AfterDownload.Execute(); }
} // OnBeforeDownload protected void OnAfterDownload(object sender, DownloadEventArgs e) { AfterDownload?.Invoke(sender, e); } // OnAfterDownload
public FFmpegDownloadViewModel(FFmpegSettings FFmpegSettings, FFmpegDownloadModel DownloadModel, IFFmpegViewsProvider FFmpegViewsProvider, IMessageProvider MessageProvider) { this.FFmpegSettings = FFmpegSettings; _downloadModel = DownloadModel; _messageProvider = MessageProvider; StartCommand = _downloaderProgress .Select(M => M.State) .Select(M => M == FFmpegDownloaderState.Ready) .ToReactiveCommand() .WithSubscribe(async() => { var progress = new Progress <FFmpegDownloaderProgress>(M => _downloaderProgress.Value = M); _downloadTask = DownloadModel.Start(progress, _cancellationTokenSource.Token); var result = await _downloadTask; AfterDownload?.Invoke(result); }); CanCancel = _downloaderProgress .Select(M => M.State) .Select(M => M == FFmpegDownloaderState.Downloading) .ToReadOnlyReactivePropertySlim(); SelectFolderCommand = _downloaderProgress .Select(M => M.State) .Select(M => M == FFmpegDownloaderState.Ready) .ToReactiveCommand() .WithSubscribe(FFmpegViewsProvider.PickFolder); OpenFolderCommand = new ReactiveCommand() .WithSubscribe(() => { var path = FFmpegSettings.GetFolderPath(); if (Directory.Exists(path)) { Process.Start(path); } }); Status = _downloaderProgress .Select(M => { switch (M.State) { case FFmpegDownloaderState.Error: return(M.ErrorMessage); case FFmpegDownloaderState.Downloading: return($"{FFmpegDownloaderState.Downloading} ({M.DownloadProgress}%)"); default: return(M.State.ToString()); } }) .ToReadOnlyReactivePropertySlim(); Progress = _downloaderProgress .Where(M => M.State == FFmpegDownloaderState.Downloading) .Select(M => M.DownloadProgress) .ToReadOnlyReactivePropertySlim(); Progress.Subscribe(M => ProgressChanged?.Invoke(M)); InProgress = _downloaderProgress .Select(M => M.State) .Select(M => M == FFmpegDownloaderState.Downloading || M == FFmpegDownloaderState.Extracting) .ToReadOnlyReactivePropertySlim(); IsDone = _downloaderProgress .Select(M => M.State) .Select(M => M == FFmpegDownloaderState.Done || M == FFmpegDownloaderState.Cancelled || M == FFmpegDownloaderState.Error) .ToReadOnlyReactivePropertySlim(); }
public async Task <DataDownloaderResult> Download(DownloadResource downloadResource, bool ignore404 = true, int requestDelay = 0) { if (downloadResource == null || string.IsNullOrEmpty(downloadResource.Url)) { throw new ArgumentException("Для скачивания должны быть заполнены пути к данным"); } _logger.LogInformation("Downloading data: " + downloadResource.Url); var uri = downloadResource.Url.StartsWith("htt") ? new Uri(downloadResource.Url) : new Uri(new Uri(BaseUrl), downloadResource.Url); var filePath = string.IsNullOrEmpty(downloadResource.RelativePath) ? string.Empty : Path.Combine(_diaryPath, downloadResource.RelativePath); var client = new CF_WebClient(_cookieContainer); BeforeDownload?.Invoke(this, new DataDownloaderEventArgs { Resource = downloadResource }); Thread.Sleep(requestDelay); byte[] downloadedData; var retries = 0; while (true) { try { downloadedData = await client.DownloadDataTaskAsync(uri); break; //i want to break freeeeee } catch (WebException e) { if (e.Status == WebExceptionStatus.ProtocolError && ignore404) { var response = e.Response as HttpWebResponse; if (response != null) { if (response.StatusCode == HttpStatusCode.NotFound) { _logger.LogWarning("Url not found: " + e.Response.ResponseUri.AbsoluteUri); downloadResource.LocalPath = ""; return(new DataDownloaderResult { Resource = downloadResource, DownloadedData = null }); } } } retries += 1; _logger.LogError(e, $"Error, retry count: {retries}"); if (retries >= Constants.DownloadRetryCount) { throw; } Thread.Sleep(2000); } } AfterDownload?.Invoke(this, new DataDownloaderEventArgs { Resource = downloadResource, DownloadedData = downloadedData }); if (!string.IsNullOrEmpty(filePath)) { using (var f = File.Create(filePath)) { await f.WriteAsync(downloadedData, 0, downloadedData.Length); } } return(new DataDownloaderResult { Resource = downloadResource, DownloadedData = downloadedData }); }