Ejemplo n.º 1
0
		private static bool ShouldUpload(OverwriteCondition overwriteCondition, LocalFileInfo localFileInfo, FtpFileInfo ftpFileInfo)
		{
			bool shouldUpload;

			switch (overwriteCondition)
			{
				case OverwriteCondition.Always:
					shouldUpload = true;
					break;
				case OverwriteCondition.DifferentSize:
					shouldUpload = ftpFileInfo.Length != localFileInfo.Length;
					break;
				case OverwriteCondition.DifferentSizeOrMoreRecentLocalFile:
					shouldUpload = ftpFileInfo.Length != localFileInfo.Length || ftpFileInfo.LastWriteTimeUtc < localFileInfo.LastWriteTimeUtc;
					break;
				case OverwriteCondition.MoreRecentLocalFile:
					shouldUpload = ftpFileInfo.LastWriteTimeUtc < localFileInfo.LastWriteTimeUtc;
					break;
				case OverwriteCondition.Never:
					shouldUpload = !ftpFileInfo.Exists;
					break;
				default:
					throw new NotSupportedException(string.Format("Overwrite Condition {0} not supported", overwriteCondition));
			}

			return shouldUpload;
		}
Ejemplo n.º 2
0
		/// <summary>
		/// Uploads a local file to a remote FTP server.
		/// </summary>
		/// <param name="localFile">The local file to upload.</param>
		/// <param name="remoteUri">The FTP URI of the destination.</param>
		/// <param name="overwriteCondition">Defines the condition under which the remote file will be overwritten if it exists.
		/// Default value if <see cref="OverwriteCondition.DifferentSizeOrMoreRecentLocalFile"/>.</param>
		public FtpStatusCode UploadFile(string localFile, string remoteUri, OverwriteCondition overwriteCondition)
		{
			LocalFileInfo localFileInfo = new LocalFileInfo(localFile);
			FtpFileInfo ftpFileInfo = new FtpFileInfo(new FtpWebRequestFactory { Username = Username, Password = Password }, remoteUri, Logger);

			Logger.Info(string.Format("UploadFile. {0} to {1}, overwrite condition={2}", localFileInfo.Name, remoteUri, overwriteCondition));

			if (!ShouldUpload(overwriteCondition, localFileInfo, ftpFileInfo))
			{
				return FtpStatusCode.CommandOK;
			}

			FtpStatusCode result = localFileInfo.CopyTo(ftpFileInfo);

			Logger.Info(string.Format("Uploaded {0} to {1}, result {2}", localFileInfo.Name, remoteUri, (int)result));

			return result;
		}