Пример #1
0
 /// <summary>
 /// Abort the transfer.
 /// </summary>
 public void Abort()
 {
     if (_sftp.Connected)
     {
         _sftp.Cancel();
     }
 }
Пример #2
0
        private async Task SFTP(string url, string localpath)
        {
            bool iscanceled = false;

            string[] split    = url.Split(':');
            string[] split2   = split[1].Split(new char[] { '/' }, 4);
            string   ftphost  = split2[2];
            string   fileName = split2[3].Substring(split2[3].LastIndexOf("/") + 1, (split2[3].Length - split2[3].LastIndexOf("/") - 1));
            string   folder   = split2[3].Remove(split2[3].Length - fileName.Length);

            string login    = Properties.Settings.Default.DataServerLogin;
            string password = MainHandler.Decode(Properties.Settings.Default.DataServerPass);

            lastDownloadFileName = fileName;

            string ftpfilepath = "/" + folder + fileName;

            filesToDownload.Add(localpath);
            numberOfActiveParallelDownloads++;

            if (!File.Exists(localpath))
            {
                DownloadStatus dl = new DownloadStatus();
                dl.File    = localpath;
                dl.percent = 0.0;
                dl.active  = true;
                statusOfDownloads.Add(dl);

                Sftp sftp = new Sftp(ftphost, login, password);
                try
                {
                    sftp.OnTransferProgress += (src, dst, transferredBytes, totalBytes, message) =>
                    {
                        dl.percent = ((double)transferredBytes / (double)totalBytes) * 100.0;
                        control.Dispatcher.BeginInvoke(new Action <DownloadStatus>(UpdateOnDownload), DispatcherPriority.Normal, dl);
                    };

                    Action EmptyDelegate = delegate() { };
                    control.ShadowBoxText.Text               = "Connecting to Server...";
                    control.ShadowBox.Visibility             = Visibility.Visible;
                    control.shadowBoxCancelButton.Visibility = Visibility.Visible;
                    control.UpdateLayout();
                    control.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);
                    sftp.Connect();

                    CancellationToken token = tokenSource.Token;

                    await Task.Run(() =>
                    {
                        if (sftp.Connected)
                        {
                            token.Register(() => { sftp.Cancel(); iscanceled = true; while (sftp.Connected)
                                                   {
                                                       Thread.Sleep(100);
                                                   }
                                                   CanceledDownload(); return; });
                            if (!iscanceled)
                            {
                                try
                                {
                                    sftp.Get(ftpfilepath, localpath);
                                    sftp.Close();
                                }
                                catch
                                {
                                    sftp.Cancel();
                                    sftp.Close();
                                }
                            }
                        }
                    }, token);
                }
                catch
                {
                    if (null != sftp && sftp.Connected)
                    {
                        sftp.Cancel();
                    }
                    MessageBox.Show("Can't login to data server, not authorized!");
                }
            }

            if (!iscanceled)
            {
                await control.Dispatcher.BeginInvoke(new Action <string>(FinishedDownload), DispatcherPriority.Normal, localpath);
            }
        }
Пример #3
0
 public void Cancel()
 {
     m_sshCp.Cancel();
 }
Пример #4
0
        private async Task SFTPDownloadFiles(string ftphost, string folder, string db, string sessionid, string fileName, string login, string password)
        {
            bool   iscanceled    = false;
            string localfilepath = Properties.Settings.Default.DatabaseDirectory + "\\" + db + "\\" + sessionid + "\\";

            lastdlfile = fileName;

            string ftpfilepath = "/" + folder + fileName;
            string localpath   = localfilepath + fileName;

            if (!localpath.EndsWith(".stream~"))
            {
                filesToDownload.Add(localpath);
            }
            numberOfParallelDownloads++;

            if (!File.Exists(localfilepath + fileName))
            {
                DownloadStatus dl = new DownloadStatus();
                dl.File    = localpath;
                dl.percent = "0.00";
                dl.active  = true;
                downloads.Add(dl);

                Sftp sftp = new Sftp(ftphost, login, password);
                try
                {
                    sftp.OnTransferProgress += new FileTransferEvent(SFTPonTransferProgress);
                    await control.Dispatcher.BeginInvoke(new Action <string>(SFTPConnect), DispatcherPriority.Normal, "");

                    Console.WriteLine("Connecting...");
                    sftp.Connect();
                    Console.WriteLine("OK");

                    Directory.CreateDirectory(Properties.Settings.Default.DatabaseDirectory + "\\" + db + "\\" + sessionid);
                    CancellationToken token = tokenSource.Token;

                    await Task.Run(() =>
                    {
                        if (sftp.Connected)
                        {
                            token.Register(() => { sftp.Cancel(); iscanceled = true; while (sftp.Connected)
                                                   {
                                                       Thread.Sleep(100);
                                                   }
                                                   CanceledDownload(localpath); return; });
                            if (!iscanceled)
                            {
                                try
                                {
                                    Console.WriteLine("Downloading...");
                                    sftp.Get(ftpfilepath, localfilepath);
                                    Console.WriteLine("Disconnecting...");
                                    sftp.Close();
                                }
                                catch
                                {
                                    Console.WriteLine("Disconnecting on cancel..");
                                    sftp.Cancel();
                                    sftp.Close();
                                }
                            }
                        }
                    }, token);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    if (null != sftp && sftp.Connected)
                    {
                        sftp.Cancel();
                    }
                    MessageBox.Show("Can't login to Data Server. Not authorized. Continuing without media.");
                }
            }
            else
            {
                Console.WriteLine("File " + fileName + " already exists, skipping download");
            }

            if (!iscanceled)
            {
                await control.Dispatcher.BeginInvoke(new Action <string>(SFTPUpdateOnTransferEnd), DispatcherPriority.Normal, "");
            }
        }