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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user