Esempio n. 1
0
        private void DownloadEpgPackage(string archiveDirectory, string localDownloadDirectory)
        {
            SftpTransferOperationResult = false;
            var localfile    = Path.Combine(localDownloadDirectory, LatestEpgPackage.Name);
            var archivedFile = Path.Combine(archiveDirectory, LatestEpgPackage.Name);

            if (System.IO.File.Exists(localfile) || System.IO.File.Exists(archivedFile))
            {
                Console.WriteLine($"Epg file: {LatestEpgPackage.Name} has previously been downloaded.");
            }
            else
            {
                Console.WriteLine($"Downloading Latest EPG File: {LatestEpgPackage.Name} to {localDownloadDirectory}");
                var operationResult = SftpSession.GetFiles(RemotePath.EscapeFileMask(LatestEpgPackage.FullName), localDownloadDirectory);

                if (operationResult.IsSuccess)
                {
                    Console.WriteLine($"Remote File: {LatestEpgPackage.FullName} Downloaded successfully");
                    EpgTarBall = new FileInfo(localfile);
                    SftpTransferOperationResult = true;
                }
                else
                {
                    throw new Exception($"Failed to download file: {operationResult.Failures[0]}");
                }
            }
        }
Esempio n. 2
0
        static void DownloadFiles()
        {
            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol = Protocol.Ftp,
                HostName = ApplicationSetting.FtpHost,
                UserName = ApplicationSetting.FtpUserName,
                Password = ApplicationSetting.FtpPassword //,
                                                          //SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx..."
            };

            string remotePath = ApplicationSetting.FtpRemoteFolder;
            string localPath  = ApplicationSetting.LatestFilesPath;

            using (Session session = new Session())
            {
                // Connect
                session.Open(sessionOptions);

                // Enumerate files and directories to download
                IEnumerable <RemoteFileInfo> fileInfos =
                    session.EnumerateRemoteFiles(
                        remotePath, null,
                        EnumerationOptions.EnumerateDirectories |
                        EnumerationOptions.AllDirectories);

                foreach (RemoteFileInfo fileInfo in fileInfos)
                {
                    string localFilePath =
                        RemotePath.TranslateRemotePathToLocal(
                            fileInfo.FullName, remotePath, localPath);

                    if (fileInfo.IsDirectory)
                    {
                        // Create local subdirectory, if it does not exist yet
                        if (!Directory.Exists(localFilePath))
                        {
                            Directory.CreateDirectory(localFilePath);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Downloading file {0}...", fileInfo.FullName);
                        // Download file
                        string remoteFilePath = RemotePath.EscapeFileMask(fileInfo.FullName);
                        TransferOperationResult transferResult =
                            session.GetFiles(remoteFilePath, localFilePath);

                        // Did the download succeeded?
                        if (!transferResult.IsSuccess)
                        {
                            // Print error (but continue with other files)
                            Console.WriteLine(
                                "Error downloading file {0}: {1}",
                                fileInfo.FullName, transferResult.Failures[0].Message);
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// </summary>
        /// <param name="session"></param>
        /// <param name="fileName">"/home/user/file.name.txt"</param>
        /// <param name="localPath"></param>
        public void DownloadFileRemoteDir(SessionOptions options, string fileName, string localPath)
        {
            using var session = new Session();
            // Connect
            session.Open(options);

            // Download the selected file
            //check // Throw on any error
            session.GetFiles(RemotePath.EscapeFileMask(fileName), localPath).Check();
        }
        public async Task SaveCSVToBlobAsync(ExecutionContext context)
        {
            using Session session = new Session
                  {
                      ExecutablePath = Path.Combine(context.FunctionAppDirectory, "winscp.exe")
                  };

            session.Open(new SessionOptions
            {
                Protocol = Protocol.Sftp,
                HostName = _configSettings.SFTPHostName,
                UserName = _configSettings.SFTPUserName,
                Password = _configSettings.SFTPPassword,
                SshHostKeyFingerprint = _configSettings.SFTPSshHostKeyFingerprint
            });

            var remotePath = _configSettings.SFTPRemotePath;
            var directory  = session.ListDirectory(remotePath);

            var files = directory.Files
                        .Where(fileInfo => !fileInfo.IsDirectory && fileInfo.Name.EndsWith(".csv", StringComparison.OrdinalIgnoreCase));

            if (files.Count() == 0)
            {
                throw new Exception($"No mathcing file found in the location");
            }
            else if (files.Count() > 1)
            {
                throw new Exception($"More than one mathcing file found in the location");
            }
            else
            {
                var fileName   = files.Single().Name;
                var tempPath   = Path.GetTempFileName();
                var sourcePath = RemotePath.EscapeFileMask(remotePath + "/" + fileName);

                session.GetFiles(sourcePath, tempPath).Check();

                using var uploadFileStream = File.OpenRead(tempPath);
                await _blobService.UploadFile(uploadFileStream, fileName);

                uploadFileStream.Close();

                File.Delete(tempPath);
                session.RemoveFiles(sourcePath);
            }
        }
Esempio n. 5
0
        static internal void WinScpGetLog()
        {
            LogEvent("Start Download");
            using (Session session = new Session())
            {
                // Connect
                session.Open(GetWinScpSession());

                string remotePath = ApplicationSetting.FtpRemoteFolder;
                string localPath  = ApplicationSetting.DownloadPath;

                // Get list of files in the directory
                RemoteDirectoryInfo directoryInfo = session.ListDirectory(remotePath);

                //latest file name in the table by application name
                var latestLogFileInDb = Path.GetFileName(unitOfWork.IISLogRepository.LatestFileName(ApplicationSetting.ApplicationName));

                //get the date of the latest file from FTP
                var logFileDate = directoryInfo.Files
                                  .Where(w => w.Name.ToLower() == latestLogFileInDb?.ToLower())
                                  .Select(s => s.LastWriteTime).FirstOrDefault();

                // Select the files not in database table
                IEnumerable <RemoteFileInfo> notInLogTable =
                    directoryInfo.Files
                    .Where(file => !file.IsDirectory && file.LastWriteTime > logFileDate).ToList();

                //// Download the selected file
                foreach (RemoteFileInfo fileInfo in notInLogTable)
                {
                    string localFilePath =
                        RemotePath.TranslateRemotePathToLocal(
                            fileInfo.FullName, remotePath, localPath);

                    string remoteFilePath = RemotePath.EscapeFileMask(fileInfo.FullName);

                    //download
                    TransferOperationResult transferResult =
                        session.GetFiles(remoteFilePath, localFilePath);
                }
                LogEvent("End Download");
            }
        }
Esempio n. 6
0
        /// <summary>
        /// </summary>
        /// <param name="session"></param>
        /// <param name="remotePath"></param>
        /// <param name="localPath"></param>
        public void DowloadFilesAndDirectories(SessionOptions options, string remotePath, string localPath)
        {
            using var session = new Session();
            // Connect
            session.Open(options);

            var fileInfos = GetFilesAndDirectoriesRemoteDir(options, remotePath);

            foreach (var fileInfo in fileInfos)
            {
                var localFilePath =
                    RemotePath.TranslateRemotePathToLocal(
                        fileInfo.FullName, remotePath, localPath);

                if (fileInfo.IsDirectory)
                {
                    // Create local subdirectory, if it does not exist yet
                    if (!Directory.Exists(localFilePath))
                    {
                        Directory.CreateDirectory(localFilePath);
                    }
                }
                else
                {
                    Console.WriteLine("Downloading file {0}...", fileInfo.FullName);
                    // Download file
                    var remoteFilePath = RemotePath.EscapeFileMask(fileInfo.FullName);

                    var transferResult =
                        session.GetFiles(remoteFilePath, localFilePath);

                    // Did the download succeeded?
                    if (!transferResult.IsSuccess)
                    {
                        // Print error (but continue with other files)
                        Console.WriteLine(
                            "Error downloading file {0}: {1}",
                            fileInfo.FullName, transferResult.Failures[0].Message);
                    }
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Delete the directory from the FTP server.
        /// </summary>
        /// <param name="FTPFilePath">Pass the full address of the directory to be excluded: Example: / folder / subfolder</param>
        /// <returns>Returns true if deleted.</returns>
        public bool DeleteSFTPFolder(string FTPFilePath)
        {
            try
            {
                var sessionOptions = InitializeFTPSession();
                using (var session = new Session())
                {
                    // Connect
                    session.Open(sessionOptions);

                    // Your code
                    session.RemoveFiles(RemotePath.EscapeFileMask(FTPFilePath));
                    return(true);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("e: " + ex.Message);
                return(false);
            }
        }
Esempio n. 8
0
        private void ConnectFTP()
        {
            txtInform1.AppendText("\r\n Start Connect SFTP .... \r\n");
            Response_SFTP  result_SFTP    = new Response_SFTP();
            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol = Protocol.Sftp,
                HostName = "sftp01.mmvietnam.com",
                UserName = "******",
                SshHostKeyFingerprint = "ssh-ed25519 255 gbfFmqgr8jbd8lAQOxHTcVQmNCk5OlnD8MXGMerAttg=",
                SshPrivateKeyPath     = @"E:\Software\MMVN\lspgmdprod.ppk",
            };

            using (Session session = new Session())
            {
                try
                {
                    session.Open(sessionOptions);
                }
                catch (InvalidOperationException e)
                {
                    result_SFTP.Handle_Error_Sftp("Connect", e.Message.ToString());
                    txtInform1.AppendText("\r\n Connect SFTP Fail .... \r\n");
                    return;
                }


                const string        remotePath = Constant.PATH_GETFILE_SFTP;
                RemoteDirectoryInfo directory  = session.ListDirectory(remotePath);

                foreach (RemoteFileInfo fileInfo in directory.Files)
                {
                    if (!fileInfo.IsDirectory && fileInfo.Name.EndsWith(".DAT", StringComparison.OrdinalIgnoreCase))
                    {
                        string tempPath   = Path.GetTempFileName();
                        var    sourcePath = RemotePath.EscapeFileMask(remotePath + "/" + fileInfo.Name);
                        string targerPath = Constant.PATH_MOVEFILE_SFTP;
                        string errorPath  = Constant.PATH_MOVEFILE_ERROR_SFTP;
                        string fileName   = fileInfo.Name;
                        string category   = fileName.Substring(9, 2).ToUpper();
                        string backupPath = string.Format(Constant.PATH_BACKUP, fileName);
                        try
                        {
                            session.GetFiles(sourcePath, backupPath).Check();
                            session.GetFiles(sourcePath, tempPath).Check();
                        }
                        catch (Exception e)
                        {
                            result_SFTP.Handle_Error_Sftp(fileName, e.Message.ToString());
                        }


                        string[] lines           = File.ReadAllLines(tempPath);
                        int      numberRow       = lines.Count();
                        int      countRowSuccess = 0;

                        ResultDatabase_Model result = new ResultDatabase_Model();
                        if (category == "MD")
                        {
                            this.Show_Message_Start(fileName);

                            foreach (string text in lines)
                            {
                                try
                                {
                                    int indicator = DAL.IntReturn(text.Substring(0, 1));
                                }
                                catch (Exception e)
                                {
                                    errorPath = string.Format(errorPath, fileName);
                                    session.MoveFile(sourcePath, errorPath);
                                    result_SFTP.Handle_Error_Sftp(fileName, e.Message.ToString());
                                    continue;
                                }
                                result          = CountLineSuccess_MasterData(text, countRowSuccess, fileName);
                                countRowSuccess = result.numberRowSuccess;
                            }
                            if (numberRow == countRowSuccess)
                            {
                                this.Show_Message_Success(fileName);

                                targerPath = string.Format(targerPath, fileName);
                                try
                                {
                                    session.MoveFile(sourcePath, targerPath);
                                }
                                catch (Exception e)
                                {
                                    result_SFTP.Handle_Error_Sftp(fileName, e.Message.ToString());
                                    continue;
                                }
                            }
                            else
                            {
                                try
                                {
                                    errorPath = string.Format(errorPath, fileName);
                                    session.MoveFile(sourcePath, errorPath);
                                    this.Show_Message_Error(fileName, countRowSuccess);
                                }
                                catch (Exception e)
                                {
                                    result_SFTP.Handle_Error_Sftp(fileName, e.Message.ToString());
                                    continue;
                                }
                            }
                        }
                        else if (category == "PO")
                        {
                            this.Show_Message_Start(fileName);

                            foreach (string text in lines)
                            {
                                try
                                {
                                    int indicator = DAL.IntReturn(text.Substring(0, 1));
                                }
                                catch (Exception e)
                                {
                                    errorPath = string.Format(errorPath, fileName);
                                    session.MoveFile(sourcePath, errorPath);
                                    result_SFTP.Handle_Error_Sftp(fileName, e.Message.ToString());
                                    continue;
                                }

                                result = CountLineSuccess_PO(text, countRowSuccess, fileName);

                                countRowSuccess = result.numberRowSuccess;
                            }
                            if (numberRow == countRowSuccess)
                            {
                                this.Show_Message_Success(fileName);

                                targerPath = string.Format(targerPath, fileName);
                                try
                                {
                                    session.MoveFile(sourcePath, targerPath);
                                }
                                catch (Exception e)
                                {
                                    result_SFTP.Handle_Error_Sftp(fileName, e.Message.ToString());
                                    continue;
                                }
                            }
                            else
                            {
                                try
                                {
                                    errorPath = string.Format(errorPath, fileName);
                                    session.MoveFile(sourcePath, errorPath);
                                    this.Show_Message_Error(fileName, countRowSuccess);
                                }
                                catch (Exception e)
                                {
                                    result_SFTP.Handle_Error_Sftp(fileName, e.Message.ToString());
                                    continue;
                                }
                            }
                        }
                        else if (category == "SO")
                        {
                            this.Show_Message_Start(fileName);

                            foreach (string text in lines)
                            {
                                try
                                {
                                    int indicator = DAL.IntReturn(text.Substring(0, 1));
                                }
                                catch (Exception e)
                                {
                                    errorPath = string.Format(errorPath, fileName);
                                    session.MoveFile(sourcePath, errorPath);
                                    result_SFTP.Handle_Error_Sftp(fileName, e.Message.ToString());
                                    continue;
                                }
                                result          = CountLineSuccess_SO(text, countRowSuccess, fileName);
                                countRowSuccess = result.numberRowSuccess;
                            }
                            if (numberRow == countRowSuccess)
                            {
                                this.Show_Message_Success(fileName);
                                targerPath = string.Format(targerPath, fileName);
                                try
                                {
                                    session.MoveFile(sourcePath, targerPath);
                                }
                                catch (Exception e)
                                {
                                    result_SFTP.Handle_Error_Sftp(fileName, e.Message.ToString());
                                    continue;
                                }
                            }
                            else
                            {
                                try
                                {
                                    errorPath = string.Format(errorPath, fileName);
                                    session.MoveFile(sourcePath, errorPath);
                                    this.Show_Message_Error(fileName, countRowSuccess);
                                }
                                catch (Exception e)
                                {
                                    result_SFTP.Handle_Error_Sftp(fileName, e.Message.ToString());
                                    continue;
                                }
                            }
                        }
                    }
                }


                session.Close();
            }
        }
Esempio n. 9
0
        //单线程即可
        public static Tuple <int, string> m_fSendFile(m_cRecModel m_mRecModel, string m_sRemoteDirectory, bool m_bTest, LogTyper lt = LogTyper.LogLogger)
        {
            int    status = 0;
            string msg    = string.Empty;

            try
            {
                ///链接(S)FTP
                SessionOptions sessionOptions = new SessionOptions();
                sessionOptions.Protocol   = (Protocol)(int.Parse(m_cConfigConstants.m_sProtocol));
                sessionOptions.HostName   = m_cConfigConstants.m_sIP;
                sessionOptions.PortNumber = int.Parse(m_cConfigConstants.m_sPort);
                sessionOptions.UserName   = m_cConfigConstants.m_sUa;
                sessionOptions.FtpMode    = (FtpMode)(int.Parse(m_cConfigConstants.m_sMode));
                if (!string.IsNullOrWhiteSpace(m_cConfigConstants.m_sPwd))
                {
                    sessionOptions.Password = m_cConfigConstants.m_sPwd;
                }
                if (!string.IsNullOrWhiteSpace(m_cConfigConstants.m_sKey))
                {
                    sessionOptions.SshHostKeyFingerprint = m_cConfigConstants.m_sKey;
                }

                ///放入测试
                using (WinSCP.Session session = new WinSCP.Session())
                {
                    session.Open(sessionOptions);

                    TransferEventArgs transferEventArgs;
                    TransferOptions   transferOptions = new TransferOptions();
                    transferOptions.TransferMode = TransferMode.Binary;

                    //是否需要创建文件夹,逐级创建
                    if (!string.IsNullOrWhiteSpace(m_sRemoteDirectory) && !session.FileExists(RemotePath.EscapeFileMask(m_sRemoteDirectory)))
                    {
                        //查找出所有目录
                        string[] m_lDirectory  = m_sRemoteDirectory.Split('/');
                        string   m_sPrefixMulu = string.Empty;

                        foreach (string m_sMulu in m_lDirectory)
                        {
                            if (!string.IsNullOrWhiteSpace(m_sMulu))
                            {
                                m_sPrefixMulu += $"/{m_sMulu}";
                                if (!session.FileExists(RemotePath.EscapeFileMask(m_sPrefixMulu)))
                                {
                                    session.CreateDirectory(RemotePath.EscapeFileMask(m_sPrefixMulu));

                                    Log.Instance.Debug($"逐级创建(S)FTP目录:“{m_sPrefixMulu}”", lt);
                                }
                            }
                        }
                    }

                    //判断文件是否已经上传
                    if (!session.FileExists(RemotePath.EscapeFileMask($"{m_sRemoteDirectory}/{System.IO.Path.GetFileName(m_mRecModel.mimiFile)}")))
                    {
                        //如果为测试,文件不进行删除
                        transferEventArgs = session.PutFileToDirectory(m_mRecModel.mimiFile, RemotePath.EscapeFileMask(m_sRemoteDirectory), !m_bTest, transferOptions);

                        string m_sErrMsg = string.Join(",", transferEventArgs?.Error?.Message);
                        if (!string.IsNullOrWhiteSpace(m_sErrMsg))
                        {
                            Log.Instance.Debug(m_sErrMsg, lt);
                            status = 1;
                            msg    = m_sErrMsg;
                        }
                        else
                        {
                            status = 0;
                            msg    = "录音上传(S)FTP成功";
                        }
                    }
                    else
                    {
                        status = 0;
                        msg    = "录音已存在,略过上传(S)FTP";

                        if (!m_bTest)
                        {
                            Log.Instance.Debug($"{m_mRecModel.Id}:“{msg}”", lt);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                status = 1;
                msg    = ex.Message;

                //如果有误
                if (!m_bTest)
                {
                    m_cSQL.m_fSetActionState(m_mRecModel.Id, "3", $"上传(S)FTP错误:{msg}", lt);
                }

                //打印一下错误,处理一下此处导致计划任务无法继续的问题
                Log.Instance.Debug(ex, lt);
            }
            return(new Tuple <int, string>(status, msg));
        }