예제 #1
0
파일: SFTPUtils.cs 프로젝트: chanhappy/.Net
        private FtpStatusCode UploadFile(SFTPUploadArgument uploadArgument)
        {
            m_Logger.Debug($"uploadArgument:{uploadArgument}");

            Requires.NotNull(uploadArgument, "uploadArgument");
            Requires.NotNullOrEmpty(uploadArgument.RemoteHost, "remoteHost");
            Requires.NotNullOrEmpty(uploadArgument.LocalPathWithFileName, "localPathWithFileName");
            Requires.NotNullOrEmpty(uploadArgument.BaseDirectory, "baseDirectory");
            Requires.NotNullOrEmpty(uploadArgument.TargetPath, "targetPath");
            Requires.NotNullOrEmpty(uploadArgument.UserName, "userName");
            Requires.NotNullOrEmpty(uploadArgument.Password, "password");
            _sftpClient = GetInstance(uploadArgument.RemoteHost, uploadArgument.UserName, uploadArgument.Password);
            var sftpResult = _sftpClient.UploadFile(uploadArgument.IsNeedCheckDirectory, uploadArgument.LocalPathWithFileName, uploadArgument.BaseDirectory, uploadArgument.TargetPath, uploadArgument.UpFileName);

            if (!string.IsNullOrEmpty(uploadArgument.NeedChage) && uploadArgument.NeedChage != "0")
            {
                short    newPermission = Convert.ToInt16(uploadArgument.Permission);
                string   _baseDir      = uploadArgument.BaseDirectory + "/";
                string[] paths         = uploadArgument.TargetPath.Split('/');
                foreach (string path in paths)
                {
                    _baseDir = _baseDir + path + "/";
                    _sftpClient.changePermission(_baseDir, newPermission);
                }
                //  如果未传入上传后的文件名,使用源文件的名称
                string fileName = string.IsNullOrWhiteSpace(uploadArgument.UpFileName) ? Path.GetFileName(uploadArgument.LocalPathWithFileName) : uploadArgument.UpFileName;
                _sftpClient.changePermission(_baseDir + "/" + fileName, newPermission);
            }
            return(sftpResult);
        }
예제 #2
0
파일: SFTPUtils.cs 프로젝트: chanhappy/.Net
 public void UploadFiles(SFTPUploadArgument[] uploadArguments, string host, string username, string password, CallbackContext callbackContext)
 {
     try
     {
         Requires.NotNullOrEmpty(host, "remoteHost");
         Requires.NotNullOrEmpty(username, "userName");
         Requires.NotNullOrEmpty(password, "password");
         foreach (SFTPUploadArgument uploadArgument in uploadArguments)
         {
             m_Logger.Debug($"uploadArgument:{uploadArgument}");
             Requires.NotNull(uploadArgument, "uploadArgument");
             Requires.NotNullOrEmpty(uploadArgument.LocalPathWithFileName, "localPathWithFileName");
             Requires.NotNullOrEmpty(uploadArgument.BaseDirectory, "baseDirectory");
             Requires.NotNullOrEmpty(uploadArgument.TargetPath, "targetPath");
         }
         List <FtpStatusCode> result = new List <FtpStatusCode>();
         _sftpClient = GetInstance(host, username, password);
         foreach (SFTPUploadArgument uploadArgument in uploadArguments)
         {
             var sftpResult = _sftpClient.UploadFile(uploadArgument.IsNeedCheckDirectory, uploadArgument.LocalPathWithFileName, uploadArgument.BaseDirectory, uploadArgument.TargetPath, uploadArgument.UpFileName);
             if (!string.IsNullOrEmpty(uploadArgument.NeedChage) && uploadArgument.NeedChage != "0")
             {
                 short    newPermission = Convert.ToInt16(uploadArgument.Permission);
                 string   _baseDir      = uploadArgument.BaseDirectory + "/";
                 string[] paths         = uploadArgument.TargetPath.Split('/');
                 foreach (string path in paths)
                 {
                     _baseDir = _baseDir + path + "/";
                     _sftpClient.changePermission(_baseDir, newPermission);
                 }
                 _sftpClient.changePermission(_baseDir + uploadArgument.UpFileName, newPermission);
             }
             result.Add(sftpResult);
         }
         callbackContext.SendPluginResult(new PluginResult(PluginResult.Status.OK, result));
     }
     catch (Exception ex)
     {
         m_Logger.Error(ex);
         callbackContext.Error(new
         {
             type    = "Exception",
             code    = "",
             message = ex.Message,
             details = ex.StackTrace
         });
     }
 }
예제 #3
0
        private bool SFTPUpload_UploadFile(string fileName, string localFileFullPath, string remoteFullPath, ref bool overwite)
        {
            if (IsFileTransferCanceled())
            {
                Log("Canceled");
                return(false);   // cancel
            }

            if (!overwite)
            {
                bool existence;
                try {
                    _sftp.GetFileInformations(remoteFullPath, true);
                    existence = true;
                }
                catch (SFTPClientException e) {
                    if (!IsSFTPError(e, SFTPStatusCode.SSH_FX_NO_SUCH_FILE))
                    {
                        throw;
                    }
                    existence = false;
                }

                if (existence)
                {
                    DialogResult result = DialogResult.None;
                    this.Invoke((MethodInvoker) delegate() {
                        string caption = SFTPPlugin.Instance.StringResource.GetString("SFTPForm.Confirmation");
                        string format  = SFTPPlugin.Instance.StringResource.GetString("SFTPForm.AskOverwriteFormat");
                        string message = String.Format(format, remoteFullPath);

                        using (YesNoAllDialog dialog = new YesNoAllDialog(message, caption)) {
                            result = dialog.ShowDialog(this);
                        }
                    });

                    if (result == DialogResult.Cancel)
                    {
                        Log("Canceled");
                        return(false);   // cancel
                    }
                    if (result == DialogResult.No)
                    {
                        Log(" | Skipped: " + localFileFullPath);
                        return(true);    // skip
                    }
                    if (result == YesNoAllDialog.YesToAll)
                    {
                        overwite = true;
                    }
                }
            }

            FileInfo localFileInfo = new FileInfo(localFileFullPath);
            ulong    fileSize      = (ulong)localFileInfo.Length;

            _sftp.UploadFile(localFileFullPath, remoteFullPath, _fileTransferCancellation,
                             delegate(SFTPFileTransferStatus status, ulong transmitted) {
                ShowProgress(localFileFullPath, fileName, status, fileSize, transmitted);
            });

            if (IsFileTransferCanceled())
            {
                return(false);   // canceled
            }
            else
            {
                return(true);
            }
        }