Files
DDUtility/JWH/NETWORK/FtpsClient.cs
2025-03-07 11:57:24 +09:00

215 lines
5.5 KiB
C#

using System;
using System.Net;
using FluentFTP;
using Org.BouncyCastle.Crypto.Agreement.JPake;
namespace JWH.NETWORK
{
public static class FtpsClient
{
private static FluentFTP.FtpClient _ftpClient;
public static string _host { get; private set; }
private static int _port = 990; // FTPS 기본 포트
private static string _userId;
private static string _password;
public static void Initialize(string host, string userId, string password, int port = 990)
{
try
{
_host = host;
_userId = userId;
_password = password;
_port = port;
_ftpClient = new FluentFTP.FtpClient(_host, _port, new NetworkCredential(_userId, _password))
{
//EncryptionMode = FtpEncryptionMode.Explicit,
EncryptionMode = FtpEncryptionMode.Implicit,
SslProtocols = System.Security.Authentication.SslProtocols.Tls12,
ConnectTimeout = 500 // 타임아웃 설정 (0.5초)
};
_ftpClient.ValidateCertificate += (control, args) => args.Accept = true; // 인증서 검증 비활성화 (운영 확인 필요!)
}
catch (Exception ex)
{
throw ex;
}
finally
{
}
}
public static void Connect()
{
try
{
if (_ftpClient == null)
throw new InvalidOperationException("FTPS client is not initialized. Call Initialize() first.");
if (!_ftpClient.IsConnected)
_ftpClient.Connect();
}
catch (Exception ex)
{
throw ex;
}
finally
{
}
}
public static void Disconnect()
{
try
{
if (_ftpClient != null && _ftpClient.IsConnected)
_ftpClient.Disconnect();
}
catch (Exception ex)
{
throw ex;
}
finally
{
}
}
public static bool IsFileExists(string path)
{
try
{
Connect();
return _ftpClient.FileExists(path);
}
catch (Exception ex)
{
throw ex;
}
finally
{
Disconnect();
}
}
public static bool IsDirExists(string path)
{
try
{
Connect();
return _ftpClient.DirectoryExists(path);
}
catch (Exception ex)
{
throw ex;
}
finally
{
Disconnect();
}
}
public static FtpListItem[] GetFtpsList(string path)
{
try
{
Connect();
return _ftpClient.GetListing(path);
}
catch (Exception ex)
{
throw ex;
}
finally
{
Disconnect();
}
}
public static object GetObjectInfo(string filePath)
{
try
{
Connect();
return _ftpClient.GetObjectInfo(filePath);
}
catch (Exception ex)
{
throw ex;
}
finally
{
Disconnect();
}
}
public static FtpListItem GetFileInfo(string remoteFilePath)
{
try
{
Connect();
if (!_ftpClient.IsConnected)
throw new InvalidOperationException("FTPS 서버에 연결되지 않았습니다.");
// Passive Mode 설정 (필요한 경우)
// _ftpClient.SetPassiveMode(true);
if (!FtpsClient.IsFileExists(remoteFilePath))
throw new InvalidOperationException("파일이 존재하지 않습니다.");
var fileInfo = FtpsClient.GetObjectInfo(remoteFilePath);
if (fileInfo == null)
throw new InvalidOperationException("파일 정보를 가져오는 데 실패했습니다.");
return (FtpListItem)fileInfo;
}
catch (Exception ex)
{
throw ex;
}
finally
{
Disconnect();
}
}
public static bool DownloadFtpsFile(string remotePath, string localPath)
{
try
{
Connect();
_ftpClient.DownloadFile(localPath, remotePath);
return true;
}
catch (Exception ex)
{
throw ex;
}
finally
{
Disconnect();
}
}
public static bool UploadFtpsFile(string localPath, string remotePath)
{
try
{
Connect();
_ftpClient.UploadFile(localPath, remotePath);
return true;
}
catch (Exception ex)
{
throw ex;
}
finally
{
Disconnect();
}
}
}
}