Пример #1
0
        /// <summary>
        /// The actual functionality to download with optional hash verification
        /// subclasses that wish to return the contents of the downloaded file
        /// or do something else with it can override this instead of RunWithReturn.
        /// </summary>
        /// <param name="success"></param>
        /// <returns></returns>
        protected virtual NPath RunDownload(bool success)
        {
            Exception exception   = null;
            var       attempts    = 0;
            bool      result      = false;
            var       partialFile = TargetDirectory.Combine(Filename + ".partial");

            TargetDirectory.EnsureDirectoryExists();
            do
            {
                exception = null;

                if (Token.IsCancellationRequested)
                {
                    break;
                }

                try
                {
                    Logger.Trace($"Download of {Url} to {Destination} Attempt {attempts + 1} of {RetryCount + 1}");

                    using (var destinationStream = fileSystem.OpenWrite(partialFile, FileMode.Append))
                    {
                        result = Downloader.Download(Logger, Url, destinationStream,
                                                     (value, total) =>
                        {
                            UpdateProgress(value, total);
                            return(!Token.IsCancellationRequested);
                        });
                    }

                    if (result)
                    {
                        partialFile.Move(Destination);
                    }
                }
                catch (Exception ex)
                {
                    exception = ex;
                    result    = false;
                }
            } while (!result && attempts++ < RetryCount);

            if (!result)
            {
                Token.ThrowIfCancellationRequested();
                throw new DownloadException("Error downloading file", exception);
            }

            return(Destination);
        }