public virtual FileInfo DownloadFile(string url, string filename) { if (string.IsNullOrWhiteSpace(url)) { throw new ArgumentNullException(nameof(url)); } if (string.IsNullOrWhiteSpace(RequestEmail) || !RequestEmail.Contains('@') || RequestEmail.Length < 10) { Log.WriteLine("Internal error. Cannot request FTP content because no contact email is provided.", LogLevel.Error); throw new InvalidOperationException("No valid requesting contact email provided. Please set the RequestEmail to continue. Cant comply."); } FileInfo localFile = new FileInfo(Path.Combine(CacheDirectory, filename)); if (!localFile.Directory.Exists) { localFile.Directory.Create(); } if (OfflineMode) { localFile.Refresh(); if (!localFile.Exists) { throw new FileNotFoundException("The downloaded file cannot be located. Application is in offline mode."); } Log.WriteLine("Offline mode. No file download for %@. Cached file size: %@ bytes", LogLevel.Message, localFile.Name, localFile.Length.ToString("N0")); return(localFile); } if (FtpClient == null) { FtpClient = new FluentFTP.FtpClient(PublicFtpHostname, PublicFtpPort, new System.Net.NetworkCredential("anonymous", RequestEmail)); FtpClient.ConnectTimeout = 7000; FtpClient.DataConnectionConnectTimeout = 7000; FtpClient.DataConnectionReadTimeout = 10000; FtpClient.ReadTimeout = 7000; } else { Log.WriteLine("Reusing existing FTP connection to ftp://%@@%@:%@", LogLevel.Debug, FtpClient.Credentials.UserName, PublicFtpHostname, PublicFtpPort.ToString()); } int tryIndex = 0; int maxTries = 10; int tryDelayFormulaBase = 2; bool abortLoop = false; while (!FtpClient.IsConnected) { try { if (tryIndex >= maxTries) { abortLoop = true; throw new IOException("Cannot connect to the remote server."); } int delay = tryIndex > 0 ? (int)Math.Pow(tryDelayFormulaBase, tryIndex) : 0; if (delay > 0) { if (Settings.Current.HeadlessMode) { Log.Write("Retrying in %@ seconds... ", LogLevel.Message, delay.ToString("N0")); System.Threading.Thread.Sleep(delay * 1000); } else { Log.Write("Retrying in %@ seconds. Press Q at any time to abort. ", LogLevel.Message, delay.ToString("N0")); for (int i = 0; i < delay; i++) { System.Threading.Thread.Sleep(1000); while (Console.KeyAvailable) { if (Console.ReadKey().Key == ConsoleKey.Q) { abortLoop = true; throw new TimeoutException("The operation timed out and a retry attempt was aborted"); } } } } Log.WriteLine("Retrying..."); } if (tryIndex > 0) { Log.Write("Try %@ of %@. ", LogLevel.Message, tryIndex + 1, maxTries); } tryIndex++; Log.Write("Connecting to the remote FTP host (%@) at ftp://%@@%@:%@... ", LogLevel.Message, Identifier, FtpClient.Credentials.UserName, PublicFtpHostname, PublicFtpPort.ToString()); FtpClient.RetryAttempts = 1; FluentFTP.FtpProfile profile = FtpClient.AutoConnect(); if (profile == null || !FtpClient.IsConnected) { throw new TimeoutException("The operation timed out."); } Log.WriteColoredLine("OK! (%@)", ConsoleColor.Green, LogLevel.Message, profile.DataConnection.ToString()); } catch (Exception ex) { Log.WriteColoredLine("FAIL!", ConsoleColor.Red, LogLevel.Error); Log.WriteLine("Failed to connect. %@. %@", LogLevel.Error, ex.GetType().Name, ex.Message); if (abortLoop) { throw; } } } Log.WriteLine("Connected to ftp://%@@%@:%@/. Server Type: %@", LogLevel.Debug, FtpClient.Credentials.UserName, PublicFtpHostname, PublicFtpPort.ToString(), FtpClient.SystemType); Log.WriteLine("Requesting file info for %@...", LogLevel.Debug, url); try { FluentFTP.FtpListItem remoteFileInfo = FtpClient.GetObjectInfo(url); if (remoteFileInfo == null) { throw new IOException("The requested FTP file does not exists."); } FtpCurrentTransferSize = FtpClient.GetFileSize(url); Log.WriteLine(" File size: %@ bytes.", LogLevel.Debug, FtpCurrentTransferSize.ToString("N0")); if (FtpCurrentTransferSize == 0) { //FtpClient.Disconnect(); throw new IOException("The requested file is empty."); } } catch (Exception ex) { Log.WriteLine("An error occurred while requesting file info. %@. %@", LogLevel.Warning, ex.GetType().Name, ex.Message); throw; } try { using (FileStream fs = new FileStream(localFile.FullName, FileMode.Create, FileAccess.Write, FileShare.None)) { FtpClient.Download(fs, url, progress: FtpTransferProgressUpdated); if (Console.CursorLeft > 0) { Log.WriteLine(); } if (fs.Position == FtpCurrentTransferSize || FtpCurrentTransferSize == -1) { Log.WriteLine("File transfer completed. %@ bytes received.", LogLevel.Debug, fs.Position.ToString("N0")); } else { Log.WriteLine("File transfer completed but file size differs!!. Expected %@ but received %@ bytes.", LogLevel.Error, FtpCurrentTransferSize.ToString("N0"), fs.Position.ToString("N0")); if (!Settings.Current.IgnoreFtpTransferSizeMismatch) { //FtpClient.Disconnect(); throw new IOException("The received file doesnt match the expected file length."); } } } } catch (Exception ex) { Log.WriteLine("An error occurred while requesting the file. %@. %@. Aborted.", LogLevel.Error, ex.GetType().Name, ex.Message); throw; } //FtpClient.Disconnect(); //Log.WriteLine("Disconnected from FTP server.", LogLevel.Debug); localFile.Refresh(); return(localFile); }
protected void btnSave_Click(object sender, EventArgs e) { int UserID = 0; int intCampaignID = 0; // lblCurrentCampaign.Text.ToString().ToInt32(); int.TryParse(ddlCampaign.SelectedValue, out intCampaignID); if (Session["CampaignPlayerRoleID"] == null) { Session["CampaignPlayerRoleID"] = 0; } int CampaignPlayerRoleID = Session["CampaignPlayerRoleID"].ToString().ToInt32(); string strUsername = ""; string RequestEmail; if (Session["Username"] != null) { strUsername = ""; } else { strUsername = Session["Username"].ToString(); } if (Session["UserID"].ToString().ToInt32() != 0) { UserID = Session["UserID"].ToString().ToInt32(); } Classes.cCampaignBase Campaign = new Classes.cCampaignBase(intCampaignID, strUsername, UserID); if (Campaign.JoinRequestEmail == "") { RequestEmail = Campaign.InfoRequestEmail; } else { RequestEmail = Campaign.JoinRequestEmail; } if (RequestEmail.Contains("@")) { // It has a "@". Assume the email format is close enough, go on. } else { RequestEmail = ""; } foreach (ListItem item in cblRole.Items) { if (item.Selected) { switch (item.Value) { case "NPC": // Permanent NPC needs approval if (RequestEmail == "") { SignUpForSelectedRole(6, UserID, intCampaignID, 55); } else { SendApprovalEmail(intCampaignID, UserID, 6, RequestEmail); SignUpForSelectedRole(10, UserID, intCampaignID, 56); } Response.Redirect("~/General/MemberHome.aspx"); break; case "6False": // Permanent NPC no approval SignUpForSelectedRole(6, UserID, intCampaignID, 55); break; case "EventNPC": // Event NPC needs approval if (RequestEmail == "") { SignUpForSelectedRole(7, UserID, intCampaignID, 55); } else { SendApprovalEmail(intCampaignID, UserID, 7, RequestEmail); SignUpForSelectedRole(10, UserID, intCampaignID, 56); } Response.Redirect("~/General/MemberHome.aspx"); break; case "7False": // Event NPC no approval SignUpForSelectedRole(7, UserID, intCampaignID, 55); break; case "PC": // PC needs approval if (RequestEmail == "") { SignUpForSelectedRole(8, UserID, intCampaignID, 55); } else { SendApprovalEmail(intCampaignID, UserID, 8, RequestEmail); SignUpForSelectedRole(8, UserID, intCampaignID, 56); } Response.Redirect("~/General/MemberHome.aspx"); break; case "8False": // PC no approval SignUpForSelectedRole(8, UserID, intCampaignID, 55); break; case "10True": // NPC needs approval if (RequestEmail == "") { SignUpForSelectedRole(10, UserID, intCampaignID, 55); } else { SendApprovalEmail(intCampaignID, UserID, 10, RequestEmail); SignUpForSelectedRole(10, UserID, intCampaignID, 56); } break; case "10False": // NPC no approval SignUpForSelectedRole(10, UserID, intCampaignID, 55); break; default: // Technically we shouldn't be able to get here so do nothing Response.Redirect("~/General/MemberHome.aspx"); break; } } } Response.Redirect("~/General/MemberHome.aspx"); }
protected void btnSave_Click(object sender, EventArgs e) { int intCampaignID = 0; int.TryParse(ddlCampaign.SelectedValue, out intCampaignID); string RequestEmail; int iUserID = 0; if (!int.TryParse(hidUserID.Value, out iUserID)) { iUserID = 0; } Classes.cCampaignBase Campaign = new Classes.cCampaignBase(intCampaignID, hidUsername.Value, iUserID); if (Campaign.JoinRequestEmail == "") { RequestEmail = Campaign.InfoRequestEmail; } else { RequestEmail = Campaign.JoinRequestEmail; } if (RequestEmail.Contains("@")) { // It has a "@". Assume the email format is close enough, go on. } else { RequestEmail = ""; } foreach (ListItem item in cblRole.Items) { if (item.Selected) { switch (item.Value) { case "NPC": // Permanent NPC needs approval if (RequestEmail == "") { SignUpForSelectedRole(6, iUserID, intCampaignID, 55); } else { SendApprovalEmail(intCampaignID, iUserID, 6, RequestEmail); SignUpForSelectedRole(10, iUserID, intCampaignID, 56); } Response.Redirect("~/Default.aspx"); break; case "6False": // Permanent NPC no approval SignUpForSelectedRole(6, iUserID, intCampaignID, 55); break; case "EventNPC": // Event NPC needs approval if (RequestEmail == "") { SignUpForSelectedRole(7, iUserID, intCampaignID, 55); } else { SendApprovalEmail(intCampaignID, iUserID, 7, RequestEmail); SignUpForSelectedRole(10, iUserID, intCampaignID, 56); } Response.Redirect("~/Default.aspx"); break; case "7False": // Event NPC no approval SignUpForSelectedRole(7, iUserID, intCampaignID, 55); break; case "PC": // PC needs approval if (RequestEmail == "") { SignUpForSelectedRole(8, iUserID, intCampaignID, 55); } else { SendApprovalEmail(intCampaignID, iUserID, 8, RequestEmail); SignUpForSelectedRole(8, iUserID, intCampaignID, 56); } Response.Redirect("~/Default.aspx"); break; case "8False": // PC no approval SignUpForSelectedRole(8, iUserID, intCampaignID, 55); break; case "10True": // NPC needs approval if (RequestEmail == "") { SignUpForSelectedRole(10, iUserID, intCampaignID, 55); } else { SendApprovalEmail(intCampaignID, iUserID, 10, RequestEmail); SignUpForSelectedRole(10, iUserID, intCampaignID, 56); } break; case "10False": // NPC no approval SignUpForSelectedRole(10, iUserID, intCampaignID, 55); break; default: // Technically we shouldn't be able to get here so do nothing Response.Redirect("~/Default.aspx"); break; } } } Response.Redirect("~/Default.aspx"); }