Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
        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;
                });
            }
        }