117 lines
4.4 KiB
C#
117 lines
4.4 KiB
C#
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;
|
|
}
|
|
}
|
|
|