Exemple #1
0
        public void UploadFile(TransferRecord record)
        {
            var defaultLocalPath  = _cfg.LocalPath;
            var defaultRemotePath = _cfg.RemotePath;

            TransferOptions transferOptions = new TransferOptions();

            transferOptions.TransferMode = TransferMode.Automatic;
            //switch (_cfg.ProtocolType)
            //{
            //    case FtpType.SFTP:
            //        //恢復續傳
            //        transferOptions.ResumeSupport.State = TransferResumeSupportState.On;
            //        break;
            //    case FtpType.FTP:
            //        //no way...
            //        break;
            //}
            //傳輸速度(KB/s)
            if (!(_cfg.SpeedLimit == 0))
            {
                transferOptions.SpeedLimit = _cfg.SpeedLimit;
            }

            Stopwatch sw = new Stopwatch();

            string filePath       = Path.Combine(defaultLocalPath, record.FileName);
            string remoteFilePath = Path.Combine(defaultRemotePath, record.FileName);

            // 嘗試次數
            record.ProcessTimes++;
            record.LocalFilePath  = filePath;
            record.RemoteFilePath = remoteFilePath;
            record.Status         = "20";

            Console.WriteLine($"檔名:{record.FileName}");
            Console.WriteLine($"本機路徑:{record.LocalFilePath}");
            Console.WriteLine($"遠端路徑:{record.RemoteFilePath}");

            log.Info($"檔名:{record.FileName}");
            log.Info($"本機路徑:{record.LocalFilePath}");
            log.Info($"遠端路徑:{record.RemoteFilePath}");

            //file exist or not
            if (File.Exists(filePath))
            {
                //上傳前驗證碼檢查
                record.GetFileSHA256();
                if (!record.ComparisonHash())
                {
                    record.Done         = false;
                    record.ErrorCode    = "SHA";
                    record.Status       = "21";
                    record.ErrorMessage = "檔案上傳失敗,訊息:檔案SHA256驗證碼失敗";
                    log.Error($"[失敗]{record.ErrorMessage}");
                    Console.WriteLine($"[失敗]{record.ErrorMessage}");
                    return;
                }

                log.Info("開始上傳");
                Console.WriteLine("開始上傳");
                sw.Start();
                var transferResult = _session.PutFiles(filePath, defaultRemotePath, false, transferOptions);
                sw.Stop();
                record.TransferElapsed = sw.Elapsed;

                //Throw the first failure in the list
                transferResult.Check();
                //檔案長度(位元組)
                record.TransferFileSize = _session.GetFileInfo(remoteFilePath).Length;
                record.CalAveRate();
                log.Info($"檔案大小(位元組):{record.TransferFileSize.ToString("#,#")}");
                log.Info($"均速(kB/s):{record.TransferSpeed.ToString("#,#")}");
                Console.WriteLine($"檔案大小(位元組):{record.TransferFileSize.ToString("#,#")}");
                Console.WriteLine($"均速(kB/s):{record.TransferSpeed.ToString("#,#")}");

                if (transferResult.IsSuccess)
                {
                    record.Done   = true;
                    record.Status = "30";
                    log.Info("[完成]");
                    Console.WriteLine("[完成]");
                }
                else
                {
                    record.Done      = false;
                    record.ErrorCode = "UpLoad";
                    record.Status    = "21";

                    var error = transferResult.Transfers.FirstOrDefault();
                    if (error != null)
                    {
                        record.ErrorMessage = $"檔案上傳失敗,訊息:{ error.Error.Message}";
                        log.Error($"[失敗]{record.ErrorMessage}");
                        Console.WriteLine($"[失敗]{record.ErrorMessage}");
                    }
                }
            }
            else
            {
                record.Done         = false;
                record.Status       = "21";
                record.ErrorMessage = $"檔案上傳失敗,訊息:檔案不存在";
                log.Error($"[失敗]{record.ErrorMessage}");
                Console.WriteLine($"[失敗]{record.ErrorMessage}");
            }
        }
Exemple #2
0
        /// <summary>下載</summary>
        /// <param name="record"></param>
        public void DownloadFile(TransferRecord record)
        {
            var defaultLocalPath  = _cfg.LocalPath;
            var defaultRemotePath = _cfg.RemotePath;

            TransferOptions transferOptions = new TransferOptions();

            transferOptions.TransferMode = TransferMode.Automatic;
            //恢復續傳
            transferOptions.OverwriteMode = OverwriteMode.Resume;

            //傳輸速度(kb/s)
            if (!(_cfg.SpeedLimit == 0))
            {
                transferOptions.SpeedLimit = _cfg.SpeedLimit;
            }

            Stopwatch sw = new Stopwatch();

            string filePath      = Path.Combine(defaultRemotePath, record.FileName);
            string localFilePath = Path.Combine(defaultLocalPath, record.FileName);

            // 嘗試次數
            record.ProcessTimes++;
            record.LocalFilePath  = localFilePath;
            record.RemoteFilePath = filePath;
            record.Status         = "40";

            log.Info($"檔名:{record.FileName}");
            log.Info($"本機路徑:{record.LocalFilePath}");
            log.Info($"遠端路徑:{record.RemoteFilePath}");

            if (_session.FileExists(filePath))
            {
                //取得檔案長度(位元組)
                record.TransferFileSize = _session.GetFileInfo(filePath).Length;
                log.Info("開始下載");
                sw.Start();
                var transferResult = _session.GetFiles(filePath, defaultLocalPath, false, transferOptions);
                sw.Stop();
                record.TransferElapsed = sw.Elapsed;

                /*
                 * transferResult.IsSuccess:Is true, if .failures is empty collection
                 * If there's a failure, the transferResult.Check() throws.
                 */
                transferResult.Check();
                record.CalAveRate();
                log.Info($"檔案大小(位元組):{record.TransferFileSize.ToString("#,#")}");
                log.Info($"均速(kB/s):{record.TransferSpeed.ToString("#,#")}");

                if (transferResult.IsSuccess)
                {
                    //下載後驗證碼檢查
                    record.GetFileSHA256();
                    if (record.ComparisonHash())
                    {
                        record.Done   = true;
                        record.Status = "50";
                        log.Info("[完成]");
                    }
                    else
                    {
                        record.Done         = false;
                        record.ErrorCode    = "SHA";
                        record.Status       = "41";
                        record.ErrorMessage = $"檔案下載失敗,訊息:檔案SHA256驗證碼失敗";
                        log.Error($"[失敗]{record.ErrorMessage}");
                        if (File.Exists(localFilePath))
                        {
                            File.Delete(localFilePath);
                        }
                    }
                }
                else
                {
                    // 加入錯誤訊息
                    record.Done      = false;
                    record.ErrorCode = "DownLoad";
                    record.Status    = "41";

                    var error = transferResult.Transfers.FirstOrDefault();
                    if (error != null)
                    {
                        record.ErrorMessage = $"檔案下載失敗,訊息:{ error.Error.Message}";
                        log.Error($"[失敗]{record.ErrorMessage}");
                    }
                }
            }
            else
            {
                record.Done         = false;
                record.Status       = "41";
                record.ErrorMessage = $"檔案下載失敗,訊息:檔案不存在";
                log.Error($"[失敗]{record.ErrorMessage}");
            }
        }