protected FtpClient(FtpClientConfiguration config) { if(config == null) { throw new Exception("The FTP Configuration cannot be null."); } var builder = new UriBuilder(config.Uri); var targetFile = StripLeadingAndTrailingSlashes(config.TargetFileName); if (config.AppendExecutionDateToFileName) { int idx = targetFile.LastIndexOf("."); if (idx != -1) { targetFile = String.Format("{0}{1}.{2}", targetFile.Substring(0, idx - 1), DateTime.Now.ToString(config.DateTimeFormatToAppend), targetFile.Substring(idx + 1)); } else { targetFile = String.Format("{0}{1}", targetFile, DateTime.Now.ToString(config.DateTimeFormatToAppend)); } } if(!String.IsNullOrEmpty(config.FtpFolder)) { var ftpFolder = StripLeadingAndTrailingSlashes(config.FtpFolder); builder.Path = String.Format("{0}/{1}", ftpFolder, targetFile); } else { builder.Path = targetFile; } _ftpWebRequest = WebRequest.Create(builder.Uri) as FtpWebRequest; if(_ftpWebRequest == null) { throw new Exception("There was a problem creating the FtpWebRequest."); } _ftpWebRequest.Credentials = new NetworkCredential(config.UserName, config.Password); _ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile; _ftpWebRequest.KeepAlive = config.KeepAlive; _ftpWebRequest.EnableSsl = config.EnableSsl; _ftpWebRequest.UseBinary = config.UseBinary; _ftpWebRequest.UsePassive = config.UsePassive; }
public FtpDeliveryExtension() { _settingsProvider = new FtpClientConfiguration(); }
public static FtpClientConfiguration GetFtpConfigurationFromSettings(Setting[] settings) { var settingsList = settings.ToList(); var settingNames = settingsList.ToList().Select(s => s.Name); if (!settingNames.Contains("Password")) { throw new Exception("Password is a required setting."); } if (!settingNames.Contains("UserName")) { throw new Exception("UserName is a required setting."); } if (!settingNames.Contains("Uri")) { throw new Exception("Uri is a required setting."); } if (!settingNames.Contains("TargetFileName")) { throw new Exception("Target File Name is a required setting."); } if (!settingNames.Contains("FileFormat")) { throw new Exception("File Format is a required setting."); } var config = new FtpClientConfiguration(); config.EnableSsl = settingNames.Contains("EnableSsl") ? Convert.ToBoolean(settingsList.ToList().First(x => x.Name == "EnableSsl").Value) : false; config.KeepAlive = settingNames.Contains("KeepAlive") ? Convert.ToBoolean(settingsList.ToList().First(x => x.Name == "KeepAlive").Value) : false; config.UseBinary = settingNames.Contains("UseBinary") ? Convert.ToBoolean(settingsList.ToList().First(x => x.Name == "UseBinary").Value) : true; config.UsePassive = settingNames.Contains("UsePassive") ? Convert.ToBoolean(settingsList.ToList().First(x => x.Name == "UsePassive").Value) : false; config.AppendExecutionDateToFileName = settingNames.Contains("AppendExecutionDateToFileName") ? Convert.ToBoolean(settingsList.ToList().First(x => x.Name == "AppendExecutionDateToFileName").Value) : false; config.FtpPort = settingNames.Contains("FtpPort") ? Convert.ToInt32(settingsList.ToList().First(x => x.Name == "FtpPort").Value) : 21; config.FtpFolder = settingNames.Contains("FtpFolder") ? settingsList.ToList().First(x => x.Name == "FtpFolder").Value : String.Empty; config.DateTimeFormatToAppend = settingNames.Contains("DateTimeFormatToAppend") ? settingsList.ToList().First(x => x.Name == "DateTimeFormatToAppend").Value : String.Empty; config.Password = settingsList.ToList().First(x => x.Name == "Password").Value; config.TargetFileName = settingsList.ToList().First(x => x.Name == "TargetFileName").Value; config.Uri = settingsList.ToList().First(x => x.Name == "Uri").Value; config.UserName = settingsList.ToList().First(x => x.Name == "UserName").Value; config.FileFormat = settingsList.ToList().First(x => x.Name == "FileFormat").Value; return config; }
public static FtpClient GetClientFromSettings(FtpClientConfiguration ftpClientConfiguration) { return new FtpClient(ftpClientConfiguration); }