Пример #1
0
        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
 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
         });
     }
 }