private string BackupFile(string filePath) { string relativFilePath = GetRelativFilePath(filePath); string ftpFilePath = FtpExtensions.GetFtpPath(Options.RemoteFolder, relativFilePath); Logger.Debug("fileToBackup: '{0}'", ftpFilePath); if (!client.FileExists(ftpFilePath)) { statistic.NotFoundFiles++; Logger.Debug("file not found on ftp"); return(null); } string backupFilePath = Path.Combine(Options.BackupFolder, relativFilePath.TrimStart(Path.DirectorySeparatorChar)); var ftpLocalExists = Options.OverwriteFile ? FtpLocalExists.Overwrite : FtpLocalExists.Skip; if (client.DownloadFile(backupFilePath, ftpFilePath, ftpLocalExists, FtpVerify.Retry | FtpVerify.Throw)) { statistic.BackupFiles++; Logger.Debug("backupFilePath: '{0}'", backupFilePath); } else { statistic.SkipFiles++; Logger.Debug("file exist in '{0}'", backupFilePath); } return(filePath); }
/// <summary> /// /// </summary> /// <param name="backupFile"></param> public void RestoreGameFile(BackupFile backupFile) { try { if (!File.Exists(backupFile.LocalPath)) { _ = DarkMessageBox.Show(this, "This file backup doesn't exist on your computer. If your game doesn't have mods installed, then I would suggest you backup the original files.", "No File Found", MessageBoxIcon.Information); return; } FtpExtensions.UploadFile(MainWindow.FtpConnection, backupFile.LocalPath, backupFile.InstallPath); _ = DarkMessageBox.Show(this, $"Successfully restored file: {backupFile.FileName} to path: {backupFile.InstallPath}", "Success", MessageBoxIcon.Information); } catch (Exception ex) { Program.Log.Error("There was an issue attempting to restore file.", ex); _ = DarkMessageBox.Show(this, "There was an issue restoring file. Make sure the local file exists on your computer.", "Error", MessageBoxIcon.Error); } }
/// <summary> /// </summary> /// <param name="backupFile"></param> public void BackupGameFile(BackupFile backupFile) { try { FtpExtensions.DownloadFile(backupFile.LocalPath, backupFile.InstallPath); _ = XtraMessageBox.Show($"Successfully backed up file {backupFile.FileName} from {backupFile.InstallPath}.", "Success"); } catch (Exception ex) { Program.Log.Error(ex, $"Unable to backup game file. Error: {ex.Message}"); _ = DarkMessageBox.ShowError("There was a problem downloading the file. Make sure the file exists on your console.", "Error"); } }
private FtpReply GetProxyReply(FtpSocketStream stream) { var reply = new FtpReply(); string buf; #if !CORE14 lock (Lock) { #endif if (!IsConnected) { throw new InvalidOperationException("No connection to the server has been established."); } stream.ReadTimeout = ReadTimeout; while ((buf = stream.ReadLine(Encoding)) != null) { Match m; LogLine(FtpTraceLevel.Info, buf); if ((m = Regex.Match(buf, @"^HTTP/.*\s(?<code>[0-9]{3}) (?<message>.*)$")).Success) { reply.Code = m.Groups["code"].Value; reply.Message = m.Groups["message"].Value; break; } reply.InfoMessages += buf + "\n"; } // fixes #84 (missing bytes when downloading/uploading files through proxy) while ((buf = stream.ReadLine(Encoding)) != null) { LogLine(FtpTraceLevel.Info, buf); if (FtpExtensions.IsNullOrWhiteSpace(buf)) { break; } reply.InfoMessages += buf + "\n"; } #if !CORE14 } #endif return(reply); }
/// <summary> /// Create/store a backup of the specified file, and then downloads it locally to a known path /// </summary> /// <param name="modItem"></param> /// <param name="fileName"></param> /// <param name="installFilePath"></param> public void CreateBackupFile(ModItem modItem, string fileName, string installFilePath) { var fileBackupFolder = GetGameBackupFolder(modItem); _ = Directory.CreateDirectory(fileBackupFolder); var backupFile = new BackupFile { CategoryId = modItem.GameId, FileName = fileName, LocalPath = Path.Combine(fileBackupFolder, fileName), InstallPath = installFilePath }; FtpExtensions.DownloadFile(backupFile.LocalPath, backupFile.InstallPath); BackupFiles.Add(backupFile); }
/// <summary> /// Return the user's game region, either automatically by searching existing console /// directories or prompt the user to select one. /// </summary> /// <param name="gameId"> Game Id </param> /// <returns> </returns> public string GetGameRegion(Form owner, string gameId) { switch (MainWindow.Settings.RememberGameRegions) { case true: { string gameRegion = MainWindow.Settings.GetGameRegion(gameId); switch (string.IsNullOrEmpty(gameRegion)) { case false: return(gameRegion); } break; } } switch (MainWindow.Settings.AutoDetectGameRegions) { case true: { List <string> foundRegions = Regions.Where(region => FtpExtensions.DirectoryExists($"/dev_hdd0/game/{region}")).ToList(); foreach (string region in foundRegions.Where(region => XtraMessageBox.Show($"Game Region: {region} has been found for: {Title}\n\nIs this correct?", "Game Region", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)) { return(region); } XtraMessageBox.Show( MainWindow.Window.LookAndFeel, "Could not find any regions on your console for this game title. You must install the game update for this title first.", "No Game Update", MessageBoxButtons.OK, MessageBoxIcon.Error); return(null); } default: ListItem selectedItem = DialogExtensions.ShowListViewDialog(owner, "Game Regions", Regions.ToList().ConvertAll(x => new ListItem { Value = x, Name = x })); return(selectedItem?.Value); } }
private async Task <FtpReply> GetProxyReplyAsync(FtpSocketStream stream, CancellationToken token = default(CancellationToken)) { FtpReply reply = new FtpReply(); string buf; if (!IsConnected) { throw new InvalidOperationException("No connection to the server has been established."); } stream.ReadTimeout = ReadTimeout; while ((buf = await stream.ReadLineAsync(Encoding, token)) != null) { Match m; this.LogLine(FtpTraceLevel.Info, buf); if ((m = Regex.Match(buf, @"^HTTP/.*\s(?<code>[0-9]{3}) (?<message>.*)$")).Success) { reply.Code = m.Groups["code"].Value; reply.Message = m.Groups["message"].Value; break; } reply.InfoMessages += (buf + "\n"); } // fixes #84 (missing bytes when downloading/uploading files through proxy) while ((buf = await stream.ReadLineAsync(Encoding, token)) != null) { this.LogLine(FtpTraceLevel.Info, buf); if (FtpExtensions.IsNullOrWhiteSpace(buf)) { break; } reply.InfoMessages += (buf + "\n"); } return(reply); }
public static WebFile CreateFile(string fileUrl) { return(new WebFile(Path.GetFileName(fileUrl), FtpExtensions.GetFileSize(fileUrl), FtpExtensions.GetFileLastModified(fileUrl), fileUrl)); }
private void ButtonFileRequestSize_Click(object sender, EventArgs e) { ButtonFileRequestSize.Visible = false; WorkerExtensions.RunWorkAsync(() => StringExtensions.BytesToPrefix(FtpExtensions.GetFileSize(SelectedGridFile.Url)), (data) => { LabelFileValueSize.Text = data; }); }
/// <summary> /// Download FTP files asynchronously. /// </summary> /// <param name="uris">List of files' URIs to download.</param> public async Task DownloadFilesAsync(string[] uris) { try { uris = uris.Where(s => new Uri(s).Scheme.Equals(Uri.UriSchemeFtp)).ToArray(); await Task.WhenAll(uris.Select(async uri => { Uri u = new Uri(uri); using (var client = new FtpClient(u.Host)) { client.ReadTimeout = Singleton.Settings.TimeoutSeconds * 1000; client.RetryAttempts = Singleton.Settings.TimeoutRetries; string[] s = u.UserInfo.Split(':'); if (s.Length == 2) { Console.WriteLine($"{uri}: Authenticating..."); client.Credentials = new NetworkCredential(s[0], s[1]); } await client.ConnectAsync(); if (await client.FileExistsAsync(u.LocalPath)) { Progress <FtpProgress> progress = new Progress <FtpProgress>(x => { if (x.Progress >= 0) { Console.WriteLine($"{uri}: {string.Format("{0:N2}% downloaded", x.Progress)}"); } }); await client.DownloadFileAsync(Singleton.Settings.DownloadLocation + "/" + FtpExtensions.GetFtpFileName(uri), u.LocalPath, FtpLocalExists.Append, FtpVerify.None, progress); } else { Console.WriteLine($"{uri}: File does not exist"); } await client.DisconnectAsync(); } })); } catch (Exception e) { throw e; } }