74 lines
2.2 KiB
C#
74 lines
2.2 KiB
C#
using System;
|
|
using Serilog;
|
|
using WinSCP;
|
|
using LCU.Api.Internal.FTPLib.Models;
|
|
namespace LCU.Api.Internal.FTP.Helpers;
|
|
|
|
public static class WinScpSessionOptionsHelper
|
|
{
|
|
public static SessionOptions CreateSessionOptions(SftpRequest sftpRequest)
|
|
{
|
|
try
|
|
{
|
|
var protocol = FindWinScpProtocol(sftpRequest.Protocol);
|
|
|
|
SessionOptions sessionOptions = new SessionOptions
|
|
{
|
|
Protocol = protocol,
|
|
HostName = sftpRequest.HostName,
|
|
PortNumber = sftpRequest.PortNumber,
|
|
UserName = sftpRequest.UserName,
|
|
Password = sftpRequest.Password,
|
|
SshHostKeyFingerprint = sftpRequest.SshHostKeyFingerprint
|
|
};
|
|
|
|
return sessionOptions;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, "Error creating SessionOptions for Sftp Request");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static SessionOptions CreateSessionOptions(SessionInformation sessionInformation)
|
|
{
|
|
try
|
|
{
|
|
var protocol = FindWinScpProtocol(sessionInformation.Protocol);
|
|
|
|
SessionOptions sessionOptions = new SessionOptions
|
|
{
|
|
Protocol = protocol,
|
|
HostName = sessionInformation.HostName,
|
|
PortNumber = sessionInformation.PortNumber,
|
|
UserName = sessionInformation.UserName,
|
|
Password = sessionInformation.Password,
|
|
SshHostKeyFingerprint = sessionInformation.SshHostKeyFingerprint
|
|
};
|
|
|
|
if (!string.IsNullOrWhiteSpace(sessionInformation.SshKeyFilePath))
|
|
{
|
|
sessionOptions.SshPrivateKeyPath = sessionInformation.SshKeyFilePath;
|
|
}
|
|
|
|
return sessionOptions;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, "Error creating SessionOptions for Sftp Request");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static Protocol FindWinScpProtocol(ProtocolType protocolType)
|
|
{
|
|
switch (protocolType)
|
|
{
|
|
case ProtocolType.Sftp:
|
|
return Protocol.Sftp;
|
|
default:
|
|
return Protocol.Sftp;
|
|
}
|
|
}
|
|
} |