Adding
This commit is contained in:
+116
@@ -0,0 +1,116 @@
|
||||
using Newtonsoft.Json;
|
||||
using Serilog;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Api.Internal.FTP.Configuration;
|
||||
using LCU.Api.Internal.FTPLib.Models;
|
||||
using LCU.Library.Internal.Exceptions;
|
||||
using System.Linq;
|
||||
|
||||
namespace LCU.Api.Internal.FTP.Services.FtpOpertionServices;
|
||||
|
||||
public class FileOperationsService : IFileOperationsService
|
||||
{
|
||||
private readonly IHttpClientFactory _httpFactory;
|
||||
private readonly FtpApiConfiguration _ftpApiConfiguration;
|
||||
|
||||
public FileOperationsService(IHttpClientFactory httpFactory, FtpApiConfiguration ftpApiConfiguration)
|
||||
{
|
||||
|
||||
_httpFactory = httpFactory;
|
||||
_ftpApiConfiguration = ftpApiConfiguration;
|
||||
|
||||
}
|
||||
public async Task<List<string>> GetListOfFilesInDirectory(SftpUploadFileRequest uploadRequest)
|
||||
{
|
||||
List<string> files = new List<string>();
|
||||
try
|
||||
{
|
||||
var shouldBuildDynamicFileRequest = uploadRequest?.DynamicNameSetting?.RegexSettings != null && uploadRequest.DynamicNameSetting.RegexSettings.Any();
|
||||
if (shouldBuildDynamicFileRequest)
|
||||
{
|
||||
files = await BuildDynamicFileRequest(uploadRequest);
|
||||
}
|
||||
else
|
||||
{
|
||||
files = await ValidateFileExists(uploadRequest);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, "Error Generating File List");
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
public async Task<List<string>> BuildDynamicFileRequest(SftpUploadFileRequest uploadRequest)
|
||||
{
|
||||
List<string> files = new List<string>();
|
||||
try
|
||||
{
|
||||
var dynamicFileRequest = new DynamicFileListRequest()
|
||||
{
|
||||
Filename = uploadRequest.FinalLocalFileName,
|
||||
DynamicFileSettings = uploadRequest.DynamicNameSetting,
|
||||
Path = uploadRequest.FinalLocalFilePath.EndsWith("\\") ? uploadRequest.FinalLocalFilePath : $"{uploadRequest.FinalLocalFilePath}\\"
|
||||
};
|
||||
|
||||
var encodedContent = JsonConvert.SerializeObject(dynamicFileRequest);
|
||||
var content = new StringContent(encodedContent, Encoding.UTF8, "application/json");
|
||||
var client = _httpFactory.CreateClient();
|
||||
HttpResponseMessage response = await client.PostAsync(_ftpApiConfiguration.Apis.FileOperationApi.BuildDynamicFileRequest, content);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
throw new ServiceException($"StatusCode:{response.StatusCode}, " +
|
||||
$"Content: {response.Content.ReadAsStringAsync().Result}");
|
||||
}
|
||||
|
||||
var jsonString = await response.Content.ReadAsStringAsync();
|
||||
files = JsonConvert.DeserializeObject<List<string>>(jsonString);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, "Error Building Dynamic File List");
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
public async Task<List<string>> ValidateFileExists(SftpUploadFileRequest uploadRequest)
|
||||
{
|
||||
List<string> files = new List<string>();
|
||||
try
|
||||
{
|
||||
var path = uploadRequest.FinalLocalFilePath.EndsWith("\\") ? uploadRequest.FinalLocalFilePath : $"{uploadRequest.FinalLocalFilePath}\\";
|
||||
var finalFilePath = $"{path}{uploadRequest.FinalLocalFileName}";
|
||||
|
||||
var encodedContent = JsonConvert.SerializeObject(finalFilePath);
|
||||
var content = new StringContent(encodedContent, Encoding.UTF8, "application/json");
|
||||
var client = _httpFactory.CreateClient();
|
||||
HttpResponseMessage response = await client.PostAsync(_ftpApiConfiguration.Apis.FileOperationApi.ValidateFileExists, content);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
throw new ServiceException($"StatusCode:{response.StatusCode}, " +
|
||||
$"Content: {response.Content.ReadAsStringAsync().Result}");
|
||||
}
|
||||
|
||||
var jsonString = await response.Content.ReadAsStringAsync();
|
||||
var result = JsonConvert.DeserializeObject<bool>(jsonString);
|
||||
if (result)
|
||||
{
|
||||
files.Add(uploadRequest.FinalLocalFileName);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, "Error Validating if File Exist");
|
||||
}
|
||||
return files;
|
||||
}
|
||||
}
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.Api.Internal.FTPLib.Models;
|
||||
|
||||
namespace LCU.Api.Internal.FTP.Services.FtpOpertionServices
|
||||
{
|
||||
public interface IFileOperationsService
|
||||
{
|
||||
Task<List<string>> GetListOfFilesInDirectory(SftpUploadFileRequest uploadRequest);
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
using LCU.Api.Internal.FTPLib.Models;
|
||||
namespace LCU.Api.Internal.FTP.Services.FtpOpertionServices;
|
||||
|
||||
public interface ISshFtpService
|
||||
{
|
||||
FtpRequestResponse ExecuteAction(QueueRequest queueRequest);
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
using LCU.Api.Internal.FTPLib.Models;
|
||||
using System.Collections.Generic;
|
||||
namespace LCU.Api.Internal.FTP.Services.FtpOpertionServices;
|
||||
|
||||
public interface IWinScpListDirectoryService
|
||||
{
|
||||
List<string> GetListOfFilesInDirectory(ListDirectoryRequest listDirectoryRequest);
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
using LCU.Api.Internal.FTPLib.Models;
|
||||
namespace LCU.Api.Internal.FTP.Services.FtpOpertionServices;
|
||||
|
||||
public interface IWinScpSftpDownloadFilesService
|
||||
{
|
||||
FtpRequestResponse ExecuteAction(QueueRequest queueRequest);
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
using LCU.Api.Internal.FTPLib.Models;
|
||||
namespace LCU.Api.Internal.FTP.Services.FtpOpertionServices;
|
||||
|
||||
public interface IWinScpSftpUploadFileService
|
||||
{
|
||||
FtpRequestResponse ExecuteAction(QueueRequest queueRequest);
|
||||
}
|
||||
+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();
|
||||
}
|
||||
}
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using Serilog;
|
||||
using WinSCP;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using LCU.Api.Internal.FTPLib.Models;
|
||||
using LCU.Api.Internal.FTP.Helpers;
|
||||
namespace LCU.Api.Internal.FTP.Services.FtpOpertionServices;
|
||||
|
||||
public class WinScpListDirectoryService : IWinScpListDirectoryService
|
||||
{
|
||||
private const char FileTypeFiles = '-';
|
||||
|
||||
public List<string> GetListOfFilesInDirectory(ListDirectoryRequest listDirectoryRequest)
|
||||
{
|
||||
try
|
||||
{
|
||||
var remoteDirectoryInfo = GetRemoteDirectoryInfo(listDirectoryRequest);
|
||||
var fileNamesInDirectory = remoteDirectoryInfo.Files.Where(file => file.FileType == FileTypeFiles)
|
||||
.Select(file => file.Name)
|
||||
.ToList();
|
||||
return fileNamesInDirectory;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Log.Error(exception, $"Failed to get directory list with WinSCP. " +
|
||||
$"Host: {listDirectoryRequest.SessionInformation.HostName}, " +
|
||||
$"RemotePath: {listDirectoryRequest.RemotePath}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public RemoteDirectoryInfo GetRemoteDirectoryInfo(ListDirectoryRequest listDirectoryRequest)
|
||||
{
|
||||
RemoteDirectoryInfo remoteDirectoryInfo = null;
|
||||
try
|
||||
{
|
||||
var sessionOptions = WinScpSessionOptionsHelper.CreateSessionOptions(listDirectoryRequest.SessionInformation);
|
||||
using (var session = new Session())
|
||||
{
|
||||
session.Open(sessionOptions);
|
||||
var transferOptions = new TransferOptions { TransferMode = TransferMode.Binary, PreserveTimestamp = false };
|
||||
remoteDirectoryInfo = session.ListDirectory(listDirectoryRequest.RemotePath);
|
||||
ArgumentNullException.ThrowIfNull(remoteDirectoryInfo);
|
||||
}
|
||||
|
||||
return remoteDirectoryInfo;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Log.Error(exception, $"Failed to get directory list with WinSCP. " +
|
||||
$"Host: {listDirectoryRequest.SessionInformation.HostName}, " +
|
||||
$"RemotePath: {listDirectoryRequest.RemotePath}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using Serilog;
|
||||
using WinSCP;
|
||||
using System.Text.RegularExpressions;
|
||||
using LCU.Api.Internal.FTP.Helpers;
|
||||
using LCU.Api.Internal.FTPLib.Models;
|
||||
using LCU.Library.Internal.FileOperationsModels.Shared;
|
||||
namespace LCU.Api.Internal.FTP.Services.FtpOpertionServices;
|
||||
|
||||
public class WinScpSftpDownloadFilesService : IWinScpSftpDownloadFilesService
|
||||
{
|
||||
private readonly IWinScpListDirectoryService _winScpListDirectoryService;
|
||||
public WinScpSftpDownloadFilesService(IWinScpListDirectoryService winScpListDirectoryService)
|
||||
{
|
||||
_winScpListDirectoryService = winScpListDirectoryService;
|
||||
}
|
||||
|
||||
public FtpRequestResponse ExecuteAction(QueueRequest queueRequest)
|
||||
{
|
||||
var ftpRequestResponse = new FtpRequestResponse();
|
||||
try
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(queueRequest);
|
||||
var request = JsonConvert.DeserializeObject<SftpDownloadFileRequest>(queueRequest.Request);
|
||||
ArgumentNullException.ThrowIfNull(request);
|
||||
ftpRequestResponse.ReturnResponse = request;
|
||||
ftpRequestResponse = TransferFiles(request, ftpRequestResponse);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
ftpRequestResponse.IsSuccessful = false;
|
||||
ftpRequestResponse.MessageStack.Add(exception.Message);
|
||||
}
|
||||
return ftpRequestResponse;
|
||||
}
|
||||
|
||||
private FtpRequestResponse TransferFiles(SftpDownloadFileRequest winScpDownloadFileRequest, FtpRequestResponse ftpRequestResponse)
|
||||
{
|
||||
try
|
||||
{
|
||||
var listDirectoryRequest = new ListDirectoryRequest()
|
||||
{
|
||||
SessionInformation = winScpDownloadFileRequest.SessionInformation,
|
||||
RemotePath = winScpDownloadFileRequest.FinalRemoteFilePath
|
||||
};
|
||||
var filesInDirectory = _winScpListDirectoryService.GetListOfFilesInDirectory(listDirectoryRequest);
|
||||
var remoteFileNameRegex = winScpDownloadFileRequest.DynamicNameSetting != null ?
|
||||
RegexNameHandler.BuildRegexName(winScpDownloadFileRequest.FinalRemoteFileName, winScpDownloadFileRequest.DynamicNameSetting) :
|
||||
new Regex(winScpDownloadFileRequest.FinalRemoteFileName);
|
||||
|
||||
var matchedDirectoryFiles = filesInDirectory.Where(fileName => remoteFileNameRegex.IsMatch(fileName)).ToList();
|
||||
if (!matchedDirectoryFiles.Any())
|
||||
{
|
||||
ftpRequestResponse.MessageStack.Add($"No files available to be downloaded. " +
|
||||
$"RegexFileName: {remoteFileNameRegex}. " +
|
||||
$"Succeed On Empty?: {winScpDownloadFileRequest.SucceedOnEmpty}");
|
||||
ftpRequestResponse.IsSuccessful = winScpDownloadFileRequest.SucceedOnEmpty;
|
||||
return ftpRequestResponse;
|
||||
}
|
||||
|
||||
var sessionOptions = WinScpSessionOptionsHelper.CreateSessionOptions(winScpDownloadFileRequest.SessionInformation);
|
||||
WinScpFile(sessionOptions, winScpDownloadFileRequest, matchedDirectoryFiles);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
ftpRequestResponse.MessageStack.Add($"Failed to download file with WinSCP. " +
|
||||
$"Host: {winScpDownloadFileRequest.SessionInformation.HostName}, " +
|
||||
$"RemotePath: {winScpDownloadFileRequest.FinalRemoteFilePath} " +
|
||||
$"ExceptionMessage: { exception.Message }");
|
||||
ftpRequestResponse.IsSuccessful = false;
|
||||
}
|
||||
|
||||
return ftpRequestResponse;
|
||||
}
|
||||
|
||||
private void WinScpFile(SessionOptions sessionOptions, SftpDownloadFileRequest winScpDownloadFileRequest, List<string> remoteFileNames)
|
||||
{
|
||||
using (var session = new Session())
|
||||
{
|
||||
session.Open(sessionOptions);
|
||||
var transferOptions = new TransferOptions { TransferMode = TransferMode.Binary, PreserveTimestamp = false };
|
||||
|
||||
if (!winScpDownloadFileRequest.ResumeSupportStateOn)
|
||||
{
|
||||
transferOptions.ResumeSupport.State = TransferResumeSupportState.Off;
|
||||
}
|
||||
|
||||
foreach(string remoteFileName in remoteFileNames)
|
||||
{
|
||||
var addedSlash = winScpDownloadFileRequest.FinalRemoteFilePath.EndsWith('/') ? "" : "/";
|
||||
var remotefilePath = $"{winScpDownloadFileRequest.FinalRemoteFilePath}{addedSlash}{remoteFileName}";
|
||||
var transferResult = session.GetFiles(remotefilePath, winScpDownloadFileRequest.FinalLocalFilePath, winScpDownloadFileRequest.ShouldRemove, transferOptions);
|
||||
transferResult.Check();
|
||||
foreach (TransferEventArgs transfer in transferResult.Transfers)
|
||||
{
|
||||
Log.Debug($"Download of {transfer.FileName} succeeded");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
using Newtonsoft.Json;
|
||||
using Serilog;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using WinSCP;
|
||||
using LCU.Api.Internal.FTPLib.Models;
|
||||
using LCU.Api.Internal.FTP.Helpers;
|
||||
|
||||
namespace LCU.Api.Internal.FTP.Services.FtpOpertionServices;
|
||||
|
||||
public class WinScpSftpUploadFileService : IWinScpSftpUploadFileService
|
||||
{
|
||||
IFileOperationsService _fileOperationsService;
|
||||
|
||||
public WinScpSftpUploadFileService(IFileOperationsService fileOperationsService)
|
||||
{
|
||||
_fileOperationsService = fileOperationsService;
|
||||
}
|
||||
|
||||
public FtpRequestResponse ExecuteAction(QueueRequest queueRequest)
|
||||
{
|
||||
var ftpRequestResponse = new FtpRequestResponse();
|
||||
try
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(queueRequest);
|
||||
var request = JsonConvert.DeserializeObject<SftpUploadFileRequest>(queueRequest.Request);
|
||||
ArgumentNullException.ThrowIfNull(request);
|
||||
ftpRequestResponse.ReturnResponse = request;
|
||||
ftpRequestResponse = TransferFiles(request, ftpRequestResponse);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
ftpRequestResponse.IsSuccessful = false;
|
||||
ftpRequestResponse.MessageStack.Add(exception.Message);
|
||||
}
|
||||
return ftpRequestResponse;
|
||||
}
|
||||
|
||||
private FtpRequestResponse TransferFiles(SftpUploadFileRequest winScpUploadFileRequest, FtpRequestResponse ftpRequestResponse)
|
||||
{
|
||||
try
|
||||
{
|
||||
var filesInDirectory = _fileOperationsService.GetListOfFilesInDirectory(winScpUploadFileRequest).Result;
|
||||
if (!filesInDirectory.Any())
|
||||
{
|
||||
ftpRequestResponse.MessageStack.Add($"No files available to be uploaded. " +
|
||||
$"RegexFileName: {winScpUploadFileRequest.LocalFileName}. " +
|
||||
$"Succeed On Empty?: {winScpUploadFileRequest.SucceedOnEmpty}");
|
||||
ftpRequestResponse.IsSuccessful = winScpUploadFileRequest.SucceedOnEmpty;
|
||||
return ftpRequestResponse;
|
||||
}
|
||||
var sessionOptions = WinScpSessionOptionsHelper.CreateSessionOptions(winScpUploadFileRequest.SessionInformation);
|
||||
WinScpFile(sessionOptions, winScpUploadFileRequest.FinalLocalFilePath, winScpUploadFileRequest.FinalRemoteFilePath, filesInDirectory, winScpUploadFileRequest.ResumeSupportStateOn);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
ftpRequestResponse.MessageStack.Add($"Failed to upload file with WinSCP. " +
|
||||
$"Host: {winScpUploadFileRequest.SessionInformation.HostName}, " +
|
||||
$"File: {winScpUploadFileRequest.FinalLocalFilePath}" +
|
||||
$"ExceptionMessage: {exception.Message}");
|
||||
ftpRequestResponse.IsSuccessful = false;
|
||||
}
|
||||
|
||||
return ftpRequestResponse;
|
||||
}
|
||||
|
||||
private void WinScpFile(SessionOptions sessionOptions, string filePath, string RemotePath, List<string> localFileNames, bool supportStateOn)
|
||||
{
|
||||
using (var session = new Session())
|
||||
{
|
||||
session.Open(sessionOptions);
|
||||
var transferOptions = new TransferOptions { TransferMode = TransferMode.Binary, PreserveTimestamp = false };
|
||||
|
||||
if(!supportStateOn)
|
||||
{
|
||||
transferOptions.ResumeSupport.State = TransferResumeSupportState.Off;
|
||||
}
|
||||
|
||||
var addedSlash = filePath.EndsWith(@"\") ? "" : @"\";
|
||||
foreach (string fileName in localFileNames)
|
||||
{
|
||||
var localfilePath = $"{filePath}{addedSlash}{fileName}";
|
||||
var transferResult = session.PutFiles(localfilePath, RemotePath, false, transferOptions);
|
||||
|
||||
transferResult.Check();
|
||||
foreach (TransferEventArgs transfer in transferResult.Transfers)
|
||||
{
|
||||
Log.Debug($"Upload of {transfer.FileName} succeeded");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
using System;
|
||||
using Serilog;
|
||||
using LCU.Library.Internal.ApiResponse.Services;
|
||||
using LCU.Api.Internal.FTP.Configuration;
|
||||
using LCU.Api.Internal.FTPLib.Models.Enums;
|
||||
using LCU.Api.Internal.FTP.Services.FtpOpertionServices;
|
||||
using LCU.Api.Internal.FTPLib.Models;
|
||||
using LCU.Api.Internal.FTP.Helpers;
|
||||
using LCU.Library.Internal.Exceptions;
|
||||
namespace LCU.Api.Internal.FTP.Services;
|
||||
|
||||
public class FtpService : IFtpService
|
||||
{
|
||||
private readonly IQueueFtpService _queueSftpService;
|
||||
private readonly IWinScpSftpUploadFileService _winScpSftpUploadFilesService;
|
||||
private readonly IWinScpSftpDownloadFilesService _winScpSftpDownloadFilesService;
|
||||
private readonly ISshFtpService _sshFtpService;
|
||||
private readonly FtpApiConfiguration _ftpApiConfiguration;
|
||||
|
||||
public FtpService(IQueueFtpService queueSftpService,
|
||||
IWinScpSftpUploadFileService winScpSftpUploadFilesService,
|
||||
IWinScpSftpDownloadFilesService winScpSftpDownloadFilesService,
|
||||
ISshFtpService sshFtpService,
|
||||
FtpApiConfiguration ftpApiConfiguration)
|
||||
{
|
||||
_queueSftpService = queueSftpService;
|
||||
_winScpSftpUploadFilesService = winScpSftpUploadFilesService;
|
||||
_winScpSftpDownloadFilesService = winScpSftpDownloadFilesService;
|
||||
_sshFtpService = sshFtpService;
|
||||
_ftpApiConfiguration = ftpApiConfiguration;
|
||||
}
|
||||
|
||||
public void HandleFtpRequest(QueueRequest queueRequest)
|
||||
{
|
||||
var result = PerformFtpOperation(queueRequest);
|
||||
|
||||
if (ShouldPerformFtpRequestAgain(queueRequest, result))
|
||||
{
|
||||
Log.Information($"Attempting to requeue ftp request. FtpType: {queueRequest.FtpType}");
|
||||
queueRequest.Attempts++;
|
||||
_queueSftpService.QueueFtpRequest(queueRequest);
|
||||
return;
|
||||
}
|
||||
|
||||
QueueReturnResponse.QueueResponse(result.ReturnResponse, result.IsSuccessful, result.MessageStack);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
var errorMessage = String.Join("; ", result.MessageStack.ToArray());
|
||||
Log.Error($"Ftp Request was not successful. MessageStack:{errorMessage}");
|
||||
throw new ServiceException($"Ftp Request was not successful. MessageStack:{errorMessage}");
|
||||
}
|
||||
}
|
||||
|
||||
public FtpRequestResponse PerformFtpOperation(QueueRequest queueRequest)
|
||||
{
|
||||
var ftpRequestResponse = new FtpRequestResponse();
|
||||
try
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(queueRequest);
|
||||
|
||||
ftpRequestResponse = queueRequest.FtpType switch
|
||||
{
|
||||
FtpType.WinScpSftpUpload => _winScpSftpUploadFilesService.ExecuteAction(queueRequest),
|
||||
FtpType.WinScpSftpDownload => _winScpSftpDownloadFilesService.ExecuteAction(queueRequest),
|
||||
FtpType.SshFtp => _sshFtpService.ExecuteAction(queueRequest),
|
||||
_ => throw new NotImplementedException()
|
||||
};
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
var failureMessage = $"Failed to perform ftp request.";
|
||||
Log.Error(exception, failureMessage);
|
||||
|
||||
var fileOperationType = queueRequest?.FtpType;
|
||||
if (fileOperationType == null)
|
||||
{
|
||||
ftpRequestResponse.MessageStack.Add($"{failureMessage}. FtpType is Null. " +
|
||||
$"ExceptionMessage: {exception.Message}");
|
||||
return ftpRequestResponse;
|
||||
}
|
||||
|
||||
ftpRequestResponse = FtpRequestResponseHelper.ExceptionUpdateRequestResponse(ftpRequestResponse, queueRequest.FtpType, exception.Message);
|
||||
}
|
||||
|
||||
return ftpRequestResponse;
|
||||
}
|
||||
|
||||
public bool ShouldPerformFtpRequestAgain(QueueRequest request, FtpRequestResponse ftpRequestResponse)
|
||||
{
|
||||
return !ftpRequestResponse.IsSuccessful &&
|
||||
request.Attempts != null &&
|
||||
request.Attempts < _ftpApiConfiguration.AttemptsLimit;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using System.Threading;
|
||||
using LCU.Api.Internal.FTPLib.Models;
|
||||
namespace LCU.Api.Internal.FTP.Services;
|
||||
|
||||
public interface IFtpService
|
||||
{
|
||||
void HandleFtpRequest(QueueRequest queueRequest);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using LCU.Api.Internal.FTPLib.Models;
|
||||
namespace LCU.Api.Internal.FTP.Services;
|
||||
|
||||
public interface IQueueFtpService
|
||||
{
|
||||
[Obsolete("01/04/2022 - After the SftpController endpoints are no longer in use this can be removed.")]
|
||||
void QueueSftp(SftpRequest request);
|
||||
[Obsolete("01/04/2022 - After the SftpController endpoints are no longer in use this can be removed.")]
|
||||
void QueueSftpDynamicFileRequest(SftpDynamicFileRequest sftpDynamicFileRequest);
|
||||
void QueueSftpUploadRequest(SftpUploadFileRequest winScpUploadFileRequest);
|
||||
void QueueSftpDownloadRequest(SftpDownloadFileRequest winScpDownloadFileRequest);
|
||||
void QueueFtpRequest(QueueRequest queueRequest);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
using LCU.Api.Internal.FTPLib.Models;
|
||||
namespace LCU.Api.Internal.FTP.Services;
|
||||
|
||||
public interface IQueueSshFtpService
|
||||
{
|
||||
void QueueSshFtpRequest(SshFtpRequest request);
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
using Serilog;
|
||||
using MassTransit;
|
||||
using LCU.Api.Internal.FTP.Configuration;
|
||||
using LCU.Api.Internal.FTPLib.Models.Enums;
|
||||
using LCU.Api.Internal.FTPLib.Models;
|
||||
using LCU.Library.Internal.Exceptions;
|
||||
using LCU.Library.Internal.LcuMassTransitModels;
|
||||
namespace LCU.Api.Internal.FTP.Services;
|
||||
|
||||
public class QueueFtpService : IQueueFtpService
|
||||
{
|
||||
private readonly IQueueSshFtpService _queueSshFtpService;
|
||||
private readonly ISendEndpoint _sendEndpoint;
|
||||
|
||||
public QueueFtpService(IQueueSshFtpService queueSshFtpService,
|
||||
IBusControl busControl,
|
||||
ReceiveEndpointConfiguration receiveEndpointsConfiguration)
|
||||
{
|
||||
_queueSshFtpService = queueSshFtpService;
|
||||
_sendEndpoint = busControl.GetSendEndpoint(receiveEndpointsConfiguration.ReceiveEndpointUri).Result;
|
||||
}
|
||||
|
||||
[Obsolete("01/04/2022 - After the SftpController endpoints that call this method are no longer in use this can be removed.")]
|
||||
public void QueueSftp(SftpRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
//If it has localfiledata then we need to use sshftp to accommodate old endpoints.
|
||||
if (request.LocalFileData != null && request.LocalFileData.Length > 0)
|
||||
{
|
||||
var sshFtpRequest = new SshFtpRequest()
|
||||
{
|
||||
Protocol = request.Protocol,
|
||||
HostName = request.HostName,
|
||||
PortNumber = request.PortNumber,
|
||||
UserName = request.UserName,
|
||||
Password = request.Password,
|
||||
RemoteFilePath = request.RemoteFilePath,
|
||||
LocalFileData = request.LocalFileData
|
||||
};
|
||||
|
||||
_queueSshFtpService.QueueSshFtpRequest(sshFtpRequest);
|
||||
return;
|
||||
}
|
||||
|
||||
var winScpUploadFileRequest = new SftpUploadFileRequest()
|
||||
{
|
||||
SessionInformation = new SessionInformation()
|
||||
{
|
||||
Protocol = request.Protocol,
|
||||
HostName = request.HostName,
|
||||
PortNumber = request.PortNumber,
|
||||
UserName = request.UserName,
|
||||
Password = request.Password,
|
||||
SshHostKeyFingerprint = request.SshHostKeyFingerprint
|
||||
},
|
||||
LocalFilePath = request.LocalFilePath,
|
||||
LocalFileName = null,
|
||||
RemoteFilePath = request.RemoteFilePath,
|
||||
DynamicNameSetting = null,
|
||||
ResponseItems = request.ResponseItems
|
||||
};
|
||||
|
||||
var queueRequest = new QueueRequest()
|
||||
{
|
||||
Request = JsonConvert.SerializeObject(winScpUploadFileRequest),
|
||||
FtpType = FtpType.WinScpSftpUpload
|
||||
};
|
||||
|
||||
_sendEndpoint.Send(queueRequest);
|
||||
Log.Debug($"Queued The Following Sftp Request {request}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, $"Failed to Queue Sftp Request. Request: {request}");
|
||||
throw new ServiceException($"Failed to Queue Sftp Request. Request: {request}");
|
||||
}
|
||||
}
|
||||
|
||||
[Obsolete("01/04/2022 - After the SftpController endpoints that call this method are no longer in use this can be removed.")]
|
||||
public void QueueSftpDynamicFileRequest(SftpDynamicFileRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
var winScpUploadFileRequest = new SftpUploadFileRequest()
|
||||
{
|
||||
SessionInformation = new SessionInformation()
|
||||
{
|
||||
Protocol = request.Protocol,
|
||||
HostName = request.HostName,
|
||||
PortNumber = request.PortNumber,
|
||||
UserName = request.UserName,
|
||||
Password = request.Password,
|
||||
SshHostKeyFingerprint = request.SshHostKeyFingerprint
|
||||
},
|
||||
LocalFilePath = request.LocalFilePath,
|
||||
LocalFileName = request.LocalFileName,
|
||||
RemoteFilePath = request.RemoteFilePath,
|
||||
DynamicNameSetting = request.DynamicNameSetting,
|
||||
ResponseItems = request.ResponseItems
|
||||
};
|
||||
|
||||
var queueRequest = new QueueRequest()
|
||||
{
|
||||
Request = JsonConvert.SerializeObject(winScpUploadFileRequest),
|
||||
FtpType = FtpType.WinScpSftpUpload
|
||||
};
|
||||
|
||||
_sendEndpoint.Send(queueRequest);
|
||||
Log.Debug($"Queued The Following Sftp Request to host {request.HostName}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, $"Failed to Queue Sftp dynamic file request. " +
|
||||
$"Host: {request.HostName}");
|
||||
throw new ServiceException($"Failed to Queue Sftp dynamic file request. " +
|
||||
$"Host: {request.HostName}");
|
||||
}
|
||||
}
|
||||
|
||||
public void QueueSftpUploadRequest(SftpUploadFileRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
var queueRequest = new QueueRequest()
|
||||
{
|
||||
Request = JsonConvert.SerializeObject(request),
|
||||
FtpType = FtpType.WinScpSftpUpload
|
||||
};
|
||||
|
||||
_sendEndpoint.Send(queueRequest);
|
||||
Log.Debug($"Queued sftp upload request to " +
|
||||
$"host {request.SessionInformation.HostName}");
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Log.Error(exception, $"Failed to Queue Sftp Upload Request. " +
|
||||
$"Host: {request.SessionInformation.HostName}");
|
||||
throw new ServiceException($"Failed to Queue Sftp Upload Request. " +
|
||||
$"Host: {request.SessionInformation.HostName}");
|
||||
}
|
||||
}
|
||||
|
||||
public void QueueSftpDownloadRequest(SftpDownloadFileRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
var queueRequest = new QueueRequest()
|
||||
{
|
||||
Request = JsonConvert.SerializeObject(request),
|
||||
FtpType = FtpType.WinScpSftpDownload
|
||||
};
|
||||
|
||||
_sendEndpoint.Send(queueRequest);
|
||||
Log.Debug($"Queued sftp download request from " +
|
||||
$"host {request.SessionInformation.HostName}");
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Log.Error(exception, $"Failed to Queue Sftp Download Request. " +
|
||||
$"Host: {request.SessionInformation.HostName}");
|
||||
throw new ServiceException($"Failed to Queue Sftp Download Request. " +
|
||||
$"Host: {request.SessionInformation.HostName}");
|
||||
}
|
||||
}
|
||||
|
||||
public void QueueFtpRequest(QueueRequest queueRequest)
|
||||
{
|
||||
try
|
||||
{
|
||||
_sendEndpoint.Send(queueRequest);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, $"Failed to Queue ftp request. FtpType: {queueRequest.FtpType}");
|
||||
throw new ServiceException($"Failed to Queue ftp request. FtpType: {queueRequest.FtpType}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
using Serilog;
|
||||
using MassTransit;
|
||||
using LCU.Api.Internal.FTP.Configuration;
|
||||
using LCU.Api.Internal.FTPLib.Models.Enums;
|
||||
using LCU.Api.Internal.FTPLib.Models;
|
||||
using LCU.Library.Internal.Exceptions;
|
||||
using LCU.Library.Internal.LcuMassTransitModels;
|
||||
namespace LCU.Api.Internal.FTP.Services;
|
||||
|
||||
public class QueueSshFtpService : IQueueSshFtpService
|
||||
{
|
||||
private readonly ISendEndpoint _sendEndpoint;
|
||||
|
||||
public QueueSshFtpService(IBusControl busControl, ReceiveEndpointConfiguration receiveEndpointsConfiguration)
|
||||
{
|
||||
_sendEndpoint = busControl.GetSendEndpoint(receiveEndpointsConfiguration.ReceiveEndpointUri).Result;
|
||||
}
|
||||
|
||||
public void QueueSshFtpRequest(SshFtpRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
var queueRequest = new QueueRequest();
|
||||
|
||||
queueRequest = new QueueRequest()
|
||||
{
|
||||
Request = JsonConvert.SerializeObject(request),
|
||||
FtpType = FtpType.SshFtp
|
||||
};
|
||||
|
||||
_sendEndpoint.Send(queueRequest);
|
||||
Log.Debug($"Queued ssh ftp request to host {request.HostName}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, $"Failed to queue ssh ftp request to host {request.HostName}");
|
||||
throw new ServiceException($"Failed to queue ssh ftp request to host {request.HostName}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user