Adding
This commit is contained in:
+54
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Renci.SshNet;
|
||||
using Serilog;
|
||||
using Newtonsoft.Json;
|
||||
using LCU.Api.Internal.FTPLib.Models;
|
||||
namespace LCU.Api.Internal.FTP.Services.FtpOpertionServices;
|
||||
|
||||
public class SshFtpService : ISshFtpService
|
||||
{
|
||||
public FtpRequestResponse ExecuteAction(QueueRequest queueRequest)
|
||||
{
|
||||
var ftpRequestResponse = new FtpRequestResponse();
|
||||
try
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(queueRequest);
|
||||
var request = JsonConvert.DeserializeObject<SshFtpRequest>(queueRequest.Request);
|
||||
ArgumentNullException.ThrowIfNull(request);
|
||||
ftpRequestResponse.ReturnResponse = request;
|
||||
TransferFiles(request);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
ftpRequestResponse.IsSuccessful = false;
|
||||
ftpRequestResponse.MessageStack.Add(exception.Message);
|
||||
}
|
||||
return ftpRequestResponse;
|
||||
}
|
||||
|
||||
public void TransferFiles(SshFtpRequest request)
|
||||
{
|
||||
using var client = new SftpClient(request.HostName,
|
||||
request.PortNumber == 0 ? 22 : request.PortNumber,
|
||||
request.UserName, request.Password);
|
||||
|
||||
try
|
||||
{
|
||||
client.Connect();
|
||||
using var stream = new MemoryStream(request.LocalFileData);
|
||||
client.UploadFile(stream, request.RemoteFilePath);
|
||||
Log.Information(
|
||||
$"Uploaded [{request.RemoteFilePath}]");
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception($"Failed uploading [{request.RemoteFilePath}]" +
|
||||
$"ExceptionMessage: {exception.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
client.Disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user