public ProgressEventArgs(ProgressStatistic progressStatistic)
		{
			if (progressStatistic == null)
			{
				throw new ArgumentNullException(nameof(progressStatistic));
			}
			ProgressStatistic = progressStatistic;
		}
	    public DownloadedEventArgs(DownloadedFile file, ProgressStatistic stats = null)
        {
            File = file;
            ProgressStatistics = stats;
        }
예제 #3
0
		public bool DownloadFile(FtpListItem file)
		{
			var filePath = _settings.TempDownloadPath + file.Name;

			try
			{
				var fileExists = File.Exists(filePath);
				
				var stats = new ProgressStatistic
				{
					CurrentBytesSampleCount = 50
				};

				stats.ProgressChanged += OnProgressUpdated;
				stats.Finished += OnProgressUpdated;

				if (!_client.FileExists(file.FullName))
				{
					Log.Warn($"Attempted to download {file.Name}, but it no longer exists on the server.");
					DownloadError(new DownloadedEventArgs(new DownloadedFile {DownloadLocation = filePath, FtpListItem = file}));
					return false;
				}

				DownloadStarted(new DownloadedEventArgs(new DownloadedFile { DownloadLocation = filePath, FtpListItem = file }, stats));

				using (var local = new FileStream(filePath, fileExists ? FileMode.Append : FileMode.Create))
				{					
					using (var stream = _client.OpenRead(file.FullName, local.Position))
					{
						var args = new CopyFromArguments(stats.ProgressChange, new TimeSpan(0, 0, 1), stream.Length);
						Log.Info($"{(fileExists ? "Resuming" : "Downloading")} {file.Name}");
						local.CopyFrom(stream, args);
					}

					if (local.Length != file.Size)
					{
						throw new Exception("File reported as download, but is not complete.");
					}
				}

				Log.Info("Completed " + file.Name);

				DownloadComplete(new DownloadedEventArgs(new DownloadedFile {DownloadLocation = filePath, FtpListItem = file}, stats));

				return true;
			}

			catch (FtpException e)
			{
				DownloadError(new DownloadedEventArgs(new DownloadedFile { DownloadLocation = filePath, FtpListItem = file }));
				Log.Error("FTP error with " + file.Name);
				Log.Error(e.Message);
				TrackErrorCount(file);
				return false;
			}
			
			catch (NotSupportedException)
			{
				//The file exists, but doesn't support seeking to the end for resume.
				//Delete the existing file and try again from scratch.
				File.Delete(filePath);
				return DownloadFile(file);
			}

			catch (Exception e)
			{
				DownloadError(new DownloadedEventArgs(new DownloadedFile { DownloadLocation = filePath, FtpListItem = file }));
				Log.Error("Error with " + file.Name);
				Log.Error(e.Message);
				TrackErrorCount(file);
				return false;
			}
		}