示例#1
0
        /// <summary>
        /// ftp transfer
        /// </summary>
        /// <param name="content">ftp content</param>
        /// <param name="sftpSetting">ftp setting</param>
        /// <returns></returns>
        bool FTPTransfer(string content, SFTPSetting sftpSetting)
        {
            try
            {
                _logger.LogInformation($"ftp begin");
                using (var ftp = new Ftp())
                {
                    ftp.Connect(sftpSetting.Host);
                    ftp.Login(sftpSetting.UserName, sftpSetting.Password);
                    ftp.Mode = FtpMode.Active;
                    ftp.ChangeFolder(sftpSetting.TransferDirectory);
                    var encoding = Encoding.GetEncoding(sftpSetting.FileEncoding);
                    var response = ftp.Upload($"{sftpSetting.TransferFilePrefix}{sftpSetting.FileName}", 0, encoding.GetBytes(content));
                    if (response.Code.HasValue && (response.Code.Value == 226 || response.Code.Value == 200))
                    {
                        ftp.Close();
                        _logger.LogInformation($"ftp uplodad success");
                        return(true);
                    }
                    else
                    {
                        _logger.LogError($"ftp uplodad failure,because:{response.Message}");
                        ftp.Close();
                        return(false);
                    }
                }
            }
            catch (Exception exc)
            {
                _logger.LogCritical(exc, $"ftp uplodad failure:{exc.Message}");

                return(false);
            }
        }
示例#2
0
        /// <summary>
        /// sftp transfer
        /// </summary>
        /// <param name="content">sftp content</param>
        /// <param name="sftpSetting">sftpsetting</param>
        /// <returns></returns>
        bool SFTPTransfer(string content, SFTPSetting sftpSetting)
        {
            _logger.LogInformation("sftp begin");
            var authMethods = new List <AuthenticationMethod>();

            if (!string.IsNullOrEmpty(sftpSetting.Password?.Trim()))
            {
                authMethods.Add(new PasswordAuthenticationMethod(sftpSetting.UserName, sftpSetting.Password));
            }
            if (!string.IsNullOrEmpty(sftpSetting?.CertificatePath?.Trim()))
            {
                var pathToExe         = Process.GetCurrentProcess().MainModule.FileName;
                var pathToContentRoot = Path.GetDirectoryName(pathToExe);
                authMethods.Add(new PrivateKeyAuthenticationMethod(sftpSetting.UserName, new PrivateKeyFile(pathToContentRoot + sftpSetting.CertificatePath)));
            }
            var connectionInfo = new ConnectionInfo(sftpSetting.Host, sftpSetting.Port,
                                                    sftpSetting.UserName,
                                                    authMethods.ToArray()
                                                    );

            using (var client = new SftpClient(connectionInfo))
            {
                client.Connect();
                //create directory and enter this directory
                var dirArr = sftpSetting.TransferDirectory.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
                var mark   = true;
                foreach (var dir in dirArr)
                {
                    if (!client.Exists(dir) && mark)
                    {
                        client.CreateDirectory(dir);
                    }
                    else
                    {
                        mark = false;
                    }
                    client.ChangeDirectory(dir);
                }
                var encoding       = Encoding.GetEncoding(sftpSetting.FileEncoding);
                var attachmentArry = encoding.GetBytes(content);
                var stream         = new MemoryStream(attachmentArry);
                try
                {
                    client.UploadFile(stream, $"{sftpSetting.TransferFilePrefix}{sftpSetting.FileName}", (result) =>
                    {
                        if (result < 0)
                        {
                            _logger.LogError($"sftp failure:result={result}");
                        }
                    });
                }
                catch (Exception exc)
                {
                    _logger.LogCritical(exc, $"sftp failure:{exc.Message}");
                    return(false);
                }
            }
            _logger.LogInformation($"sftp success");
            return(true);
        }
        /// <summary>
        /// add sftpsetting
        /// </summary>
        /// <param name="sFTPSetting">sftpsetting</param>
        /// <returns></returns>
        public bool AddSFTPSetting(SFTPSetting sFTPSetting)
        {
            var sql = @"INSERT INTO sftpettings
		(keyname,host,port,username,password,certificatepath,transferdirectory,transferfileprefix,filename,fileencoding,validate,createon)VALUES
        (@keyname,@host,@port,@username,@password,@certificatepath,@transferdirectory,@transferfileprefix,@filename,@fileencoding,@validate,@createon)";

            using (var con = new NpgsqlConnection(_connectionString))
            {
                sFTPSetting.CreateOn = DateTime.Now;
                return(con.Execute(sql, sFTPSetting) > 0);
            }
        }
        /// <summary>
        /// modify sftpsetting
        /// </summary>
        /// <param name="sFTPSetting">sftpsetting</param>
        /// <returns></returns>
        public bool ModifySFTPSetting(SFTPSetting sFTPSetting)
        {
            var sql = @"UPDATE sftpettings
	SET  keyname=@keyname,host =@host,port = @port,username = @username,password = @password,certificatepath =@certificatepath
		,transferdirectory = @transferdirectory,transferfileprefix = @transferfileprefix,filename=@filename,fileencoding=@fileencoding,validate = @validate	
	WHERE id=@id"    ;

            using (var con = new NpgsqlConnection(_connectionString))
            {
                return(con.Execute(sql, sFTPSetting) > 0);
            }
        }
 public IActionResult ModifySFTPSetting([FromBody] SFTPSetting SFTPSetting)
 {
     try
     {
         _logger.LogInformation($"modify SFTPsetting,keyname={SFTPSetting.KeyName}");
         return(new JsonResult(new { result = true, data = _SFTPProvider.ModifySFTPSetting(SFTPSetting) }));
     }
     catch (Exception exc)
     {
         _logger.LogCritical(exc, exc.Message);
         return(new JsonResult(new { result = false, message = exc.Message }));
     }
 }