99 lines
3.9 KiB
C#
99 lines
3.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using Newtonsoft.Json;
|
|
using LCU.Library.Internal.FileOperationsModels.Models.NameSettings;
|
|
using LCU.Library.Internal.ArchiverApi.Utilities;
|
|
using LCU.Library.Internal.FileOperationsModels.Models;
|
|
using LCU.Api.Internal.ArchiverApi.Configuration;
|
|
namespace LCU.Api.Internal.ArchiverApi.Services;
|
|
|
|
public class FileOperationsService : IFileOperationsService
|
|
{
|
|
private readonly ArchiverApiConfig _ArchiverApiConfig;
|
|
|
|
public FileOperationsService(ArchiverApiConfig ArchiverApiConfig)
|
|
{
|
|
_ArchiverApiConfig = ArchiverApiConfig;
|
|
}
|
|
|
|
public void SendBulkOperationsAsync(BulkOperationRequest<MoveFileRequest> bulkOperationRequest)
|
|
{
|
|
try
|
|
{
|
|
var encoded = JsonConvert.SerializeObject(bulkOperationRequest);
|
|
var client = HttpUtilities.GetClient(_ArchiverApiConfig.FileOperationApiBulkMoveFile);
|
|
var content = new StringContent(encoded, Encoding.UTF8, "application/json");
|
|
|
|
HttpResponseMessage response = client.PostAsync(_ArchiverApiConfig.FileOperationApiBulkMoveFile, content).Result;
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
throw new Exception($"IsSuccessStatusCode: {response.IsSuccessStatusCode}");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception($"Failure to use the Bulk Operations. Message{ex.Message}");
|
|
}
|
|
}
|
|
|
|
public async Task<List<string>> BuildDynamicFileListAsync(string path, string file, DynamicNameSetting dynamicNameSetting)
|
|
{
|
|
try
|
|
{
|
|
var dynamicFileListRequest = new DynamicFileListRequest()
|
|
{
|
|
Path = path,
|
|
Filename = file,
|
|
DynamicFileSettings = dynamicNameSetting
|
|
};
|
|
|
|
var encoded = JsonConvert.SerializeObject(dynamicFileListRequest);
|
|
var client = HttpUtilities.GetClient(_ArchiverApiConfig.FileOperationApiBuildDynamicFileList);
|
|
var content = new StringContent(encoded, Encoding.UTF8, "application/json");
|
|
HttpResponseMessage response = await client.PostAsync(_ArchiverApiConfig.FileOperationApiBuildDynamicFileList, content);
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
throw new Exception($"IsSuccessStatusCode: {response.IsSuccessStatusCode}");
|
|
}
|
|
|
|
var stringResponse = await response.Content.ReadAsStringAsync();
|
|
var fileList = JsonConvert.DeserializeObject<List<string>>(stringResponse);
|
|
return fileList;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
throw new Exception($"Unable to build dynamic list of files. Exception Message: {exception}");
|
|
}
|
|
}
|
|
|
|
public async Task<bool> ValidateFileExists(string sourceFilePath)
|
|
{
|
|
try
|
|
{
|
|
var encoded = JsonConvert.SerializeObject(sourceFilePath);
|
|
var client = HttpUtilities.GetClient(_ArchiverApiConfig.FileOperationApiValidateFileExists);
|
|
var content = new StringContent(encoded, Encoding.UTF8, "application/json");
|
|
HttpResponseMessage response = await client.PostAsync(_ArchiverApiConfig.FileOperationApiValidateFileExists, content);
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
throw new Exception($"IsSuccessStatusCode: {response.IsSuccessStatusCode}");
|
|
}
|
|
|
|
var stringResponse = await response.Content.ReadAsStringAsync();
|
|
var result = JsonConvert.DeserializeObject<bool>(stringResponse);
|
|
return result;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
throw new Exception($"Failure to validate files exist. " +
|
|
$"Exception Message:{exception.Message}");
|
|
}
|
|
}
|
|
} |