Пример #1
0
        public override void GetFilesFromSource(CertificateTask task, IList <CertificateFile> files)
        {
            var catalogId = task.DocumentLine.ProductEntity.CatalogProduct.Id;
            var catalogs  = GetSourceCatalog(catalogId, task.SerialNumber);

            if (catalogs.Count == 0)
            {
                task.DocumentLine.CertificateError = "Нет записи в таблице перекодировки";
                return;
            }

            foreach (var certificateSourceCatalog in catalogs)
            {
                var filename = certificateSourceCatalog.OriginFilePath;
                var mask     = String.Format("{0}*{1}", Path.GetFileNameWithoutExtension(filename), Path.GetExtension(filename));

                var uri = new Uri(task.CertificateSource.LookupUrl);
                var dir = Path.Combine(Path.GetDirectoryName(uri.AbsolutePath) ?? "",
                                       Path.GetDirectoryName(certificateSourceCatalog.OriginFilePath) ?? "");
                using (var ftpClient = new FTP_Client()) {
                    ftpClient.PassiveMode = true;
                    ftpClient.Connect(uri.Host, uri.Port);
                    var credentials = Util.GetCredentials(uri);
                    if (credentials != null)
                    {
                        ftpClient.Authenticate(credentials.UserName, credentials.Password);
                    }
                    else
                    {
                        ftpClient.Authenticate("anonymous", "");
                    }
                    if (!String.IsNullOrEmpty(dir))
                    {
                        ftpClient.SetCurrentDir(dir);
                    }
                    var ftpFiles        = ftpClient.GetList();
                    var filesToDownload = ftpFiles.Tables["DirInfo"]
                                          .AsEnumerable()
                                          .Where(r => !Convert.ToBoolean(r["IsDirectory"]))
                                          .Select(r => r["Name"].ToString())
                                          .Where(n => FileHelper.CheckMask(n, mask))
                                          .ToList();
                    foreach (var file in filesToDownload)
                    {
                        var tempFileName = Path.GetTempFileName();
                        ftpClient.ReceiveFile(file, tempFileName);
                        files.Add(new CertificateFile(
                                      tempFileName,
                                      certificateSourceCatalog.OriginFilePath,
                                      file,
                                      task.CertificateSource));
                    }
                }
            }

            if (files.Count == 0)
            {
                task.DocumentLine.CertificateError = "Файл сертификата не найден на ftp поставщика";
            }
        }
        private DataSet GetFtpFilesAndDirectories(FTP_Client ftpClient, string ftpHost, int ftpPort, string username, string password, string ftpDirectory, bool ftpPassiveMode = true)
        {
            DataSet dataSetEntries = null;

            ftpClient.PassiveMode = ftpPassiveMode;
#if DEBUG
            if (UseStub)
            {
                if (ftpDirectory.StartsWith("/"))
                {
                    ftpDirectory = ftpDirectory.Remove(0, 1);
                }
                dataSetEntries = ToDirInfo(Path.Combine(Settings.Default.FTPOptBoxPath, ftpDirectory), new DataSet());
            }
            else
            {
#endif
            ftpClient.Connect(ftpHost, ftpPort);
            if (!String.IsNullOrEmpty(username))
            {
                ftpClient.Authenticate(username, password);
            }
            else
            {
                ftpClient.Authenticate("anonymous", "");
            }
            dataSetEntries = ftpClient.GetList();
#if DEBUG
        }
#endif
            return(dataSetEntries);
        }
Пример #3
0
        public override Server creack(String ip, int port, String username, String password, int timeOut)
        {
            FTP_Client ftp = null;

            Server server = new Server();

            try
            {
                ftp         = new FTP_Client();
                ftp.Timeout = timeOut;
                ftp.Connect(ip, port, false);
                if (ftp.IsConnected)
                {
                    ftp.Authenticate(username, password);
                    server.isSuccess = ftp.IsAuthenticated;
                    server.banner    = ftp.GreetingText;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally {
                if (ftp != null)
                {
                    ftp.Disconnect();
                }
            }
            return(server);
        }
 /// <summary>
 /// Sends message.
 /// </summary>
 private void Send()
 {
     try{
         using (FTP_Client ftp = new FTP_Client()){
             ftp.Connect(m_Server, m_Port);
             ftp.Authenticate(m_User, m_Password);
             ftp.SetCurrentDir(m_Folder);
             ftp.StoreFile(m_FileName, m_DataStream);
         }
     }
     catch (Exception x) {
         Error.DumpError(x, new System.Diagnostics.StackTrace());
     }
 }
 private void Send()
 {
     try
     {
         using (FTP_Client fTP_Client = new FTP_Client())
         {
             fTP_Client.Connect(this.m_Server, this.m_Port);
             fTP_Client.Authenticate(this.m_User, this.m_Password);
             fTP_Client.SetCurrentDir(this.m_Folder);
             fTP_Client.StoreFile(this.m_FileName, this.m_DataStream);
         }
     }
     catch (Exception x)
     {
         Error.DumpError(x);
     }
 }
        public static DownloadedFile GetFileFromSource(PriceSource source, string downHandlerPath)
        {
            var ftpHost = source.PricePath;

            if (ftpHost.StartsWith(@"ftp://", StringComparison.OrdinalIgnoreCase))
            {
                ftpHost = ftpHost.Substring(6);
            }
            if (ftpHost.EndsWith(@"/"))
            {
                ftpHost = ftpHost.Substring(0, ftpHost.Length - 1);
            }

            var pricePath = source.FtpDir;

            if (!pricePath.StartsWith(@"/", StringComparison.OrdinalIgnoreCase))
            {
                pricePath = @"/" + pricePath;
            }

            var ftpFileName   = String.Empty;
            var downFileName  = String.Empty;
            var shortFileName = String.Empty;
            var priceDateTime = source.PriceDateTime;

            using (var ftpClient = new FTP_Client()) {
                ftpClient.PassiveMode = true;
                ftpClient.Connect(ftpHost, 21);
                ftpClient.Authenticate(source.FtpLogin, source.FtpPassword);
                ftpClient.SetCurrentDir(pricePath);

                var dsEntries = ftpClient.GetList();

                foreach (DataRow entry in dsEntries.Tables["DirInfo"].Rows)
                {
                    if (Convert.ToBoolean(entry["IsDirectory"]))
                    {
                        continue;
                    }

                    shortFileName = entry["Name"].ToString();

                    var priceMaskIsMatched = PriceProcessor.FileHelper.CheckMask(shortFileName, source.PriceMask);
                    if (priceMaskIsMatched)
                    {
                        var fileLastWriteTime = Convert.ToDateTime(entry["Date"]);
#if DEBUG
                        priceDateTime = fileLastWriteTime;
                        ftpFileName   = shortFileName;
#endif
                        if (((fileLastWriteTime.CompareTo(priceDateTime) > 0) &&
                             (DateTime.Now.Subtract(fileLastWriteTime).TotalMinutes > Settings.Default.FileDownloadInterval)) ||
                            ((fileLastWriteTime.CompareTo(DateTime.Now) > 0) && (fileLastWriteTime.Subtract(priceDateTime).TotalMinutes > 0)))
                        {
                            priceDateTime = fileLastWriteTime;
                            ftpFileName   = shortFileName;
                        }
                    }
                }

                if (String.IsNullOrEmpty(ftpFileName))
                {
                    return(null);
                }
                downFileName = Path.Combine(downHandlerPath, ftpFileName);
                using (var file = new FileStream(downFileName, FileMode.Create))
                    ftpClient.ReceiveFile(ftpFileName, file);
            }

            return(new DownloadedFile(downFileName, priceDateTime));
        }