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; } }