예제 #1
0
        public void Remove(string Path, string Filename)
        {
            try
            {
                if (_ftpClient.DirectoryExists(Path))
                {
                    _ftpClient.RetryAttempts = 10;

                    string fullpath = Path + Filename;

                    _log.Info($"{FtpTemplateMessage("Remove", Filename)}", fullpath);
                    _ftpClient.DeleteFile(fullpath);
                }
                else
                {
                    _log.Info($"{FtpTemplateMessage("Remove", Filename, "not exist")}", Path + Filename);
                    throw new Exception("File Not Exist");
                }
            }
            catch (Exception ex)
            {
                _log.Error($"{FtpTemplateMessage("Remove", Filename, "throw exception")}", ex);
                throw ex;
            }
        }
예제 #2
0
파일: FTP.cs 프로젝트: study4coder/FtpSync
        private void SyncDirectory(IFtpClient client, string remotePath, string localPath, bool isDel)
        {
            var localFolder = new DirectoryInfo(localPath);
            var infos       = localFolder.GetFileSystemInfos();

            foreach (var info in infos)
            {
                if (!client.IsConnected)
                {
                    client.Connect();
                }
                if (info is FileInfo)
                {
                    var size = (info as FileInfo).Length;

                    var remoteFile = Path.Combine(remotePath, info.Name);

                    if (!client.FileExists(remoteFile) || client.GetFileSize(remoteFile) != size)
                    {
                        client.UploadFile(info.FullName, remoteFile);

                        Logger.Info($"Uploaded==>{info.FullName}");
                    }
                }
                else if (info is DirectoryInfo)
                {
                    var remoteFile = Path.Combine(remotePath, info.Name);

                    if (!client.DirectoryExists(remoteFile))
                    {
                        client.CreateDirectory(remoteFile);

                        Logger.Info($"CreateFtpDirectory==>{remoteFile}");
                    }
                    SyncDirectory(client, Path.Combine(remotePath, info.Name), info.FullName, isDel);
                }
            }

            if (isDel)
            {
                var items = client.GetListing(remotePath);
                foreach (var item in items)
                {
                    if (infos.All(info => info.Name != item.Name))
                    {
                        if (item.Type == FtpFileSystemObjectType.File)
                        {
                            client.DeleteFile(item.FullName);
                        }
                        else if (item.Type == FtpFileSystemObjectType.Directory)
                        {
                            client.DeleteDirectory(item.FullName);
                        }

                        Logger.Info($"DeletedFtp==>{item.FullName}");
                    }
                }
            }
        }
예제 #3
0
        protected virtual async Task <ImportVehiclesForSiteResult> InternalImportForSiteAsync(Site site, IFtpClient ftpClient)
        {
            ImportVehiclesForSiteResult importResult;

            try
            {
                if (site.ImportRelativeFtpPath != null)
                {
                    if (ftpClient.DirectoryExists(site.ImportRelativeFtpPath))
                    {
                        FtpListItem lastModifiedFileInfo = (await ftpClient.GetListingAsync(site.ImportRelativeFtpPath, FtpListOption.Auto))
                                                           .Where(r => r.Type == FtpFileSystemObjectType.File)
                                                           .OrderByDescending(r => r.Modified)
                                                           .FirstOrDefault();
                        if (lastModifiedFileInfo != null)
                        {
                            IEnumerable <Vehicle> vehicles;
                            using (var csvFileStream = new MemoryStream(await ftpClient.DownloadAsync(lastModifiedFileInfo.FullName)))
                            {
                                vehicles = VehicleBulkFactory.Create(new VehicleFromCsvFileBulkFactorySettings(site.Id, csvFileStream));
                            }
                            await VehicleRepository.RefreshEntitiesForSiteAsync(site.Id, vehicles);

                            importResult = new ImportVehiclesForSiteResult(
                                site.Id, site.Name, vehicles.Count(),
                                vehicles.Count(r => r.Condition == VehicleConditions.New),
                                vehicles.Count(r => r.Condition == VehicleConditions.Used));
                        }
                        else
                        {
                            importResult = new ImportVehiclesForSiteResult(site.Id, site.Name, $"No files in specified folder ({site.ImportRelativeFtpPath}).", ImportStatusEnum.NotStarted);
                        }
                    }
                    else
                    {
                        importResult = new ImportVehiclesForSiteResult(site.Id, site.Name, $"The specified folder ({site.ImportRelativeFtpPath}) was not found.", ImportStatusEnum.NotStarted);
                    }
                }
                else
                {
                    importResult = new ImportVehiclesForSiteResult(site.Id, site.Name, $"The specified folder is not defined.", ImportStatusEnum.NotStarted);
                }
            }
            catch (Exception ex)
            {
                importResult = new ImportVehiclesForSiteResult(site.Id, site.Name, ex.Message, ImportStatusEnum.Failed);
            }
            return(importResult);
        }
예제 #4
0
        public void UploadFiles(string[] FilePaths, TreeNode pNode)
        {
            IFtpClient pFtpClient = SysDBConfig.GetInstance().GetFtpClient("CHXQ");

            pFtpClient.Connect();
            IDataBase pDataBase = SysDBConfig.GetInstance().GetOleDataBase("OrclConn");

            pDataBase.OpenConnection();
            string DirPath = GetFtpPathByNode(pNode);

            try
            {
                if (!pFtpClient.DirectoryExists(DirPath))
                {
                    pFtpClient.CreateDirectory(DirPath);
                }
                foreach (string FilePath in FilePaths)
                {
                    string RemotePath = string.Format("{0}\\{1}", DirPath, System.IO.Path.GetFileName(FilePath));
                    pFtpClient.UploadFile(FilePath, RemotePath);
                    string           ProcedureName  = "AddFile";
                    IDataParameter[] DataParameters = new IDataParameter[] {
                        new OracleParameter()
                        {
                            ParameterName = "V_FileName", OracleType = OracleType.NVarChar, Value = System.IO.Path.GetFileNameWithoutExtension(FilePath)
                        },
                        new OracleParameter()
                        {
                            ParameterName = "V_Path", OracleType = OracleType.NVarChar, Value = RemotePath
                        },
                        new OracleParameter()
                        {
                            ParameterName = "V_DirID", OracleType = OracleType.Number, Value = this.GetNodeID(pNode)
                        }
                    };
                    pDataBase.ExecuteProcedure(ProcedureName, ref DataParameters);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                pDataBase.CloseConnection();
                pFtpClient.Close();
            }
        }
예제 #5
0
        private void UploadFile(string FilePath, string UploadPath)
        {
            IFtpClient pFtpClient = HR.Utility.SysDBConfig.GetInstance().GetFtpClient("CHXQ");

            pFtpClient.Connect();
            string Dir = System.IO.Path.GetDirectoryName(UploadPath);

            if (!pFtpClient.DirectoryExists(Dir))
            {
                pFtpClient.CreateDirectory(Dir);
            }
            if (pFtpClient.Exists(UploadPath))
            {
                pFtpClient.DeleteFile(UploadPath);
            }
            pFtpClient.UploadFile(FilePath, UploadPath);
            pFtpClient.Close();
        }
        private void CreateMissingDirectories(string serverPath, IFtpClient client)
        {
            var pathItems      = serverPath.SplitPath();
            var serverLocation = string.Empty;

            foreach (var pathItem in pathItems)
            {
                serverLocation = serverLocation.CombinePath(pathItem);
                var exists = client.DirectoryExists(serverLocation);
                if (exists)
                {
                    continue;
                }
                var creationResult = client.CreateDirectory(serverLocation);
                if (creationResult == false)
                {
                    Debug.LogError($"Can't create Remote Folder {serverLocation}");
                }
            }
        }