Updating
This commit is contained in:
+213
@@ -0,0 +1,213 @@
|
||||
using Org.BouncyCastle.Bcpg;
|
||||
using Org.BouncyCastle.Bcpg.OpenPgp;
|
||||
using Org.BouncyCastle.Security;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using Org.BouncyCastle.Utilities.IO;
|
||||
|
||||
namespace LCU.Api.Internal.FileOperations.Services
|
||||
{
|
||||
public static class CryptoHelper
|
||||
{
|
||||
private static PgpPublicKey ReadPublicKey(Stream inputStream)
|
||||
{
|
||||
inputStream = PgpUtilities.GetDecoderStream(inputStream);
|
||||
PgpPublicKeyRingBundle pgpPub = new PgpPublicKeyRingBundle(inputStream);
|
||||
|
||||
foreach (PgpPublicKeyRing keyRing in pgpPub.GetKeyRings())
|
||||
{
|
||||
foreach (PgpPublicKey key in keyRing.GetPublicKeys())
|
||||
{
|
||||
if (key.IsEncryptionKey)
|
||||
{
|
||||
return key;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw new ArgumentException("Can't find encryption key in key ring.");
|
||||
}
|
||||
|
||||
private static PgpPrivateKey FindSecretKey(PgpSecretKeyRingBundle pgpSec, long keyId, char[] pass)
|
||||
{
|
||||
PgpSecretKey pgpSecKey = pgpSec.GetSecretKey(keyId);
|
||||
return pgpSecKey?.ExtractPrivateKey(pass);
|
||||
}
|
||||
|
||||
public static void DecryptPgpFile(string inputFile, string outputFile, string keyFile, string passPhrase)
|
||||
{
|
||||
var encryptedFileData = File.ReadAllBytes(inputFile);
|
||||
var secretKeyData = File.ReadAllBytes(keyFile);
|
||||
var decryptedData = DecryptPgpData(encryptedFileData, secretKeyData, passPhrase);
|
||||
|
||||
File.WriteAllText(outputFile, decryptedData);
|
||||
}
|
||||
|
||||
public static string DecryptPgpData(byte[] inputData, byte[] secretKeyData, string passPhrase)
|
||||
{
|
||||
Stream inputStream = new MemoryStream(inputData);
|
||||
Stream secretKeyStream = new MemoryStream(secretKeyData);
|
||||
|
||||
string output;
|
||||
|
||||
PgpObjectFactory pgpFactory = new PgpObjectFactory(PgpUtilities.GetDecoderStream(inputStream));
|
||||
// find secret key
|
||||
PgpSecretKeyRingBundle pgpKeyRing = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(secretKeyStream));
|
||||
|
||||
PgpObject pgp = null;
|
||||
if (pgpFactory != null)
|
||||
{
|
||||
pgp = pgpFactory.NextPgpObject();
|
||||
}
|
||||
|
||||
// the first object might be a PGP marker packet.
|
||||
PgpEncryptedDataList encryptedData = null;
|
||||
if (pgp is PgpEncryptedDataList)
|
||||
{
|
||||
encryptedData = (PgpEncryptedDataList)pgp;
|
||||
}
|
||||
else
|
||||
{
|
||||
encryptedData = (PgpEncryptedDataList)pgpFactory.NextPgpObject();
|
||||
}
|
||||
|
||||
// decrypt
|
||||
PgpPrivateKey privateKey = null;
|
||||
PgpPublicKeyEncryptedData pubKeyData = null;
|
||||
foreach (PgpPublicKeyEncryptedData pubKeyDataItem in encryptedData.GetEncryptedDataObjects())
|
||||
{
|
||||
privateKey = FindSecretKey(pgpKeyRing, pubKeyDataItem.KeyId, passPhrase.ToCharArray());
|
||||
|
||||
if (privateKey != null)
|
||||
{
|
||||
pubKeyData = pubKeyDataItem;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (privateKey == null)
|
||||
{
|
||||
throw new ArgumentException("Secret key for message not found.");
|
||||
}
|
||||
|
||||
PgpObjectFactory plainFact = null;
|
||||
using (Stream clear = pubKeyData.GetDataStream(privateKey))
|
||||
{
|
||||
plainFact = new PgpObjectFactory(clear);
|
||||
}
|
||||
|
||||
PgpObject message = plainFact.NextPgpObject();
|
||||
|
||||
switch (message)
|
||||
{
|
||||
case PgpCompressedData data:
|
||||
{
|
||||
PgpCompressedData compressedData = data;
|
||||
PgpObjectFactory pgpCompressedFactory = null;
|
||||
|
||||
using (Stream compDataIn = compressedData.GetDataStream())
|
||||
{
|
||||
pgpCompressedFactory = new PgpObjectFactory(compDataIn);
|
||||
}
|
||||
|
||||
message = pgpCompressedFactory.NextPgpObject();
|
||||
PgpLiteralData literalData = null;
|
||||
if (message is PgpOnePassSignatureList)
|
||||
{
|
||||
message = pgpCompressedFactory.NextPgpObject();
|
||||
}
|
||||
|
||||
literalData = (PgpLiteralData)message;
|
||||
using (Stream unc = literalData.GetInputStream())
|
||||
{
|
||||
output = GetString(unc);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case PgpLiteralData data:
|
||||
{
|
||||
PgpLiteralData literalData = data;
|
||||
using (Stream unc = literalData.GetInputStream())
|
||||
{
|
||||
output = GetString(unc);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case PgpOnePassSignatureList _:
|
||||
throw new PgpException("Encrypted message contains a signed message - not literal data.");
|
||||
default:
|
||||
throw new PgpException("Message is not a simple encrypted file - type unknown.");
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
public static void EncryptPgpFile(string inputFile, string outputFile, string publicKeyFile, bool applyArmor, bool withIntegrityCheck)
|
||||
{
|
||||
using (Stream publicKeyStream = File.OpenRead(publicKeyFile))
|
||||
{
|
||||
PgpPublicKey pubKey = ReadPublicKey(publicKeyStream);
|
||||
|
||||
using (MemoryStream outputBytes = new MemoryStream())
|
||||
{
|
||||
PgpCompressedDataGenerator dataCompressor = new PgpCompressedDataGenerator(CompressionAlgorithmTag.Zip);
|
||||
PgpUtilities.WriteFileToLiteralData(dataCompressor.Open(outputBytes), PgpLiteralData.Binary, new FileInfo(inputFile));
|
||||
|
||||
dataCompressor.Close();
|
||||
PgpEncryptedDataGenerator dataGenerator =
|
||||
new PgpEncryptedDataGenerator(SymmetricKeyAlgorithmTag.Cast5, withIntegrityCheck, new SecureRandom());
|
||||
|
||||
dataGenerator.AddMethod(pubKey);
|
||||
byte[] dataBytes = outputBytes.ToArray();
|
||||
|
||||
using (Stream outputStream = File.Create(outputFile))
|
||||
{
|
||||
if (applyArmor)
|
||||
{
|
||||
using (ArmoredOutputStream armoredStream = new ArmoredOutputStream(outputStream))
|
||||
{
|
||||
WriteStream(dataGenerator.Open(armoredStream, dataBytes.Length), ref dataBytes);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
WriteStream(dataGenerator.Open(outputStream, dataBytes.Length), ref dataBytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void WriteStream(Stream inputStream, ref byte[] dataBytes)
|
||||
{
|
||||
using (Stream outputStream = inputStream)
|
||||
{
|
||||
outputStream.Write(dataBytes, 0, dataBytes.Length);
|
||||
}
|
||||
}
|
||||
|
||||
public static string GetString(Stream inputStream)
|
||||
{
|
||||
string output;
|
||||
using (StreamReader reader = new StreamReader(inputStream))
|
||||
{
|
||||
output = reader.ReadToEnd();
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
public static Stream GetStream(string stringData)
|
||||
{
|
||||
MemoryStream stream = new MemoryStream();
|
||||
StreamWriter writer = new StreamWriter(stream);
|
||||
writer.Write(stringData);
|
||||
writer.Flush();
|
||||
stream.Position = 0;
|
||||
return stream;
|
||||
}
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
using Serilog;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.Exceptions;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.Library.Internal.ApiResponse.Services;
|
||||
using LCU.Library.Internal.ApiResponse.Models;
|
||||
|
||||
namespace LCU.Api.Internal.FileOperations.Services;
|
||||
|
||||
public class DirectFileOperationsService : IDirectFileOperationsService
|
||||
{
|
||||
private readonly IFileOperationsService _fileOperationsService;
|
||||
|
||||
public DirectFileOperationsService(IFileOperationsService fileOperationsService)
|
||||
{
|
||||
_fileOperationsService = fileOperationsService;
|
||||
}
|
||||
|
||||
public async Task<RequestResponse> HandleDirectFileOperationsRequest<TRequest>(TRequest request,
|
||||
FileOperationType fileOperationType)
|
||||
where TRequest : ReturnResponse
|
||||
{
|
||||
try
|
||||
{
|
||||
var queueRequest = new QueueRequest
|
||||
{
|
||||
Request = JsonConvert.SerializeObject(request),
|
||||
FileOperationType = fileOperationType,
|
||||
Attempts = 0
|
||||
};
|
||||
|
||||
var requestResponse = _fileOperationsService.PerformFileOperation(queueRequest);
|
||||
await QueueReturnResponse.QueueResponse(request, requestResponse.IsSuccessful, requestResponse.MessageStack);
|
||||
|
||||
return requestResponse;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
var message = "Failure to Handle Direct File Operations Request";
|
||||
Log.Error(exception, message);
|
||||
throw new ServiceException($"{message}, Exception Message: {exception.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace LCU.Api.Internal.FileOperations.Services
|
||||
{
|
||||
public class DirectFileService : IDirectFileService
|
||||
{
|
||||
public byte[] GetFileBytes(string path)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(path) && File.Exists(path))
|
||||
{
|
||||
return File.ReadAllBytes(path);
|
||||
}
|
||||
|
||||
throw new ArgumentException($"Path {path} must exist.");
|
||||
}
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json;
|
||||
using Serilog;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.FileOperationsModels.Shared;
|
||||
using LCU.Library.Internal.Exceptions;
|
||||
namespace LCU.Api.Internal.FileOperations.Services;
|
||||
|
||||
public class DynamicDirectoryService : IDynamicDirectoryService
|
||||
{
|
||||
public IEnumerable<string> BuildDirectoryList(DynamicDirectoryListRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
var dynamicDirectoryNameWithDates = DynamicNameBuilder.BuildDynamicName(request.DynamicDirectoryName, request.DynamicFileSettings);
|
||||
var directoryNameList = RegexNameHandler.BuildDirectoryList(request.Path, dynamicDirectoryNameWithDates, request.DynamicFileSettings);
|
||||
return directoryNameList;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Log.Error(exception, $"Error while trying to build directory list. Request: {JsonConvert.SerializeObject(request)}");
|
||||
throw new ServiceException(exception.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
+119
@@ -0,0 +1,119 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Serilog;
|
||||
using DocumentFormat.OpenXml.Packaging;
|
||||
using DocumentFormat.OpenXml.Spreadsheet;
|
||||
using LCU.Library.Internal.Exceptions;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models.ExcelModels.Responses;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models.ExcelModels.Requests;
|
||||
|
||||
namespace LCU.Api.Internal.FileOperations.Services.ExcelServices;
|
||||
|
||||
public class ExcelService : IExcelService
|
||||
{
|
||||
public ExcelDataResponse ExtractExcelWorkBookData(ExcelDataRequest excelDataRequest)
|
||||
{
|
||||
var excelDataResponse = new ExcelDataResponse();
|
||||
try
|
||||
{
|
||||
var fileBytes = Convert.FromBase64String(excelDataRequest.FileContent);
|
||||
using var memoryStream = new MemoryStream(fileBytes);
|
||||
|
||||
using SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(memoryStream, false);
|
||||
WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
|
||||
Workbook workbook = workbookPart.Workbook;
|
||||
foreach (Sheet sheet in workbook.GetFirstChild<Sheets>().Elements<Sheet>())
|
||||
{
|
||||
SheetRequest sheetRequest = null;
|
||||
if (excelDataRequest.SkipSheetForExtraction(sheet.Name, out sheetRequest))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var sheetResponse = new SheetResponse(sheet.Name);
|
||||
var worksheetPart = (WorksheetPart)workbookPart.GetPartById(sheet.Id);
|
||||
var sheetData = worksheetPart.Worksheet.Elements<SheetData>().FirstOrDefault();
|
||||
if (sheetData == null)
|
||||
{
|
||||
Log.Information($"Sheet named {sheet.Name} does not have any sheet data.");
|
||||
continue;
|
||||
}
|
||||
|
||||
sheetResponse = ExtractRowData(sheetData, sheetRequest, sheetResponse);
|
||||
excelDataResponse.WorkBook.SheetResponses.Add(sheetResponse);
|
||||
}
|
||||
|
||||
return excelDataResponse;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
var message = $"Failure during ExtractExcelWorkBookData. Exception Message: {exception.Message}";
|
||||
Log.Error(message, exception);
|
||||
throw new ServiceException(message);
|
||||
}
|
||||
}
|
||||
|
||||
private SheetResponse ExtractRowData(SheetData sheetData, SheetRequest sheetRequest, SheetResponse sheetResponse)
|
||||
{
|
||||
foreach (var row in sheetData.Elements<Row>())
|
||||
{
|
||||
if (!int.TryParse(row.RowIndex, out int rowIndex))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (sheetRequest.SkipRowForExtraction(rowIndex))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
sheetResponse = ExtractCellData(row, rowIndex, sheetRequest, sheetResponse);
|
||||
}
|
||||
|
||||
return sheetResponse;
|
||||
}
|
||||
|
||||
private SheetResponse ExtractCellData(Row row, int rowIndex, SheetRequest sheetRequest, SheetResponse sheetResponse)
|
||||
{
|
||||
foreach (var cell in row.Elements<Cell>())
|
||||
{
|
||||
var columnName = GetColumnName(cell.CellReference);
|
||||
if (sheetRequest.SkipColumnForExtraction(columnName))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if(cell is null || cell.CellValue is null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var cellResponse = new CellResponse()
|
||||
{
|
||||
Row = rowIndex,
|
||||
ColumnName = columnName,
|
||||
Value = cell.CellValue.InnerText,
|
||||
Formula = cell.CellFormula is null ? string.Empty : cell.CellFormula.InnerText
|
||||
};
|
||||
sheetResponse.CellResponses.Add(cellResponse);
|
||||
}
|
||||
|
||||
return sheetResponse;
|
||||
}
|
||||
|
||||
private static string GetColumnName(string cellReference)
|
||||
{
|
||||
string columnName = "";
|
||||
foreach (char character in cellReference)
|
||||
{
|
||||
if (!char.IsLetter(character))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
columnName += character;
|
||||
}
|
||||
return columnName;
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
using LCU.Library.Internal.FileOperationsModels.Models.ExcelModels.Requests;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models.ExcelModels.Responses;
|
||||
|
||||
namespace LCU.Api.Internal.FileOperations.Services.ExcelServices;
|
||||
|
||||
public interface IExcelService
|
||||
{
|
||||
ExcelDataResponse ExtractExcelWorkBookData(ExcelDataRequest excelDataRequest);
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
using LCU.Api.Internal.FileOperations.Validations;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.LCUServiceFactory.Configurations;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace LCU.Api.Internal.FileOperations.Services.FileActionServices
|
||||
{
|
||||
public class AppendFileService : FileActionService, IFileActionService, IActionService<AppendFileRequest>
|
||||
{
|
||||
public AppendFileService(ServicesFactoryConfiguration servicesFactoryConfiguration)
|
||||
: base(servicesFactoryConfiguration)
|
||||
{
|
||||
}
|
||||
|
||||
public RequestResponse ExecuteAction(QueueRequest queueRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
|
||||
try
|
||||
{
|
||||
var fileRequest = JsonConvert.DeserializeObject<AppendFileRequest>(queueRequest.Request);
|
||||
|
||||
requestResponse = PerformRequest(fileRequest);
|
||||
|
||||
requestResponse.ReturnResponse = fileRequest;
|
||||
return requestResponse;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return RequestResponseHelper.ExceptionUpdateRequestResponse(requestResponse,
|
||||
queueRequest.FileOperationType,
|
||||
exception.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PerformRequest(AppendFileRequest request)
|
||||
{
|
||||
return OperationService.PerformOperation(request, IsRequestValid, AppendFile);
|
||||
}
|
||||
private void AppendFile(AppendFileRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
File.AppendAllText(request.FullFilePath, request.FileData);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception($"Exception thrown while trying to ExecuteRequestAction. " +
|
||||
$"Request: {JsonConvert.SerializeObject(request)}" +
|
||||
$"ExceptionMessage: {exception.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PreQueueValidation(AppendFileRequest originalRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
|
||||
requestResponse = IsRequestValid(originalRequest, requestResponse);
|
||||
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
public RequestResponse IsRequestValid(AppendFileRequest originalRequest, RequestResponse requestResponse)
|
||||
{
|
||||
requestResponse = DirectoryValidation.ValidateDirectoryExists(originalRequest.DestinationPath, requestResponse);
|
||||
|
||||
return requestResponse;
|
||||
}
|
||||
}
|
||||
}
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.LCUServiceFactory.Configurations;
|
||||
namespace LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
||||
|
||||
public class BulkOperationService<TRequest> : FileActionService, IFileActionService, IActionService<BulkOperationRequest<TRequest>>
|
||||
{
|
||||
private readonly IActionService<TRequest> _actionService;
|
||||
|
||||
public BulkOperationService(ServicesFactoryConfiguration servicesFactoryConfiguration, IActionService<TRequest> actionService)
|
||||
: base(servicesFactoryConfiguration)
|
||||
{
|
||||
_actionService = actionService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Bulk Operations was created to always try. This meaning that it should try to attempt
|
||||
/// every operation action. This also means that it will alway try to also return response and
|
||||
/// that it always returns true (Exeption being catastrophic exeption thrown).
|
||||
/// </summary>
|
||||
/// <param name="queueRequest"></param>
|
||||
/// <returns></returns>
|
||||
public RequestResponse ExecuteAction(QueueRequest queueRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
try
|
||||
{
|
||||
var request = JsonConvert.DeserializeObject<BulkOperationRequest<TRequest>>(queueRequest.Request);
|
||||
requestResponse = PerformRequest(request);
|
||||
|
||||
//Bulk Operations just tries so if it gets to this point its sucessful.
|
||||
requestResponse.IsSuccessful = true;
|
||||
requestResponse.ReturnResponse = request;
|
||||
return requestResponse;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return RequestResponseHelper.ExceptionUpdateRequestResponse(requestResponse,
|
||||
queueRequest.FileOperationType,
|
||||
exception.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PerformRequest(BulkOperationRequest<TRequest> request)
|
||||
{
|
||||
return BulkOperation(request);
|
||||
}
|
||||
|
||||
private RequestResponse BulkOperation(BulkOperationRequest<TRequest> request)
|
||||
{
|
||||
var finalRequestResponse = new RequestResponse();
|
||||
foreach (var operationRequest in request.OperationRequests)
|
||||
{
|
||||
var requestResponse = _actionService.PerformRequest(operationRequest);
|
||||
if (!requestResponse.IsSuccessful)
|
||||
{
|
||||
finalRequestResponse.IsSuccessful = requestResponse.IsSuccessful;
|
||||
finalRequestResponse.MessageStack.AddRange(requestResponse.MessageStack);
|
||||
}
|
||||
}
|
||||
|
||||
return finalRequestResponse;
|
||||
}
|
||||
|
||||
public RequestResponse PreQueueValidation(BulkOperationRequest<TRequest> originalRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
requestResponse = IsRequestValid(originalRequest, requestResponse);
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
public RequestResponse IsRequestValid(BulkOperationRequest<TRequest> request, RequestResponse requestResponse)
|
||||
{
|
||||
var anyOperationRequests = request.OperationRequests.Any();
|
||||
if (!anyOperationRequests)
|
||||
{
|
||||
requestResponse.IsSuccessful = anyOperationRequests;
|
||||
requestResponse.MessageStack.Add($"There are no bulk operation requests to perform.");
|
||||
}
|
||||
|
||||
return requestResponse;
|
||||
}
|
||||
}
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json;
|
||||
using LCU.Library.Internal.FileOperationsModels.Shared;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.LCUServiceFactory.Configurations;
|
||||
using LCU.Api.Internal.FileOperations.Validations;
|
||||
namespace LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
||||
|
||||
public class CopyFileService : FileActionService, IFileActionService, IActionService<CopyFileRequest>
|
||||
{
|
||||
public CopyFileService(ServicesFactoryConfiguration servicesFactoryConfiguration)
|
||||
: base(servicesFactoryConfiguration)
|
||||
{
|
||||
}
|
||||
|
||||
public RequestResponse ExecuteAction(QueueRequest queueRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
try
|
||||
{
|
||||
var fileRequest = JsonConvert.DeserializeObject<CopyFileRequest>(queueRequest.Request);
|
||||
var requests = DynamicNameBuilder.BuildListOfRequest(fileRequest);
|
||||
requestResponse = FileValidation.ValidateIfAnyRequest(requests, fileRequest.Source, fileRequest.SucceedOnError, requestResponse);
|
||||
|
||||
foreach (var request in requests)
|
||||
{
|
||||
var temporaryRequestResponse = PerformRequest(request);
|
||||
if (!temporaryRequestResponse.IsSuccessful)
|
||||
{
|
||||
requestResponse.IsSuccessful = fileRequest.SucceedOnError;
|
||||
if (fileRequest.SucceedOnError)
|
||||
{
|
||||
requestResponse.MessageStack.Add("Succeed On Error is set.");
|
||||
}
|
||||
|
||||
requestResponse.MessageStack.AddRange(temporaryRequestResponse.MessageStack);
|
||||
}
|
||||
}
|
||||
|
||||
requestResponse.ReturnResponse = fileRequest;
|
||||
return requestResponse;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return RequestResponseHelper.ExceptionUpdateRequestResponse(requestResponse,
|
||||
queueRequest.FileOperationType,
|
||||
exception.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PerformRequest(CopyFileRequest request)
|
||||
{
|
||||
return OperationService.PerformOperation(request, IsRequestValid, CopyFile);
|
||||
}
|
||||
|
||||
private void CopyFile(CopyFileRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Copy(request.Source, request.Destination, request.Overwrite);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception($"Exception thrown while trying to ExecuteRequestAction. " +
|
||||
$"Request: {JsonConvert.SerializeObject(request)}" +
|
||||
$"ExceptionMessage: {exception.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PreQueueValidation(CopyFileRequest originalRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
var requestList = DynamicNameBuilder.BuildListOfRequest(originalRequest);
|
||||
requestResponse = FileValidation.ValidateIfAnyRequest(requestList, originalRequest.SourcePath, originalRequest.SucceedOnError, requestResponse);
|
||||
foreach (var request in requestList)
|
||||
{
|
||||
requestResponse = IsRequestValid(request, requestResponse);
|
||||
}
|
||||
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
|
||||
public RequestResponse IsRequestValid(CopyFileRequest fileRequest, RequestResponse requestResponse)
|
||||
{
|
||||
if (fileRequest.SucceedOnError)
|
||||
{
|
||||
return requestResponse;
|
||||
}
|
||||
requestResponse = FileValidation.ValidateSourceNotEmpty(fileRequest.Source, requestResponse);
|
||||
requestResponse = FileValidation.ValidateFileExists(fileRequest.Source, requestResponse);
|
||||
|
||||
requestResponse = FileValidation.ValidateDestinationNotEmpty(fileRequest.Destination, requestResponse);
|
||||
requestResponse = FileValidation.ValidateDirectoryExists(fileRequest.DestinationPath, requestResponse);
|
||||
|
||||
if (!fileRequest.Overwrite)
|
||||
{
|
||||
requestResponse = FileValidation.ValidateFileNotExists(fileRequest.Destination, requestResponse);
|
||||
}
|
||||
|
||||
return requestResponse;
|
||||
}
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.LCUServiceFactory.Configurations;
|
||||
using LCU.Api.Internal.FileOperations.Validations;
|
||||
namespace LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
||||
|
||||
public class CreateDirectoryService : FileActionService, IFileActionService, IActionService<CreateDirectoryRequest>
|
||||
{
|
||||
public CreateDirectoryService(ServicesFactoryConfiguration servicesFactoryConfiguration)
|
||||
: base(servicesFactoryConfiguration)
|
||||
{
|
||||
}
|
||||
|
||||
public RequestResponse ExecuteAction(QueueRequest queueRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
try
|
||||
{
|
||||
var directoryRequest = JsonConvert.DeserializeObject<CreateDirectoryRequest>(queueRequest.Request);
|
||||
requestResponse = PerformRequest(directoryRequest);
|
||||
requestResponse.ReturnResponse = directoryRequest;
|
||||
return requestResponse;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return RequestResponseHelper.ExceptionUpdateRequestResponse(requestResponse,
|
||||
queueRequest.FileOperationType,
|
||||
exception.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PerformRequest(CreateDirectoryRequest request)
|
||||
{
|
||||
return OperationService.PerformOperation(request, IsRequestValid, CreateDirectory);
|
||||
}
|
||||
|
||||
private void CreateDirectory(CreateDirectoryRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(request.DirectoryDestination);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception($"Exception thrown while trying to ExecuteRequestAction. " +
|
||||
$"Request: {JsonConvert.SerializeObject(request)}" +
|
||||
$"ExceptionMessage: {exception.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PreQueueValidation(CreateDirectoryRequest originalRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
requestResponse = IsRequestValid(originalRequest, requestResponse);
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
public RequestResponse IsRequestValid(CreateDirectoryRequest request, RequestResponse requestResponse)
|
||||
{
|
||||
requestResponse = FileValidation.ValidateDirectoryExists(request.DirectoryPath, requestResponse);
|
||||
requestResponse = FileValidation.ValidateDirectoryNotExists(request.DirectoryDestination, requestResponse);
|
||||
return requestResponse;
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.LCUServiceFactory.Configurations;
|
||||
using LCU.Api.Internal.FileOperations.Validations;
|
||||
namespace LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
||||
|
||||
public class DecryptFileDataService : FileActionService, IFileActionService, IActionService<DecryptFileDataRequest>
|
||||
{
|
||||
public DecryptFileDataService(ServicesFactoryConfiguration servicesFactoryConfiguration)
|
||||
: base(servicesFactoryConfiguration)
|
||||
{
|
||||
}
|
||||
|
||||
public RequestResponse ExecuteAction(QueueRequest queueRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
try
|
||||
{
|
||||
var request = JsonConvert.DeserializeObject<DecryptFileDataRequest>(queueRequest.Request);
|
||||
requestResponse = PerformRequest(request);
|
||||
requestResponse.ReturnResponse = request;
|
||||
return requestResponse;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return RequestResponseHelper.ExceptionUpdateRequestResponse(requestResponse,
|
||||
queueRequest.FileOperationType,
|
||||
exception.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PerformRequest(DecryptFileDataRequest request)
|
||||
{
|
||||
return OperationService.PerformOperation(request, IsRequestValid, ExecuteRequestAction);
|
||||
}
|
||||
|
||||
private void ExecuteRequestAction(DecryptFileDataRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
CryptoHelper.DecryptPgpFile(request.Source, request.Destination, request.KeySource, request.PassPhrase);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception($"Exception thrown while trying to ExecuteRequestAction. " +
|
||||
$"Request: {JsonConvert.SerializeObject(request)}" +
|
||||
$"ExceptionMessage: {exception.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PreQueueValidation(DecryptFileDataRequest originalRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
requestResponse = IsRequestValid(originalRequest, requestResponse);
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
public RequestResponse IsRequestValid(DecryptFileDataRequest fileRequest, RequestResponse requestResponse)
|
||||
{
|
||||
requestResponse = FileValidation.ValidateDirectoryExists(fileRequest.SourcePath, requestResponse);
|
||||
requestResponse = FileValidation.ValidateDirectoryExists(fileRequest.DestinationPath, requestResponse);
|
||||
requestResponse = FileValidation.ValidateDirectoryExists(fileRequest.KeySourcePath, requestResponse);
|
||||
requestResponse = FileValidation.ValidateFileExists(fileRequest.Source, requestResponse);
|
||||
requestResponse = FileValidation.ValidateFileExists(fileRequest.KeySource, requestResponse);
|
||||
return requestResponse;
|
||||
}
|
||||
}
|
||||
+123
@@ -0,0 +1,123 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.FileOperationsModels.Shared;
|
||||
using LCU.Api.Internal.FileOperations.Validations;
|
||||
using LCU.Library.Internal.LCUServiceFactory.Configurations;
|
||||
namespace LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
||||
|
||||
public class DeleteDirectoryService : FileActionService, IFileActionService, IActionService<DeleteDirectoryRequest>
|
||||
{
|
||||
public DeleteDirectoryService(ServicesFactoryConfiguration servicesFactoryConfiguration)
|
||||
: base(servicesFactoryConfiguration)
|
||||
{
|
||||
}
|
||||
|
||||
public RequestResponse ExecuteAction(QueueRequest queueRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
try
|
||||
{
|
||||
var directoryRequest = JsonConvert.DeserializeObject<DeleteDirectoryRequest>(queueRequest.Request);
|
||||
|
||||
var requests = BuildListOfRequest(directoryRequest);
|
||||
requestResponse = DirectoryValidation.ValidateDirectoryRequestsWithinLimit(requests,
|
||||
directoryRequest.MaxAmountOfDirectories,
|
||||
requestResponse);
|
||||
if (!requestResponse.IsSuccessful)
|
||||
{
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
foreach (var request in requests)
|
||||
{
|
||||
var temporaryRequestResponse = PerformRequest(request);
|
||||
if (!temporaryRequestResponse.IsSuccessful)
|
||||
{
|
||||
requestResponse.IsSuccessful = temporaryRequestResponse.IsSuccessful;
|
||||
requestResponse.MessageStack.AddRange(temporaryRequestResponse.MessageStack);
|
||||
}
|
||||
}
|
||||
|
||||
requestResponse.ReturnResponse = directoryRequest;
|
||||
return requestResponse;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return RequestResponseHelper.ExceptionUpdateRequestResponse(requestResponse,
|
||||
queueRequest.FileOperationType,
|
||||
exception.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PerformRequest(DeleteDirectoryRequest request)
|
||||
{
|
||||
return OperationService.PerformOperation(request, IsRequestValid, DeleteDirectory);
|
||||
}
|
||||
|
||||
private void DeleteDirectory(DeleteDirectoryRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
Directory.Delete(request.FinalDirectoryPath, request.Recursive);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception($"Exception thrown while trying to ExecuteRequestAction. " +
|
||||
$"Request: {JsonConvert.SerializeObject(request)}" +
|
||||
$"ExceptionMessage: {exception.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PreQueueValidation(DeleteDirectoryRequest originalRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
requestResponse = DirectoryValidation.ValidateDirectoryExists(originalRequest.DirectoryRootPath, requestResponse);
|
||||
if (!requestResponse.IsSuccessful)
|
||||
{
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
var requestList = BuildListOfRequest(originalRequest);
|
||||
requestResponse = DirectoryValidation.ValidateDirectoryRequestsWithinLimit(requestList,
|
||||
originalRequest.MaxAmountOfDirectories,
|
||||
requestResponse);
|
||||
if (!requestResponse.IsSuccessful)
|
||||
{
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
foreach (var request in requestList)
|
||||
{
|
||||
requestResponse = IsRequestValid(request, requestResponse);
|
||||
}
|
||||
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
public List<DeleteDirectoryRequest> BuildListOfRequest(DeleteDirectoryRequest originalRequest)
|
||||
{
|
||||
var nameList = RegexNameHandler.BuildDirectoryList(originalRequest.DirectoryRootPath,
|
||||
originalRequest.FinalDirectoryName,
|
||||
originalRequest.DirectoryNameSettings);
|
||||
var requests = new List<DeleteDirectoryRequest>();
|
||||
|
||||
foreach (var name in nameList)
|
||||
{
|
||||
var newRequest = Clone.DeepClone(originalRequest);
|
||||
newRequest.DirectoryName = name;
|
||||
|
||||
requests.Add(newRequest);
|
||||
}
|
||||
|
||||
return requests;
|
||||
}
|
||||
|
||||
public RequestResponse IsRequestValid(DeleteDirectoryRequest request, RequestResponse requestResponse)
|
||||
{
|
||||
requestResponse = DirectoryValidation.ValidateDirectoryExists(request.FinalDirectoryPath, requestResponse);
|
||||
return requestResponse;
|
||||
}
|
||||
}
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using LCU.Api.Internal.FileOperations.Validations;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.FileOperationsModels.Shared;
|
||||
using LCU.Library.Internal.LCUServiceFactory.Configurations;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace LCU.Api.Internal.FileOperations.Services.FileActionServices
|
||||
{
|
||||
public class DeleteEmptyDirectoryService : FileActionService, IFileActionService, IActionService<DeleteEmptyDirectoryRequest>
|
||||
{
|
||||
public DeleteEmptyDirectoryService(ServicesFactoryConfiguration servicesFactoryConfiguration)
|
||||
: base(servicesFactoryConfiguration)
|
||||
{
|
||||
}
|
||||
|
||||
public RequestResponse ExecuteAction(QueueRequest queueRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
try
|
||||
{
|
||||
var directoryRequest = JsonConvert.DeserializeObject<DeleteEmptyDirectoryRequest>(queueRequest.Request);
|
||||
|
||||
var requests = BuildListOfRequest(directoryRequest);
|
||||
|
||||
foreach (var request in requests)
|
||||
{
|
||||
if (DirectoryValidation.ValidateDirectoryIsEmpty(request.FinalDirectoryPath, new RequestResponse()).IsSuccessful)
|
||||
{
|
||||
var temporaryRequestResponse = PerformRequest(request);
|
||||
|
||||
if (!temporaryRequestResponse.IsSuccessful)
|
||||
{
|
||||
requestResponse.IsSuccessful = temporaryRequestResponse.IsSuccessful;
|
||||
requestResponse.MessageStack.AddRange(temporaryRequestResponse.MessageStack);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
requestResponse.MessageStack.Add($"Directory {request.FinalDirectoryPath} is skipped as it is not empty and won't be deleted.");
|
||||
}
|
||||
}
|
||||
|
||||
requestResponse.ReturnResponse = directoryRequest;
|
||||
return requestResponse;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
|
||||
return RequestResponseHelper.ExceptionUpdateRequestResponse(requestResponse,
|
||||
queueRequest.FileOperationType,
|
||||
exception.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PerformRequest(DeleteEmptyDirectoryRequest request)
|
||||
{
|
||||
return OperationService.PerformOperation(request, IsRequestValid, DeleteEmptyDirectory);
|
||||
}
|
||||
|
||||
private void DeleteEmptyDirectory(DeleteEmptyDirectoryRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
Directory.Delete(request.FinalDirectoryPath);
|
||||
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception($"Exception thrown while trying to ExecuteRequestAction. " +
|
||||
$"Request: {JsonConvert.SerializeObject(request)}" +
|
||||
$"ExceptionMessage: {exception.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PreQueueValidation(DeleteEmptyDirectoryRequest originalRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
requestResponse = DirectoryValidation.ValidateDirectoryExists(originalRequest.SubDirectoryPath, requestResponse);
|
||||
if (!requestResponse.IsSuccessful)
|
||||
{
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
var requestList = BuildListOfRequest(originalRequest);
|
||||
|
||||
foreach (var request in requestList)
|
||||
{
|
||||
requestResponse = IsRequestValid(request, requestResponse);
|
||||
}
|
||||
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
public RequestResponse IsRequestValid(DeleteEmptyDirectoryRequest request, RequestResponse requestResponse)
|
||||
{
|
||||
requestResponse = DirectoryValidation.ValidateDirectoryExists(request.FinalDirectoryPath, requestResponse);
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
private List<DeleteEmptyDirectoryRequest> BuildListOfRequest(DeleteEmptyDirectoryRequest originalRequest)
|
||||
{
|
||||
var nameList = RegexNameHandler.BuildDirectoryList(originalRequest.SubDirectoryPath,
|
||||
originalRequest.FinalDirectoryName,
|
||||
originalRequest.DirectoryNameSettings);
|
||||
|
||||
var requests = new List<DeleteEmptyDirectoryRequest>();
|
||||
|
||||
foreach (var name in nameList)
|
||||
{
|
||||
var newRequest = Clone.DeepClone(originalRequest);
|
||||
newRequest.DynamicDirectoryName = name;
|
||||
|
||||
requests.Add(newRequest);
|
||||
}
|
||||
|
||||
return requests;
|
||||
}
|
||||
}
|
||||
}
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.FileOperationsModels.Shared;
|
||||
using LCU.Library.Internal.LCUServiceFactory.Configurations;
|
||||
using LCU.Api.Internal.FileOperations.Validations;
|
||||
namespace LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
||||
|
||||
public class DeleteFileService : FileActionService, IFileActionService, IActionService<DeleteFileRequest>
|
||||
{
|
||||
public DeleteFileService(ServicesFactoryConfiguration servicesFactoryConfiguration)
|
||||
: base(servicesFactoryConfiguration)
|
||||
{
|
||||
}
|
||||
|
||||
public RequestResponse ExecuteAction(QueueRequest queueRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
try
|
||||
{
|
||||
var fileRequest = JsonConvert.DeserializeObject<DeleteFileRequest>(queueRequest.Request);
|
||||
var requests = BuildListOfRequest(fileRequest);
|
||||
requestResponse = FileValidation.ValidateIfAnyRequest(requests, fileRequest.Source, requestResponse);
|
||||
|
||||
foreach (var request in requests)
|
||||
{
|
||||
var temporaryRequestResponse = PerformRequest(request);
|
||||
if (!temporaryRequestResponse.IsSuccessful)
|
||||
{
|
||||
requestResponse.IsSuccessful = temporaryRequestResponse.IsSuccessful;
|
||||
requestResponse.MessageStack.AddRange(temporaryRequestResponse.MessageStack);
|
||||
}
|
||||
}
|
||||
|
||||
requestResponse.ReturnResponse = fileRequest;
|
||||
return requestResponse;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return RequestResponseHelper.ExceptionUpdateRequestResponse(requestResponse,
|
||||
queueRequest.FileOperationType,
|
||||
exception.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PerformRequest(DeleteFileRequest request)
|
||||
{
|
||||
return OperationService.PerformOperation(request, IsRequestValid, DeleteFile);
|
||||
}
|
||||
|
||||
private void DeleteFile(DeleteFileRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Delete(request.Source);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception($"Exception thrown while trying to ExecuteRequestAction. " +
|
||||
$"Request: {JsonConvert.SerializeObject(request)}" +
|
||||
$"ExceptionMessage: {exception.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PreQueueValidation(DeleteFileRequest originalRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
var requestList = BuildListOfRequest(originalRequest);
|
||||
requestResponse = FileValidation.ValidateIfAnyRequest(requestList, originalRequest.SourcePath, requestResponse);
|
||||
foreach (var request in requestList)
|
||||
{
|
||||
requestResponse = IsRequestValid(request, requestResponse);
|
||||
}
|
||||
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
public List<DeleteFileRequest> BuildListOfRequest(DeleteFileRequest originalRequest)
|
||||
{
|
||||
var nameList = RegexNameHandler.BuildFileList(originalRequest.SourcePath,
|
||||
originalRequest.FinalSourceFile,
|
||||
originalRequest.FileNameSetting);
|
||||
var requests = new List<DeleteFileRequest>();
|
||||
|
||||
foreach (var name in nameList)
|
||||
{
|
||||
var newRequest = Clone.DeepClone(originalRequest);
|
||||
newRequest.SourceFile = name;
|
||||
|
||||
requests.Add(newRequest);
|
||||
}
|
||||
|
||||
return requests;
|
||||
}
|
||||
|
||||
public RequestResponse IsRequestValid(DeleteFileRequest fileRequest, RequestResponse requestResponse)
|
||||
{
|
||||
if (fileRequest.SucceedOnError)
|
||||
{
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
requestResponse = FileValidation.ValidateSourceNotEmpty(fileRequest.Source, requestResponse);
|
||||
requestResponse = FileValidation.ValidateFileExists(fileRequest.Source, requestResponse);
|
||||
return requestResponse;
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.LCUServiceFactory.Configurations;
|
||||
using LCU.Api.Internal.FileOperations.Validations;
|
||||
namespace LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
||||
|
||||
public class EncryptFileDataService : FileActionService, IFileActionService, IActionService<EncryptFileDataRequest>
|
||||
{
|
||||
public EncryptFileDataService(ServicesFactoryConfiguration servicesFactoryConfiguration)
|
||||
: base(servicesFactoryConfiguration)
|
||||
{
|
||||
}
|
||||
|
||||
public RequestResponse ExecuteAction(QueueRequest queueRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
try
|
||||
{
|
||||
var request = JsonConvert.DeserializeObject<EncryptFileDataRequest>(queueRequest.Request);
|
||||
requestResponse = PerformRequest(request);
|
||||
requestResponse.ReturnResponse = request;
|
||||
return requestResponse;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return RequestResponseHelper.ExceptionUpdateRequestResponse(requestResponse,
|
||||
queueRequest.FileOperationType,
|
||||
exception.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PerformRequest(EncryptFileDataRequest request)
|
||||
{
|
||||
return OperationService.PerformOperation(request, IsRequestValid, ExecuteRequestAction);
|
||||
}
|
||||
|
||||
private void ExecuteRequestAction(EncryptFileDataRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
CryptoHelper.EncryptPgpFile(request.Source, request.Destination, request.KeySource, true, true);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception($"Exception thrown while trying to ExecuteRequestAction. " +
|
||||
$"Request: {JsonConvert.SerializeObject(request)}" +
|
||||
$"ExceptionMessage: {exception.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PreQueueValidation(EncryptFileDataRequest originalRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
requestResponse = IsRequestValid(originalRequest, requestResponse);
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
public RequestResponse IsRequestValid(EncryptFileDataRequest fileRequest, RequestResponse requestResponse)
|
||||
{
|
||||
requestResponse = FileValidation.ValidateDirectoryExists(fileRequest.SourcePath, requestResponse);
|
||||
requestResponse = FileValidation.ValidateDirectoryExists(fileRequest.DestinationPath, requestResponse);
|
||||
requestResponse = FileValidation.ValidateDirectoryExists(fileRequest.KeySourcePath, requestResponse);
|
||||
requestResponse = FileValidation.ValidateFileExists(fileRequest.Source, requestResponse);
|
||||
requestResponse = FileValidation.ValidateFileExists(fileRequest.KeySource, requestResponse);
|
||||
return requestResponse;
|
||||
}
|
||||
}
|
||||
+262
@@ -0,0 +1,262 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using CsvHelper;
|
||||
using CsvHelper.Configuration;
|
||||
using CsvHelper.Configuration.Attributes;
|
||||
using LCU.Api.Internal.FileOperations.Validations;
|
||||
using LCU.Api.Internal.FileOperations.Models;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.FileOperationsModels.Shared;
|
||||
using LCU.Library.Internal.LcuEvaluation.Services.EvaluationServices;
|
||||
using LCU.Library.Internal.LCUServiceFactory.Configurations;
|
||||
using LCU.Library.Internal.TypeExtentions.EnumExtensions;
|
||||
using Newtonsoft.Json;
|
||||
using Serilog;
|
||||
|
||||
namespace LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
||||
|
||||
public class ExtractIndexFileDataService : FileActionService, IFileActionService, IActionService<ExtractIndexFileDataRequest>
|
||||
{
|
||||
private readonly IRunRuleService _runRuleService;
|
||||
private const string RegexMatch = "(\")(.*?)(\")";
|
||||
|
||||
public ExtractIndexFileDataService(IRunRuleService runRuleService, ServicesFactoryConfiguration servicesFactoryConfiguration)
|
||||
: base(servicesFactoryConfiguration)
|
||||
{
|
||||
_runRuleService = runRuleService;
|
||||
}
|
||||
|
||||
public RequestResponse ExecuteAction(QueueRequest queueRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
try
|
||||
{
|
||||
var request = JsonConvert.DeserializeObject<ExtractIndexFileDataRequest>(queueRequest.Request);
|
||||
var requests = DynamicNameBuilder.BuildListOfRequest(request);
|
||||
|
||||
if (requests.Count > 1)
|
||||
{
|
||||
requestResponse.MessageStack.Add("More than one file was found.");
|
||||
requestResponse.IsSuccessful = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
requestResponse = PerformRequest(requests.FirstOrDefault());
|
||||
}
|
||||
|
||||
requestResponse.ReturnResponse = requests.FirstOrDefault();
|
||||
return requestResponse;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return RequestResponseHelper.ExceptionUpdateRequestResponse(requestResponse,
|
||||
queueRequest.FileOperationType,
|
||||
exception.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PerformRequest(ExtractIndexFileDataRequest request)
|
||||
{
|
||||
return OperationService.PerformOperation(request, IsRequestValid, ExtractIndexFileData);
|
||||
}
|
||||
|
||||
public RequestResponse PreQueueValidation(ExtractIndexFileDataRequest originalRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
public RequestResponse IsRequestValid(ExtractIndexFileDataRequest request, RequestResponse requestResponse)
|
||||
{
|
||||
requestResponse = FileValidation.ValidateFileExists(request.FinalIndexFilePath, requestResponse);
|
||||
requestResponse = DirectoryValidation.ValidateDirectoryExists(request.FinalDestinationPath, requestResponse);
|
||||
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
private void ExtractIndexFileData(ExtractIndexFileDataRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
var delimiter = request.DelimiterType.GetNameFromValue();
|
||||
|
||||
var fileLines = GetFileContents(request.FinalIndexFilePath);
|
||||
|
||||
if (fileLines is null || !fileLines.Any())
|
||||
{
|
||||
Log.Information($"No content was extracted from filepath: ({request.FinalIndexFilePath})");
|
||||
return;
|
||||
}
|
||||
|
||||
var recordsAreWrappedInQuotes = HasQuotes(fileLines);
|
||||
var headers = request.IndexHasHeaders ? GetHeaders(fileLines, delimiter, recordsAreWrappedInQuotes) : new List<string>();
|
||||
var columnMax = GetColumnCount(fileLines, delimiter, recordsAreWrappedInQuotes);
|
||||
var fileContent = string.Join(Environment.NewLine, fileLines);
|
||||
var indexEvaluationRequests = GetCsvRecords(fileContent,
|
||||
delimiter,
|
||||
request.IndexHasHeaders);
|
||||
var evaluatedRequests = BuildAndRunRules(indexEvaluationRequests, request);
|
||||
|
||||
var passedList = FilterColumns(evaluatedRequests.Passed,
|
||||
request.IndexesToExtract,
|
||||
columnMax,
|
||||
request.ExtractAllIndexes);
|
||||
var failedList = FilterColumns(evaluatedRequests.Failed,
|
||||
request.IndexesToExtract,
|
||||
columnMax,
|
||||
true);
|
||||
|
||||
if (passedList is null || !passedList.Any())
|
||||
{
|
||||
Log.Information("No results passed evaluations to write to destination file.");
|
||||
return;
|
||||
}
|
||||
|
||||
OutputResults(request, failedList, passedList, headers, recordsAreWrappedInQuotes);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception(@$"Exception thrown while trying to ExecuteRequestAction.
|
||||
ExceptionMessage: {exception.Message}
|
||||
Request: {JsonConvert.SerializeObject(request)}");
|
||||
}
|
||||
}
|
||||
|
||||
private (List<IndexEvaluationRequest> Passed, List<IndexEvaluationRequest> Failed) BuildAndRunRules(List<IndexEvaluationRequest> indexEvaluationRequests, ExtractIndexFileDataRequest request)
|
||||
{
|
||||
if (request.EvaluationRule is null)
|
||||
{
|
||||
return (indexEvaluationRequests, new List<IndexEvaluationRequest>());
|
||||
}
|
||||
|
||||
var passedRules = new List<IndexEvaluationRequest>();
|
||||
var failedRules = new List<IndexEvaluationRequest>();
|
||||
|
||||
foreach (var indexEvaluationRequest in indexEvaluationRequests)
|
||||
{
|
||||
if (_runRuleService.BuildAndRunRule(new List<IndexEvaluationRequest> { indexEvaluationRequest }, request.EvaluationRule))
|
||||
{
|
||||
passedRules.Add(indexEvaluationRequest);
|
||||
}
|
||||
else
|
||||
{
|
||||
failedRules.Add(indexEvaluationRequest);
|
||||
}
|
||||
}
|
||||
|
||||
return (passedRules, failedRules);
|
||||
}
|
||||
|
||||
private static List<string> GetFileContents(string filepath)
|
||||
=> File.ReadAllLines(filepath).Where(line => !string.IsNullOrWhiteSpace(line)).ToList();
|
||||
|
||||
private static bool HasQuotes(List<string> content)
|
||||
=> Regex.Split(content.First(), RegexMatch).Length > 1;
|
||||
|
||||
private static List<string> GetHeaders(List<string> content, string delimiter, bool recordsAreWrappedInQuotes)
|
||||
=> recordsAreWrappedInQuotes ? Regex.Split(content.First(), RegexMatch).Where(x => !new List<string> { delimiter, "\"" }.Contains(x)
|
||||
&& !string.IsNullOrWhiteSpace(x)).ToList()
|
||||
: content.First().Split(delimiter).ToList();
|
||||
|
||||
private static int GetColumnCount(List<string> content, string delimiter, bool recordsAreWrappedInQuotes)
|
||||
{
|
||||
var line = content.First();
|
||||
|
||||
if (!recordsAreWrappedInQuotes)
|
||||
{
|
||||
return line.Split(delimiter).Length;
|
||||
}
|
||||
|
||||
var filterList = new List<string> { string.Empty, "\"", delimiter };
|
||||
return Regex.Split(line, RegexMatch).Where(x => !filterList.Contains(x)).ToList().Count;
|
||||
}
|
||||
|
||||
private static List<IndexEvaluationRequest> GetCsvRecords(string fileContent, string delimiter, bool hasHeaders)
|
||||
{
|
||||
using var stringReader = new StringReader(fileContent);
|
||||
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
|
||||
{
|
||||
Delimiter = delimiter,
|
||||
MissingFieldFound = null,
|
||||
HasHeaderRecord = hasHeaders,
|
||||
BadDataFound = null
|
||||
};
|
||||
|
||||
using var csvReader = new CsvReader(stringReader, config);
|
||||
return csvReader.GetRecords<IndexEvaluationRequest>().ToList();
|
||||
}
|
||||
|
||||
private static List<List<string>> FilterColumns(List<IndexEvaluationRequest> records,
|
||||
List<int> indexesToExtract,
|
||||
int columnMax,
|
||||
bool extractAllIndexes)
|
||||
{
|
||||
if (!extractAllIndexes && (indexesToExtract is null || !indexesToExtract.Any()))
|
||||
{
|
||||
throw new Exception("IndexesToExtract was empty");
|
||||
}
|
||||
|
||||
var keepProperties = new List<string>();
|
||||
var removeProperties = new List<string>();
|
||||
|
||||
var indexes = typeof(IndexEvaluationRequest).GetProperties().ToDictionary(
|
||||
prop => ((IndexAttribute)prop.GetCustomAttributes(true).FirstOrDefault(attr => attr.GetType() == typeof(IndexAttribute)))?.Index ?? int.MaxValue,
|
||||
prop => prop.Name);
|
||||
|
||||
var recordsDictionary = JsonConvert.DeserializeObject<List<Dictionary<string,string>>>(JsonConvert.SerializeObject(records));
|
||||
|
||||
keepProperties.AddRange(indexes.Where(index => extractAllIndexes ? index.Key < columnMax : indexesToExtract.Contains(index.Key))
|
||||
.Select(x => x.Value).Distinct());
|
||||
|
||||
return recordsDictionary.Select(recordList =>
|
||||
recordList.Where(record =>
|
||||
(!keepProperties.Any() || keepProperties.Contains(record.Key))
|
||||
&& !removeProperties.Contains(record.Key)
|
||||
).Select(record => record.Value).ToList()
|
||||
).ToList();
|
||||
}
|
||||
|
||||
private static string TransformToOutputString(IEnumerable<List<string>> content, string delimiter, string wrapText)
|
||||
=> string.Join(Environment.NewLine, content.Select(x => string.Join(delimiter, x.Select(y => $"{wrapText}{y}{wrapText}"))));
|
||||
|
||||
private static void OutputResults(ExtractIndexFileDataRequest request,
|
||||
IEnumerable<List<string>> originalContent,
|
||||
IEnumerable<List<string>> newContent,
|
||||
List<string> headers,
|
||||
bool recordIsWrappedInQuotes)
|
||||
{
|
||||
var delimiter = request.DelimiterType.GetNameFromValue();
|
||||
var wrapText = recordIsWrappedInQuotes ? "\"" : string.Empty;
|
||||
var destinationFileExists = File.Exists(request.FinalDestinationIndexFilePath);
|
||||
|
||||
if (request.IndexHasHeaders)
|
||||
{
|
||||
originalContent = originalContent.Prepend(headers);
|
||||
|
||||
if (!request.AppendToDestinationIndexFile || !destinationFileExists)
|
||||
{
|
||||
newContent = !request.ExtractAllIndexes && request.IndexesToExtract.Count > 0
|
||||
? newContent.Prepend(headers.Where((_, i) => request.IndexesToExtract.Contains(i)).ToList())
|
||||
: newContent.Prepend(headers);
|
||||
}
|
||||
}
|
||||
|
||||
var originalFile = TransformToOutputString(originalContent, delimiter, wrapText);
|
||||
var newFile = TransformToOutputString(newContent, delimiter, wrapText);
|
||||
|
||||
if (request.AppendToDestinationIndexFile && destinationFileExists)
|
||||
{
|
||||
File.AppendAllText(request.FinalDestinationIndexFilePath, Environment.NewLine + newFile);
|
||||
}
|
||||
else
|
||||
{
|
||||
File.WriteAllText(request.FinalDestinationIndexFilePath, newFile);
|
||||
}
|
||||
|
||||
File.WriteAllText(request.FinalIndexFilePath, originalFile);
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
using LCU.Library.Internal.LCUServiceFactory.Configurations;
|
||||
using LCU.Library.Internal.LCUServiceFactory.Services;
|
||||
|
||||
namespace LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
||||
|
||||
public abstract class FileActionService(ServicesFactoryConfiguration servicesFactoryConfiguration)
|
||||
: LcuAbstractService(servicesFactoryConfiguration.GetServiceFactoryRegistrationByInterface<IFileActionService>())
|
||||
{
|
||||
}
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using LCU.Api.Internal.FileOperations.Validations;
|
||||
using LCU.Library.Internal.FileOperationsModels.Enums;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models.Requests;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models.Responses;
|
||||
using LCU.Library.Internal.FileOperationsModels.Shared;
|
||||
|
||||
namespace LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
||||
|
||||
public class FileSizeService : IFileSizeService
|
||||
{
|
||||
|
||||
public List<FileSizeResponse> GetFileSize(FileSizeRequest fileSizeRequest)
|
||||
{
|
||||
var fileSizeResponses = new List<FileSizeResponse>();
|
||||
try
|
||||
{
|
||||
var requests = DynamicNameBuilder.BuildListOfRequests(fileSizeRequest);
|
||||
foreach (var request in requests)
|
||||
{
|
||||
var response = DetermineFileSize(request);
|
||||
fileSizeResponses.Add(response);
|
||||
}
|
||||
|
||||
return fileSizeResponses;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception($"Failed to get file size. Exception: {exception.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public decimal ConvertFileSize(decimal fileSizeInBytes, FileSizeTypes returnFileSizeType)
|
||||
=> returnFileSizeType switch
|
||||
{
|
||||
FileSizeTypes.Byte => fileSizeInBytes,
|
||||
FileSizeTypes.Kilobyte => fileSizeInBytes/1024,
|
||||
FileSizeTypes.Megabyte => fileSizeInBytes/1024/1024,
|
||||
FileSizeTypes.Gigabyte => fileSizeInBytes/1024/1024/1024,
|
||||
FileSizeTypes.Terabyte => fileSizeInBytes/1024/1024/1024/1024,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(returnFileSizeType), $"Not expected FileSizeType value: {returnFileSizeType}"),
|
||||
};
|
||||
|
||||
public RequestResponse PreQueueValidation(FileSizeRequest fileSizeRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
try
|
||||
{
|
||||
var requestList = DynamicNameBuilder.BuildListOfRequests(fileSizeRequest);
|
||||
requestResponse = FileValidation.ValidateIfAnyRequest(requestList, fileSizeRequest.FileSourceRequest.SourcePath, requestResponse);
|
||||
foreach (var request in requestList)
|
||||
{
|
||||
requestResponse = IsRequestValid(request, requestResponse);
|
||||
}
|
||||
|
||||
return requestResponse;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
requestResponse.IsSuccessful = false;
|
||||
requestResponse.MessageStack.Add(exception.Message);
|
||||
return requestResponse;
|
||||
}
|
||||
}
|
||||
|
||||
private static RequestResponse IsRequestValid(FileSizeRequest request, RequestResponse requestResponse)
|
||||
{
|
||||
return FileValidation.ValidateFileExists(request.FileSourceRequest.Source, requestResponse);
|
||||
}
|
||||
|
||||
private FileSizeResponse DetermineFileSize(FileSizeRequest request)
|
||||
{
|
||||
var file = new FileInfo(request.FileSourceRequest.Source);
|
||||
var length = Convert.ToDecimal(file.Length);
|
||||
return new FileSizeResponse
|
||||
{
|
||||
FileName = file.Name,
|
||||
FileSize = ConvertFileSize(length, request.FileSizeType),
|
||||
FileSizeType = request.FileSizeType
|
||||
};
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
namespace LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
||||
|
||||
public interface IActionService<TRequest>
|
||||
{
|
||||
RequestResponse PerformRequest(TRequest request);
|
||||
RequestResponse PreQueueValidation(TRequest originalRequest);
|
||||
RequestResponse IsRequestValid(TRequest request, RequestResponse requestResponse);
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.LCUServiceFactory.Services;
|
||||
|
||||
namespace LCU.Api.Internal.FileOperations.Services.FileActionServices
|
||||
{
|
||||
public interface IFileActionService : ILcuService
|
||||
{
|
||||
RequestResponse ExecuteAction(QueueRequest queueRequest);
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
using System.Collections.Generic;
|
||||
using LCU.Library.Internal.FileOperationsModels.Enums;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models.Requests;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models.Responses;
|
||||
|
||||
namespace LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
||||
|
||||
public interface IFileSizeService
|
||||
{
|
||||
public List<FileSizeResponse> GetFileSize(FileSizeRequest request);
|
||||
public decimal ConvertFileSize(decimal fileSizeInBytes, FileSizeTypes returnFileSizeType);
|
||||
public RequestResponse PreQueueValidation(FileSizeRequest originalRequest);
|
||||
}
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.IO;
|
||||
using LCU.Api.Internal.FileOperations.Validations;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.FileOperationsModels.Shared;
|
||||
using LCU.Library.Internal.LCUServiceFactory.Configurations;
|
||||
|
||||
namespace LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
||||
|
||||
public class ModifyFileService : FileActionService, IFileActionService, IActionService<ModifyFileRequest>
|
||||
{
|
||||
public ModifyFileService(ServicesFactoryConfiguration servicesFactoryConfiguration)
|
||||
: base(servicesFactoryConfiguration)
|
||||
{
|
||||
}
|
||||
|
||||
public RequestResponse ExecuteAction(QueueRequest queueRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
try
|
||||
{
|
||||
var fileRequest = JsonConvert.DeserializeObject<ModifyFileRequest>(queueRequest.Request);
|
||||
var requests = DynamicNameBuilder.BuildListOfRequest(fileRequest);
|
||||
|
||||
requestResponse = FileValidation.ValidateFileRequestsWithinLimit(requests, fileRequest.MaxAmountOfFiles, requestResponse);
|
||||
|
||||
foreach (var request in requests)
|
||||
{
|
||||
var temporaryRequestResponse = PerformRequest(request);
|
||||
if (!temporaryRequestResponse.IsSuccessful)
|
||||
{
|
||||
requestResponse.IsSuccessful = fileRequest.SucceedOnError;
|
||||
if (fileRequest.SucceedOnError)
|
||||
{
|
||||
requestResponse.MessageStack.Add("Succeed On Error is set.");
|
||||
}
|
||||
|
||||
requestResponse.MessageStack.AddRange(temporaryRequestResponse.MessageStack);
|
||||
}
|
||||
}
|
||||
|
||||
requestResponse.ReturnResponse = fileRequest;
|
||||
return requestResponse;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return RequestResponseHelper.ExceptionUpdateRequestResponse(requestResponse,
|
||||
queueRequest.FileOperationType,
|
||||
exception.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PerformRequest(ModifyFileRequest request)
|
||||
{
|
||||
return OperationService.PerformOperation(request, IsRequestValid, ModifyFile);
|
||||
}
|
||||
|
||||
private void ModifyFile(ModifyFileRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Move(request.OriginalSource, request.FinalSource);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception($"Exception thrown while trying to ExecuteRequestAction. " +
|
||||
$"Request: {JsonConvert.SerializeObject(request)}" +
|
||||
$"ExceptionMessage: {exception.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PreQueueValidation(ModifyFileRequest originalRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
var requestList = DynamicNameBuilder.BuildListOfRequest(originalRequest);
|
||||
requestResponse = FileValidation.ValidateFileRequestsWithinLimit(requestList, originalRequest.MaxAmountOfFiles, requestResponse);
|
||||
foreach (var request in requestList)
|
||||
{
|
||||
requestResponse = IsRequestValid(request, requestResponse);
|
||||
}
|
||||
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
public RequestResponse IsRequestValid(ModifyFileRequest fileRequest, RequestResponse requestResponse)
|
||||
{
|
||||
if (fileRequest.SucceedOnError)
|
||||
{
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
requestResponse = FileValidation.ValidateDirectoryExists(fileRequest.SourcePath, requestResponse);
|
||||
requestResponse = FileValidation.ValidateFileExists(fileRequest.OriginalSource, requestResponse);
|
||||
requestResponse = FileValidation.ValidateFileNotExists(fileRequest.FinalSource, requestResponse);
|
||||
|
||||
return requestResponse;
|
||||
}
|
||||
}
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.FileOperationsModels.Shared;
|
||||
using LCU.Library.Internal.LCUServiceFactory.Configurations;
|
||||
using LCU.Api.Internal.FileOperations.Validations;
|
||||
|
||||
namespace LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
||||
|
||||
public class MoveFileService : FileActionService, IFileActionService, IActionService<MoveFileRequest>
|
||||
{
|
||||
public MoveFileService(ServicesFactoryConfiguration servicesFactoryConfiguration)
|
||||
: base(servicesFactoryConfiguration)
|
||||
{
|
||||
}
|
||||
|
||||
public RequestResponse ExecuteAction(QueueRequest queueRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
try
|
||||
{
|
||||
var fileRequest = JsonConvert.DeserializeObject<MoveFileRequest>(queueRequest.Request);
|
||||
var requests = DynamicNameBuilder.BuildListOfRequest(fileRequest);
|
||||
requestResponse = FileValidation.ValidateIfAnyRequest(requests, fileRequest.Source, fileRequest.SucceedOnError, requestResponse);
|
||||
|
||||
foreach (var request in requests)
|
||||
{
|
||||
var temporaryRequestResponse = PerformRequest(request);
|
||||
if (!temporaryRequestResponse.IsSuccessful)
|
||||
{
|
||||
requestResponse.IsSuccessful = fileRequest.SucceedOnError;
|
||||
if (fileRequest.SucceedOnError)
|
||||
{
|
||||
requestResponse.MessageStack.Add("Succeed On Error is set.");
|
||||
}
|
||||
|
||||
requestResponse.MessageStack.AddRange(temporaryRequestResponse.MessageStack);
|
||||
}
|
||||
}
|
||||
|
||||
requestResponse.ReturnResponse = fileRequest;
|
||||
return requestResponse;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return RequestResponseHelper.ExceptionUpdateRequestResponse(requestResponse,
|
||||
queueRequest.FileOperationType,
|
||||
exception.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PerformRequest(MoveFileRequest request)
|
||||
{
|
||||
return OperationService.PerformOperation(request, IsRequestValid, MoveFile);
|
||||
}
|
||||
|
||||
private void MoveFile(MoveFileRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Move(request.Source, request.Destination, request.Overwrite);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception($"Exception thrown while trying to ExecuteRequestAction. " +
|
||||
$"Request: {JsonConvert.SerializeObject(request)}" +
|
||||
$"ExceptionMessage: {exception.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PreQueueValidation(MoveFileRequest originalRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
var requestList = DynamicNameBuilder.BuildListOfRequest(originalRequest);
|
||||
requestResponse = FileValidation.ValidateIfAnyRequest(requestList, originalRequest.SourcePath, originalRequest.SucceedOnError, requestResponse);
|
||||
foreach (var request in requestList)
|
||||
{
|
||||
requestResponse = IsRequestValid(request, requestResponse);
|
||||
}
|
||||
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
public RequestResponse IsRequestValid(MoveFileRequest fileRequest, RequestResponse requestResponse)
|
||||
{
|
||||
if (fileRequest.SucceedOnError)
|
||||
{
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
requestResponse = FileValidation.ValidateSourceNotEmpty(fileRequest.Source, requestResponse);
|
||||
requestResponse = FileValidation.ValidateFileExists(fileRequest.Source, requestResponse);
|
||||
|
||||
requestResponse = FileValidation.ValidateDestinationNotEmpty(fileRequest.Destination, requestResponse);
|
||||
requestResponse = FileValidation.ValidateDirectoryExists(fileRequest.DestinationPath, requestResponse);
|
||||
|
||||
if (!fileRequest.Overwrite)
|
||||
{
|
||||
requestResponse = FileValidation.ValidateFileNotExists(fileRequest.Destination, requestResponse);
|
||||
}
|
||||
|
||||
return requestResponse;
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
namespace LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
||||
|
||||
public static class OperationService
|
||||
{
|
||||
public static RequestResponse PerformOperation<T>(T request,
|
||||
Func<T, RequestResponse, RequestResponse> validate,
|
||||
Action<T> action)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
try
|
||||
{
|
||||
|
||||
requestResponse = validate(request, requestResponse);
|
||||
if (requestResponse.IsSuccessful)
|
||||
{
|
||||
action(request);
|
||||
}
|
||||
|
||||
return requestResponse;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
requestResponse.IsSuccessful = false;
|
||||
requestResponse.MessageStack.Add(exception.Message);
|
||||
return requestResponse;
|
||||
}
|
||||
}
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.LCUServiceFactory.Configurations;
|
||||
using LCU.Api.Internal.FileOperations.Validations;
|
||||
namespace LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
||||
|
||||
public class RenameDirectoryService : FileActionService, IFileActionService, IActionService<RenameDirectoryRequest>
|
||||
{
|
||||
public RenameDirectoryService(ServicesFactoryConfiguration servicesFactoryConfiguration)
|
||||
: base(servicesFactoryConfiguration)
|
||||
{
|
||||
}
|
||||
|
||||
public RequestResponse ExecuteAction(QueueRequest queueRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
try
|
||||
{
|
||||
var directoryRequest = JsonConvert.DeserializeObject<RenameDirectoryRequest>(queueRequest.Request);
|
||||
requestResponse = PerformRequest(directoryRequest);
|
||||
requestResponse.ReturnResponse = directoryRequest;
|
||||
return requestResponse;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return RequestResponseHelper.ExceptionUpdateRequestResponse(requestResponse,
|
||||
queueRequest.FileOperationType,
|
||||
exception.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PerformRequest(RenameDirectoryRequest request)
|
||||
{
|
||||
return OperationService.PerformOperation(request, IsRequestValid, RenameDirectory);
|
||||
}
|
||||
|
||||
private void RenameDirectory(RenameDirectoryRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
Directory.Move(request.FinalSourceDirectoryPath, request.NewDirectoryDestination);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception($"Exception thrown while trying to ExecuteRequestAction. " +
|
||||
$"Request: {JsonConvert.SerializeObject(request)}" +
|
||||
$"ExceptionMessage: {exception.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PreQueueValidation(RenameDirectoryRequest originalRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
requestResponse = IsRequestValid(originalRequest, requestResponse);
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
public RequestResponse IsRequestValid(RenameDirectoryRequest request, RequestResponse requestResponse)
|
||||
{
|
||||
requestResponse = FileValidation.ValidateDirectoryExists(request.SourceDirectoryPath, requestResponse);
|
||||
requestResponse = FileValidation.ValidateDirectoryExists(request.FinalSourceDirectoryPath, requestResponse);
|
||||
requestResponse = FileValidation.ValidateDirectoryNotExists(request.NewDirectoryDestination, requestResponse);
|
||||
return requestResponse;
|
||||
}
|
||||
}
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.LCUServiceFactory.Configurations;
|
||||
using LCU.Api.Internal.FileOperations.Validations;
|
||||
namespace LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
||||
|
||||
public class RenameFileService : FileActionService, IFileActionService, IActionService<RenameFileRequest>
|
||||
{
|
||||
public RenameFileService(ServicesFactoryConfiguration servicesFactoryConfiguration)
|
||||
: base(servicesFactoryConfiguration)
|
||||
{
|
||||
}
|
||||
|
||||
public RequestResponse ExecuteAction(QueueRequest queueRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
try
|
||||
{
|
||||
var fileRequest = JsonConvert.DeserializeObject<RenameFileRequest>(queueRequest.Request);
|
||||
requestResponse = PerformRequest(fileRequest);
|
||||
if (!requestResponse.IsSuccessful)
|
||||
{
|
||||
requestResponse.IsSuccessful = fileRequest.SucceedOnError;
|
||||
if (fileRequest.SucceedOnError)
|
||||
{
|
||||
requestResponse.MessageStack.Add("Succeed On Error is set.");
|
||||
}
|
||||
}
|
||||
|
||||
requestResponse.ReturnResponse = fileRequest;
|
||||
return requestResponse;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return RequestResponseHelper.ExceptionUpdateRequestResponse(requestResponse,
|
||||
queueRequest.FileOperationType,
|
||||
exception.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PerformRequest(RenameFileRequest request)
|
||||
{
|
||||
return OperationService.PerformOperation(request, IsRequestValid, RenameFile);
|
||||
}
|
||||
|
||||
private void RenameFile(RenameFileRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Move(request.Source, request.NewFilenamePath);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception($"Exception thrown while trying to ExecuteRequestAction. " +
|
||||
$"Request: {JsonConvert.SerializeObject(request)}" +
|
||||
$"ExceptionMessage: {exception.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PreQueueValidation(RenameFileRequest originalRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
requestResponse = IsRequestValid(originalRequest, requestResponse);
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
public RequestResponse IsRequestValid(RenameFileRequest fileRequest, RequestResponse requestResponse)
|
||||
{
|
||||
if (fileRequest.SucceedOnError)
|
||||
{
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
requestResponse = FileValidation.ValidateSourceNotEmpty(fileRequest.Source, requestResponse);
|
||||
requestResponse = FileValidation.ValidateFileExists(fileRequest.Source, requestResponse);
|
||||
|
||||
requestResponse = FileValidation.ValidateFilenameNotEmpty(fileRequest.FinalNewFileName, requestResponse);
|
||||
requestResponse = FileValidation.ValidateFilename(fileRequest.FinalNewFileName, requestResponse);
|
||||
requestResponse = FileValidation.ValidateFileNotExists(fileRequest.NewFilenamePath, requestResponse);
|
||||
|
||||
return requestResponse;
|
||||
}
|
||||
}
|
||||
+152
@@ -0,0 +1,152 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO.Compression;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.FileOperationsModels.Shared;
|
||||
using LCU.Api.Internal.FileOperations.Validations;
|
||||
using LCU.Library.Internal.LCUServiceFactory.Configurations;
|
||||
namespace LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
||||
|
||||
public class UnzipFileService : FileActionService, IFileActionService, IActionService<UnzipFileRequest>
|
||||
{
|
||||
public UnzipFileService(ServicesFactoryConfiguration servicesFactoryConfiguration)
|
||||
: base(servicesFactoryConfiguration)
|
||||
{
|
||||
}
|
||||
|
||||
public RequestResponse ExecuteAction(QueueRequest queueRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
try
|
||||
{
|
||||
var unzipRequest = JsonConvert.DeserializeObject<UnzipFileRequest>(queueRequest.Request);
|
||||
var requests = BuildFileList(unzipRequest);
|
||||
requestResponse = FileValidation.ValidateIfAnyRequest(requests, unzipRequest.FormattedSourceDirectoryPath, requestResponse);
|
||||
|
||||
foreach (var request in requests)
|
||||
{
|
||||
var temporaryRequestResponse = PerformRequest(request);
|
||||
if (!temporaryRequestResponse.IsSuccessful)
|
||||
{
|
||||
requestResponse.IsSuccessful = temporaryRequestResponse.IsSuccessful;
|
||||
requestResponse.MessageStack.AddRange(temporaryRequestResponse.MessageStack);
|
||||
}
|
||||
}
|
||||
|
||||
requestResponse.ReturnResponse = unzipRequest;
|
||||
return requestResponse;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return RequestResponseHelper.ExceptionUpdateRequestResponse(requestResponse,
|
||||
queueRequest.FileOperationType,
|
||||
exception.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PerformRequest(UnzipFileRequest request)
|
||||
{
|
||||
return OperationService.PerformOperation(request, IsRequestValid, UnzipFile);
|
||||
}
|
||||
|
||||
private void UnzipFile(UnzipFileRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
var destination = request.FormattedDestinationPath;
|
||||
if (request.UseZipFileNameAsFinalDestination)
|
||||
{
|
||||
destination = Path.GetDirectoryName(request.DestinationPathZipFileNameWithoutZip);
|
||||
if (destination.Length > 0)
|
||||
{
|
||||
Directory.CreateDirectory(destination);
|
||||
}
|
||||
}
|
||||
|
||||
ZipFile.ExtractToDirectory(request.SourceFilePath, destination);
|
||||
|
||||
if (request.IgnoreFileNameSubFolderAsDestination)
|
||||
{
|
||||
MoveFromChildFolder(destination, request);
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception($"Exception thrown while trying to ExecuteRequestAction. " +
|
||||
$"Request: {JsonConvert.SerializeObject(request)}" +
|
||||
$"ExceptionMessage: {exception.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private void MoveFromChildFolder(string destination, UnzipFileRequest request)
|
||||
{
|
||||
var recursionDepth = request.MaxFileDepth;
|
||||
var currentDepth = 0;
|
||||
var currentFilePath = destination;
|
||||
|
||||
RecursiveFolderSearch(destination, recursionDepth, currentFilePath,currentDepth);
|
||||
}
|
||||
|
||||
public void RecursiveFolderSearch(string destination, int recursionDepth, string currentFilePath, int currentDepth)
|
||||
{
|
||||
if (currentDepth>recursionDepth)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var folders = Directory.EnumerateDirectories(currentFilePath);
|
||||
foreach (var folder in folders)
|
||||
{
|
||||
RecursiveFolderSearch(destination, recursionDepth, folder, currentDepth + 1);
|
||||
var childFiles = Directory.EnumerateFiles(folder);
|
||||
|
||||
foreach (var file in childFiles)
|
||||
{
|
||||
var fileName = file[(file.LastIndexOf("\\") + 1)..];
|
||||
File.Move(file, destination + "\\" + fileName);
|
||||
}
|
||||
Directory.Delete(folder);
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PreQueueValidation(UnzipFileRequest originalRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
var requestList = BuildFileList(originalRequest);
|
||||
requestResponse = FileValidation.ValidateIfAnyRequest(requestList, originalRequest.FormattedSourceDirectoryPath, requestResponse);
|
||||
foreach (var request in requestList)
|
||||
{
|
||||
requestResponse = IsRequestValid(request, requestResponse);
|
||||
}
|
||||
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
public List<UnzipFileRequest> BuildFileList(UnzipFileRequest originalRequest)
|
||||
{
|
||||
var nameList = RegexNameHandler.BuildFileList(originalRequest.FormattedSourceDirectoryPath,
|
||||
originalRequest.FinalZipFileName,
|
||||
originalRequest.FileNameSetting);
|
||||
var requests = new List<UnzipFileRequest>();
|
||||
|
||||
foreach (var name in nameList)
|
||||
{
|
||||
var newRequest = Clone.DeepClone(originalRequest);
|
||||
newRequest.ZipFileName = name;
|
||||
|
||||
requests.Add(newRequest);
|
||||
}
|
||||
|
||||
return requests;
|
||||
}
|
||||
|
||||
public RequestResponse IsRequestValid(UnzipFileRequest fileRequest, RequestResponse requestResponse)
|
||||
{
|
||||
requestResponse = DirectoryValidation.ValidateDirectoryExists(fileRequest.FormattedSourceDirectoryPath, requestResponse);
|
||||
requestResponse = FileValidation.ValidateFileExists(fileRequest.SourceFilePath, requestResponse);
|
||||
requestResponse = DirectoryValidation.ValidateDirectoryExists(fileRequest.FormattedDestinationPath, requestResponse);
|
||||
return requestResponse;
|
||||
}
|
||||
}
|
||||
+165
@@ -0,0 +1,165 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json;
|
||||
using ICSharpCode.SharpZipLib.Zip;
|
||||
using ICSharpCode.SharpZipLib.Core;
|
||||
using System.IO;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.FileOperationsModels.Shared;
|
||||
using LCU.Api.Internal.FileOperations.Validations;
|
||||
using LCU.Library.Internal.LCUServiceFactory.Configurations;
|
||||
|
||||
namespace LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
||||
|
||||
public class UnzipPasswordProtectedFileService : FileActionService, IFileActionService, IActionService<UnzipPasswordProtectedFileRequest>
|
||||
{
|
||||
public UnzipPasswordProtectedFileService(ServicesFactoryConfiguration servicesFactoryConfiguration)
|
||||
: base(servicesFactoryConfiguration)
|
||||
{
|
||||
}
|
||||
|
||||
public RequestResponse ExecuteAction(QueueRequest queueRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
try
|
||||
{
|
||||
var unzipPasswordProtectedFileRequest = JsonConvert.DeserializeObject<UnzipPasswordProtectedFileRequest>(queueRequest.Request);
|
||||
var requests = BuildFileList(unzipPasswordProtectedFileRequest);
|
||||
requestResponse = FileValidation.ValidateIfAnyRequest(requests, unzipPasswordProtectedFileRequest.FormattedSourceDirectoryPath, requestResponse);
|
||||
|
||||
foreach (var request in requests)
|
||||
{
|
||||
var temporaryRequestResponse = PerformRequest(request);
|
||||
if (!temporaryRequestResponse.IsSuccessful)
|
||||
{
|
||||
requestResponse.IsSuccessful = temporaryRequestResponse.IsSuccessful;
|
||||
requestResponse.MessageStack.AddRange(temporaryRequestResponse.MessageStack);
|
||||
}
|
||||
}
|
||||
|
||||
requestResponse.ReturnResponse = unzipPasswordProtectedFileRequest;
|
||||
return requestResponse;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return RequestResponseHelper.ExceptionUpdateRequestResponse(requestResponse,
|
||||
queueRequest.FileOperationType,
|
||||
exception.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PerformRequest(UnzipPasswordProtectedFileRequest request)
|
||||
{
|
||||
return OperationService.PerformOperation(request, IsRequestValid, UnzipPasswordProtectedFile);
|
||||
}
|
||||
|
||||
public void UnzipPasswordProtectedFile(UnzipPasswordProtectedFileRequest request)
|
||||
{
|
||||
ZipFile passwordProtectedZipFile = null;
|
||||
try
|
||||
{
|
||||
var fileStream = File.OpenRead(request.SourceFilePath);
|
||||
passwordProtectedZipFile = new ZipFile(fileStream)
|
||||
{
|
||||
Password = request.Password
|
||||
};
|
||||
|
||||
foreach (ZipEntry zipEntry in passwordProtectedZipFile)
|
||||
{
|
||||
if (!zipEntry.IsFile)
|
||||
{
|
||||
// Ignore directories
|
||||
continue;
|
||||
}
|
||||
|
||||
var entryFileName = zipEntry.Name;
|
||||
// to remove the folder from the entry:- entryFileName = Path.GetFileName(entryFileName);
|
||||
// Optionally match entrynames against a selection list here to skip as desired.
|
||||
// The unpacked length is available in the zipEntry.Size property.
|
||||
|
||||
// 4K is optimum
|
||||
var buffer = new byte[4096];
|
||||
var zipStream = passwordProtectedZipFile.GetInputStream(zipEntry);
|
||||
|
||||
// Manipulate the output filename here as desired.
|
||||
var finalDestinationPath = request.FormattedDestinationPath;
|
||||
if (request.UseZipFileNameAsFinalDestination)
|
||||
{
|
||||
var zipFilename = request.PasswordProtectedZipFileNameWithDate.ToUpper().EndsWith(".ZIP") ?
|
||||
request.PasswordProtectedZipFileNameWithDate.ToUpper().Replace(".ZIP", "") :
|
||||
request.PasswordProtectedZipFileNameWithDate;
|
||||
finalDestinationPath = $"{finalDestinationPath}{zipFilename}\\";
|
||||
}
|
||||
|
||||
var fullZipToPath = Path.Combine(finalDestinationPath, entryFileName);
|
||||
var directoryName = Path.GetDirectoryName(fullZipToPath);
|
||||
if (directoryName.Length > 0)
|
||||
{
|
||||
Directory.CreateDirectory(directoryName);
|
||||
}
|
||||
|
||||
// Unzip file in buffered chunks. This is just as fast as unpacking to a buffer the full size
|
||||
// of the file, but does not waste memory.
|
||||
// The "using" will close the stream even if an exception occurs.
|
||||
using (FileStream streamWriter = File.Create(fullZipToPath))
|
||||
{
|
||||
StreamUtils.Copy(zipStream, streamWriter, buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception($"Exception thrown while trying to ExecuteRequestAction. " +
|
||||
$"Request: {JsonConvert.SerializeObject(request)}" +
|
||||
$"ExceptionMessage: {exception.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (passwordProtectedZipFile != null)
|
||||
{
|
||||
passwordProtectedZipFile.IsStreamOwner = true; // Makes close also shut the underlying stream
|
||||
passwordProtectedZipFile.Close(); // Ensure we release resources
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public RequestResponse PreQueueValidation(UnzipPasswordProtectedFileRequest originalRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
var requestList = BuildFileList(originalRequest);
|
||||
requestResponse = FileValidation.ValidateIfAnyRequest(requestList, originalRequest.FormattedSourceDirectoryPath, requestResponse);
|
||||
foreach (var request in requestList)
|
||||
{
|
||||
requestResponse = IsRequestValid(request, requestResponse);
|
||||
}
|
||||
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
public List<UnzipPasswordProtectedFileRequest> BuildFileList(UnzipPasswordProtectedFileRequest originalRequest)
|
||||
{
|
||||
var nameList = RegexNameHandler.BuildFileList(originalRequest.FormattedSourceDirectoryPath,
|
||||
originalRequest.FinalPasswordProtectedZipFileName,
|
||||
originalRequest.FileNameSetting);
|
||||
var requests = new List<UnzipPasswordProtectedFileRequest>();
|
||||
|
||||
foreach (var name in nameList)
|
||||
{
|
||||
var newRequest = Clone.DeepClone(originalRequest);
|
||||
newRequest.PasswordProtectedZipFileName = name;
|
||||
|
||||
requests.Add(newRequest);
|
||||
}
|
||||
|
||||
return requests;
|
||||
}
|
||||
|
||||
public RequestResponse IsRequestValid(UnzipPasswordProtectedFileRequest fileRequest, RequestResponse requestResponse)
|
||||
{
|
||||
requestResponse = DirectoryValidation.ValidateDirectoryExists(fileRequest.FormattedSourceDirectoryPath, requestResponse);
|
||||
requestResponse = FileValidation.ValidateFileExists(fileRequest.SourceFilePath, requestResponse);
|
||||
requestResponse = DirectoryValidation.ValidateDirectoryExists(fileRequest.FormattedDestinationPath, requestResponse);
|
||||
return requestResponse;
|
||||
}
|
||||
}
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json;
|
||||
using LCU.Library.Internal.LCUServiceFactory.Configurations;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.FileOperationsModels.Shared;
|
||||
using LCU.Library.Internal.FileOperationsModels.Enums;
|
||||
using LCU.Api.Internal.FileOperations.Validations;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models.Requests;
|
||||
using Microsoft.OpenApi.Extensions;
|
||||
namespace LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
||||
|
||||
public class ValidateFileService : FileActionService, IFileActionService, IActionService<ValidateFileRequest>
|
||||
{
|
||||
public ValidateFileService(ServicesFactoryConfiguration servicesFactoryConfiguration)
|
||||
: base(servicesFactoryConfiguration)
|
||||
{
|
||||
}
|
||||
|
||||
public RequestResponse ExecuteAction(QueueRequest queueRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
try
|
||||
{
|
||||
var fileRequest = JsonConvert.DeserializeObject<ValidateFileRequest>(queueRequest.Request);
|
||||
var requests = DynamicNameBuilder.BuildListOfRequests(fileRequest);
|
||||
|
||||
if (fileRequest.FileDoesExist)
|
||||
{
|
||||
requestResponse = FileValidation.ValidateIfAnyRequest(requests, fileRequest.FileSourceRequest.Source, requestResponse);
|
||||
}
|
||||
|
||||
if (fileRequest.CheckFileCount)
|
||||
{
|
||||
if (!ValidateFileCount(fileRequest, requests))
|
||||
{
|
||||
requestResponse.MessageStack.Add($"The file count condition returned false: {requests.Count} {fileRequest.OperatorType.GetDisplayName()} {fileRequest.FileCount} ");
|
||||
requestResponse.IsSuccessful = false;
|
||||
}
|
||||
}
|
||||
|
||||
requestResponse = PerformRequests(requests, requestResponse);
|
||||
|
||||
requestResponse.ReturnResponse = fileRequest;
|
||||
return requestResponse;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return RequestResponseHelper.ExceptionUpdateRequestResponse(requestResponse,
|
||||
queueRequest.FileOperationType,
|
||||
exception.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public bool ValidateFileCount(ValidateFileRequest fileRequest, List<ValidateFileRequest> requests)
|
||||
=> fileRequest.OperatorType switch
|
||||
{
|
||||
OperatorTypes.Equals => (requests.Count == fileRequest.FileCount),
|
||||
OperatorTypes.GreaterThan => (requests.Count > fileRequest.FileCount),
|
||||
OperatorTypes.GreaterThanEquals => (requests.Count >= fileRequest.FileCount ),
|
||||
OperatorTypes.LessThan => (requests.Count < fileRequest.FileCount),
|
||||
OperatorTypes.LessThanEquals => (requests.Count <= fileRequest.FileCount ),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(fileRequest.OperatorType), $"Not expected OperatorType value: {fileRequest.OperatorType}"),
|
||||
};
|
||||
|
||||
|
||||
public RequestResponse PerformRequests(List<ValidateFileRequest> requests, RequestResponse requestResponse)
|
||||
{
|
||||
foreach (var request in requests)
|
||||
{
|
||||
var temporaryRequestResponse = PerformRequest(request);
|
||||
if (!temporaryRequestResponse.IsSuccessful)
|
||||
{
|
||||
requestResponse.MessageStack.AddRange(temporaryRequestResponse.MessageStack);
|
||||
requestResponse.IsSuccessful = false;
|
||||
}
|
||||
}
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
public RequestResponse PerformRequest(ValidateFileRequest request)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
if (request.FileDoesExist)
|
||||
{
|
||||
return FileValidation.ValidateFileExists(request.FileSourceRequest.Source, requestResponse);
|
||||
}
|
||||
return FileValidation.ValidateFileNotExists(request.FileSourceRequest.Source, requestResponse);
|
||||
}
|
||||
|
||||
public RequestResponse PreQueueValidation(ValidateFileRequest originalRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
requestResponse = IsRequestValid(originalRequest, requestResponse);
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
public RequestResponse IsRequestValid(ValidateFileRequest request, RequestResponse requestResponse)
|
||||
{
|
||||
requestResponse = FileValidation.ValidateSourceNotEmpty(request.FileSourceRequest.SourceFile, requestResponse);
|
||||
|
||||
requestResponse = FileValidation.ValidateFilenameNotEmpty(request.FileSourceRequest.SourceFile, requestResponse);
|
||||
requestResponse = FileValidation.ValidateFilename(request.FileSourceRequest.SourceFile, requestResponse);
|
||||
|
||||
return requestResponse;
|
||||
}
|
||||
}
|
||||
|
||||
+111
@@ -0,0 +1,111 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using LCU.Api.Internal.FileOperations.Validations;
|
||||
using LCU.Library.Internal.FileOperationsModels.Enums;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models.Requests;
|
||||
using LCU.Library.Internal.FileOperationsModels.Shared;
|
||||
using LCU.Library.Internal.LCUServiceFactory.Configurations;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
||||
|
||||
public class ValidateFileSizeInRangeService : FileActionService, IFileActionService, IActionService<ValidateFileSizeInRangeRequest>
|
||||
{
|
||||
private readonly IFileSizeService _fileSizeService;
|
||||
|
||||
public ValidateFileSizeInRangeService(IFileSizeService fileSizeService,
|
||||
ServicesFactoryConfiguration servicesFactoryConfiguration)
|
||||
: base(servicesFactoryConfiguration)
|
||||
{
|
||||
_fileSizeService = fileSizeService;
|
||||
}
|
||||
|
||||
public RequestResponse ExecuteAction(QueueRequest queueRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
try
|
||||
{
|
||||
var fileRequest = JsonConvert.DeserializeObject<ValidateFileSizeInRangeRequest>(queueRequest.Request);
|
||||
var requests = DynamicNameBuilder.BuildListOfRequests(fileRequest);
|
||||
|
||||
requestResponse = FileValidation.ValidateIfAnyRequest(requests, fileRequest.FileSourceRequest.Source, requestResponse);
|
||||
|
||||
foreach (var request in requests)
|
||||
{
|
||||
var temporaryRequestResponse = PerformRequest(request);
|
||||
if (temporaryRequestResponse.IsSuccessful)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
requestResponse.MessageStack.AddRange(temporaryRequestResponse.MessageStack);
|
||||
requestResponse.IsSuccessful = false;
|
||||
}
|
||||
|
||||
requestResponse.ReturnResponse = fileRequest;
|
||||
return requestResponse;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return RequestResponseHelper.ExceptionUpdateRequestResponse(requestResponse,
|
||||
queueRequest.FileOperationType,
|
||||
exception.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PerformRequest(ValidateFileSizeInRangeRequest request)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
return ValidateFileSizeInRange(request.FileSourceRequest.Source,
|
||||
request.StartFileSizeRange,
|
||||
request.EndFileSizeRange,
|
||||
request.FileSizeType,
|
||||
requestResponse);
|
||||
}
|
||||
|
||||
public RequestResponse PreQueueValidation(ValidateFileSizeInRangeRequest originalRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
try
|
||||
{
|
||||
var requestList = DynamicNameBuilder.BuildListOfRequests(originalRequest);
|
||||
requestResponse = FileValidation.ValidateIfAnyRequest(requestList, originalRequest.FileSourceRequest.SourcePath, requestResponse);
|
||||
foreach (var request in requestList)
|
||||
{
|
||||
requestResponse = IsRequestValid(request, requestResponse);
|
||||
}
|
||||
|
||||
return requestResponse;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
requestResponse.IsSuccessful = false;
|
||||
requestResponse.MessageStack.Add(exception.Message);
|
||||
return requestResponse;
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse IsRequestValid(ValidateFileSizeInRangeRequest request, RequestResponse requestResponse)
|
||||
{
|
||||
requestResponse = FileValidation.ValidateFileExists(request.FileSourceRequest.Source, requestResponse);
|
||||
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
private RequestResponse ValidateFileSizeInRange(string path, decimal min, decimal max,
|
||||
FileSizeTypes fileSizeType,
|
||||
RequestResponse requestResponse)
|
||||
{
|
||||
decimal fileLengthInBytes = new FileInfo(path).Length;
|
||||
var fileLength = _fileSizeService.ConvertFileSize(fileLengthInBytes, fileSizeType);
|
||||
|
||||
if (fileLength < min || fileLength > max)
|
||||
{
|
||||
requestResponse.IsSuccessful = false;
|
||||
requestResponse.MessageStack.Add($"File size is not in range at path {path}");
|
||||
}
|
||||
|
||||
return requestResponse;
|
||||
}
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
using LCU.Api.Internal.FileOperations.Validations;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.LCUServiceFactory.Configurations;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace LCU.Api.Internal.FileOperations.Services.FileActionServices
|
||||
{
|
||||
public class WriteFileService : FileActionService, IFileActionService, IActionService<WriteFileRequest>
|
||||
{
|
||||
public WriteFileService(ServicesFactoryConfiguration servicesFactoryConfiguration)
|
||||
: base(servicesFactoryConfiguration)
|
||||
{
|
||||
}
|
||||
|
||||
public RequestResponse ExecuteAction(QueueRequest queueRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
|
||||
try
|
||||
{
|
||||
var fileRequest = JsonConvert.DeserializeObject<WriteFileRequest>(queueRequest.Request);
|
||||
|
||||
requestResponse = PerformRequest(fileRequest);
|
||||
|
||||
requestResponse.ReturnResponse = fileRequest;
|
||||
return requestResponse;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return RequestResponseHelper.ExceptionUpdateRequestResponse(requestResponse,
|
||||
queueRequest.FileOperationType,
|
||||
exception.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PerformRequest(WriteFileRequest request)
|
||||
{
|
||||
return OperationService.PerformOperation(request, IsRequestValid, WriteFile);
|
||||
}
|
||||
|
||||
private void WriteFile(WriteFileRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
File.WriteAllText(request.FullFilePath, request.FileData);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception($"Exception thrown while trying to ExecuteRequestAction. " +
|
||||
$"Request: {JsonConvert.SerializeObject(request)}" +
|
||||
$"ExceptionMessage: {exception.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PreQueueValidation(WriteFileRequest originalRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
|
||||
requestResponse = IsRequestValid(originalRequest, requestResponse);
|
||||
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
public RequestResponse IsRequestValid(WriteFileRequest originalRequest, RequestResponse requestResponse)
|
||||
{
|
||||
requestResponse = FileValidation.ValidateDirectoryExists(originalRequest.DestinationPath, requestResponse);
|
||||
|
||||
if (!originalRequest.Overwrite)
|
||||
{
|
||||
requestResponse = FileValidation.ValidateFileNotExists(originalRequest.FullFilePath, requestResponse);
|
||||
}
|
||||
|
||||
return requestResponse;
|
||||
}
|
||||
}
|
||||
}
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO.Compression;
|
||||
using Newtonsoft.Json;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.FileOperationsModels.Shared;
|
||||
using LCU.Api.Internal.FileOperations.Validations;
|
||||
using LCU.Library.Internal.LCUServiceFactory.Configurations;
|
||||
namespace LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
||||
|
||||
public class ZipDirectoryService : FileActionService, IFileActionService, IActionService<ZipDirectoryRequest>
|
||||
{
|
||||
public ZipDirectoryService(ServicesFactoryConfiguration servicesFactoryConfiguration)
|
||||
: base(servicesFactoryConfiguration)
|
||||
{
|
||||
}
|
||||
|
||||
public RequestResponse ExecuteAction(QueueRequest queueRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
try
|
||||
{
|
||||
var zipRequest = JsonConvert.DeserializeObject<ZipDirectoryRequest>(queueRequest.Request);
|
||||
var requests = BuildDirectoryList(zipRequest);
|
||||
requestResponse = FileValidation.ValidateIfAnyRequest(requests, zipRequest.SourceDirectory, requestResponse);
|
||||
var result = requestResponse.IsSuccessful;
|
||||
|
||||
foreach (var request in requests)
|
||||
{
|
||||
var temporaryRequestResponse = PerformRequest(request);
|
||||
if (!temporaryRequestResponse.IsSuccessful)
|
||||
{
|
||||
requestResponse.IsSuccessful = temporaryRequestResponse.IsSuccessful;
|
||||
requestResponse.MessageStack.AddRange(temporaryRequestResponse.MessageStack);
|
||||
}
|
||||
}
|
||||
|
||||
requestResponse.ReturnResponse = zipRequest;
|
||||
return requestResponse;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return RequestResponseHelper.ExceptionUpdateRequestResponse(requestResponse,
|
||||
queueRequest.FileOperationType,
|
||||
exception.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PerformRequest(ZipDirectoryRequest request)
|
||||
{
|
||||
return OperationService.PerformOperation(request, IsRequestValid, ZipDirectory);
|
||||
}
|
||||
|
||||
private void ZipDirectory(ZipDirectoryRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
ZipFile.CreateFromDirectory(request.SourceDirectory, request.ZipDestination);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception($"Exception thrown while trying to ExecuteRequestAction. " +
|
||||
$"Request: {JsonConvert.SerializeObject(request)}" +
|
||||
$"ExceptionMessage: {exception.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PreQueueValidation(ZipDirectoryRequest originalRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
var requestList = BuildDirectoryList(originalRequest);
|
||||
requestResponse = FileValidation.ValidateIfAnyRequest(requestList, originalRequest.FormattedSourceDirectory, requestResponse);
|
||||
foreach (var request in requestList)
|
||||
{
|
||||
requestResponse = IsRequestValid(request, requestResponse);
|
||||
}
|
||||
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
public List<ZipDirectoryRequest> BuildDirectoryList(ZipDirectoryRequest originalRequest)
|
||||
{
|
||||
var nameList = RegexNameHandler.BuildDirectoryList(originalRequest.SourceDirectory,
|
||||
originalRequest.DynamicDirectoryName,
|
||||
originalRequest.FileNameSetting);
|
||||
|
||||
var requests = new List<ZipDirectoryRequest>();
|
||||
|
||||
foreach (var name in nameList)
|
||||
{
|
||||
var newRequest = Clone.DeepClone(originalRequest);
|
||||
newRequest.DirectoryName = name;
|
||||
newRequest.SourceDirectory = String.IsNullOrWhiteSpace(newRequest.DirectoryName) ?
|
||||
originalRequest.SourceDirectory :
|
||||
$"{originalRequest.FormattedSourceDirectory}{newRequest.DirectoryName}";
|
||||
|
||||
requests.Add(newRequest);
|
||||
}
|
||||
|
||||
return requests;
|
||||
}
|
||||
|
||||
public RequestResponse IsRequestValid(ZipDirectoryRequest fileRequest, RequestResponse requestResponse)
|
||||
{
|
||||
requestResponse = DirectoryValidation.ValidateDirectoryExists(fileRequest.SourceDirectory, requestResponse);
|
||||
requestResponse = DirectoryValidation.ValidateDirectoryExists(fileRequest.ZipPath, requestResponse);
|
||||
requestResponse = FileValidation.ValidateFileNotExists(fileRequest.ZipDestination, requestResponse);
|
||||
return requestResponse;
|
||||
}
|
||||
}
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using Serilog;
|
||||
using LCU.Library.Internal.ApiResponse.Services;
|
||||
using LCU.Library.Internal.Exceptions;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.LCUServiceFactory.Services;
|
||||
using LCU.Api.Internal.FileOperations.Services.QueueServices;
|
||||
using LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
||||
using LCU.Api.Internal.FileOperations.Configurations;
|
||||
namespace LCU.Api.Internal.FileOperations.Services;
|
||||
|
||||
public class FileOperationsService : IFileOperationsService
|
||||
{
|
||||
private readonly IQueueFileOperationsService _queueFileOperationsService;
|
||||
private readonly ApiConfiguration _apiConfiguration;
|
||||
|
||||
private readonly ILcuServiceFactory<IFileActionService> _lcuServiceFactory;
|
||||
|
||||
public FileOperationsService(IQueueFileOperationsService queueFileOperationsService,
|
||||
ApiConfiguration apiConfiguration,
|
||||
ILcuServiceFactory<IFileActionService> lcuServiceFactory)
|
||||
{
|
||||
_queueFileOperationsService = queueFileOperationsService;
|
||||
_apiConfiguration = apiConfiguration;
|
||||
_lcuServiceFactory = lcuServiceFactory;
|
||||
}
|
||||
|
||||
public async Task HandleFileOperationsRequest(QueueRequest queueRequest)
|
||||
{
|
||||
var result = PerformFileOperation(queueRequest);
|
||||
|
||||
if (ShouldPerformFileOperationsAgain(queueRequest, result))
|
||||
{
|
||||
Log.Information($"Attempting to requeue file operations request. Request: {JsonConvert.SerializeObject(queueRequest)}");
|
||||
queueRequest.Attempts++;
|
||||
await _queueFileOperationsService.QueueFileOperationsRequest(queueRequest);
|
||||
return;
|
||||
}
|
||||
|
||||
await QueueReturnResponse.QueueResponse(queueRequest.ReturnResponse, result.IsSuccessful, result.MessageStack);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
var errorMessage = String.Join("; ", result.MessageStack.ToArray());
|
||||
throw new ServiceException($"File Operation Request was not successful. MessageStack:{errorMessage}");
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PerformFileOperation(QueueRequest queueRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
try
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(queueRequest);
|
||||
|
||||
var fileActionService = _lcuServiceFactory.GetService(queueRequest.FileOperationType);
|
||||
|
||||
return fileActionService.ExecuteAction(queueRequest);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
var failureMessage = $"Failed to perform file operations.";
|
||||
Log.Error(exception, failureMessage);
|
||||
|
||||
var fileOperationType = queueRequest?.FileOperationType;
|
||||
if (fileOperationType == null)
|
||||
{
|
||||
requestResponse.MessageStack.Add($"{failureMessage}. FileOperationType is Null. " +
|
||||
$"ExceptionMessage: {exception.Message}");
|
||||
return requestResponse;
|
||||
}
|
||||
|
||||
requestResponse = RequestResponseHelper.ExceptionUpdateRequestResponse(requestResponse, queueRequest.FileOperationType, exception.Message);
|
||||
|
||||
return requestResponse;
|
||||
}
|
||||
}
|
||||
|
||||
public bool ShouldPerformFileOperationsAgain(QueueRequest request, RequestResponse requestResponse)
|
||||
{
|
||||
return !requestResponse.IsSuccessful &&
|
||||
request.Attempts != null &&
|
||||
request.Attempts < _apiConfiguration.AttemptsLimit;
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
using System.Threading.Tasks;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.ApiResponse.Models;
|
||||
|
||||
namespace LCU.Api.Internal.FileOperations.Services;
|
||||
|
||||
public interface IDirectFileOperationsService
|
||||
{
|
||||
Task<RequestResponse> HandleDirectFileOperationsRequest<TRequest>(TRequest request,
|
||||
FileOperationType fileOperationType) where TRequest : ReturnResponse;
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
namespace LCU.Api.Internal.FileOperations.Services
|
||||
{
|
||||
public interface IDirectFileService
|
||||
{
|
||||
byte[] GetFileBytes(string path);
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
using System.Collections.Generic;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
namespace LCU.Api.Internal.FileOperations.Services;
|
||||
|
||||
public interface IDynamicDirectoryService
|
||||
{
|
||||
public IEnumerable<string> BuildDirectoryList(DynamicDirectoryListRequest request);
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LCU.Api.Internal.FileOperations.Services;
|
||||
|
||||
public interface IFileOperationsService
|
||||
{
|
||||
RequestResponse PerformFileOperation(QueueRequest queueRequest);
|
||||
Task HandleFileOperationsRequest(QueueRequest queueRequest);
|
||||
}
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using PdfSharp.Pdf;
|
||||
using PdfSharp.Pdf.IO;
|
||||
using Serilog;
|
||||
using Newtonsoft.Json;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
||||
using LCU.Api.Internal.FileOperations.Validations;
|
||||
using LCU.Library.Internal.LCUServiceFactory.Configurations;
|
||||
namespace LCU.Api.Internal.FileOperations.Services.PdfServices;
|
||||
|
||||
public class BatchPdfParserService : FileActionService, IFileActionService, IBatchPdfParserService
|
||||
{
|
||||
public BatchPdfParserService(ServicesFactoryConfiguration servicesFactoryConfiguration)
|
||||
: base(servicesFactoryConfiguration)
|
||||
{
|
||||
}
|
||||
|
||||
public RequestResponse ExecuteAction(QueueRequest queueRequest)
|
||||
{
|
||||
var requestResponse = new RequestResponse();
|
||||
try
|
||||
{
|
||||
var request = JsonConvert.DeserializeObject<BatchParsePdfRequest>(queueRequest.Request);
|
||||
requestResponse = PerformRequest(request);
|
||||
requestResponse.ReturnResponse = request;
|
||||
return requestResponse;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return RequestResponseHelper.ExceptionUpdateRequestResponse(requestResponse,
|
||||
queueRequest.FileOperationType,
|
||||
exception.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public RequestResponse PerformRequest(BatchParsePdfRequest request)
|
||||
{
|
||||
return OperationService.PerformOperation(request, IsRequestValid, BatchParsePdfs);
|
||||
}
|
||||
|
||||
public void BatchParsePdfs(BatchParsePdfRequest batchParsePdfRequest)
|
||||
{
|
||||
try
|
||||
{
|
||||
var inputDocument = PdfReader.Open(batchParsePdfRequest.FullPdfPath, PdfDocumentOpenMode.Import);
|
||||
foreach(var batchPdf in batchParsePdfRequest.BatchPdfs)
|
||||
{
|
||||
ParseAndSavePdf(batchParsePdfRequest, batchPdf, inputDocument);
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Log.Error(exception, $"Failure to Batch Parse Pdfs. ParsePdfRequest:{JsonConvert.SerializeObject(batchParsePdfRequest)}");
|
||||
throw new Exception($"Failure to Batch Parse Pdfs. ExceptionMessage:{exception.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public void ParseAndSavePdf(BatchParsePdfRequest batchParsePdfRequest, BatchPdf batchPdf, PdfDocument inputDocument)
|
||||
{
|
||||
var outputDocument = AddPdfPagesToOutputDocument(batchPdf.PageStart, batchPdf.PageStop, inputDocument);
|
||||
string name = Path.GetFileNameWithoutExtension(batchParsePdfRequest.PdfName);
|
||||
var newPdfName = string.IsNullOrWhiteSpace(batchPdf.ParsedPdfname) ? $"{name}.{batchPdf.PageStart}.pdf" : batchPdf.ParsedPdfname;
|
||||
var newPath = Path.Combine(batchParsePdfRequest.RootPath, newPdfName);
|
||||
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
||||
outputDocument.Save(newPath);
|
||||
}
|
||||
|
||||
public PdfDocument AddPdfPagesToOutputDocument(int pageStart, int pageStop, PdfDocument inputDocument)
|
||||
{
|
||||
var startIndex = pageStart - 1;
|
||||
var stopIndex = pageStop - 1;
|
||||
var outputDocument = new PdfDocument();
|
||||
for (int page = startIndex; page <= stopIndex; page++)
|
||||
{
|
||||
outputDocument.Version = inputDocument.Version;
|
||||
outputDocument.Info.Title = inputDocument.Info.Title;
|
||||
outputDocument.Info.Creator = inputDocument.Info.Creator;
|
||||
outputDocument.AddPage(inputDocument.Pages[page]);
|
||||
}
|
||||
|
||||
return outputDocument;
|
||||
}
|
||||
|
||||
public RequestResponse IsRequestValid(BatchParsePdfRequest request, RequestResponse requestResponse)
|
||||
{
|
||||
requestResponse = DirectoryValidation.ValidateDirectoryExists(request.RootPath, requestResponse);
|
||||
requestResponse = FileValidation.ValidateFileExists(request.FullPdfPath, requestResponse);
|
||||
return requestResponse;
|
||||
}
|
||||
}
|
||||
+193
@@ -0,0 +1,193 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using Serilog;
|
||||
using Newtonsoft.Json;
|
||||
using LCU.Library.Internal.Exceptions;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models.HtmlToPdfModels;
|
||||
using LCU.Api.Internal.FileOperations.Configurations;
|
||||
using System.Threading;
|
||||
namespace LCU.Api.Internal.FileOperations.Services.PdfServices;
|
||||
|
||||
public class HtmlToPdfService : IHtmlToPdfService
|
||||
{
|
||||
private readonly ApiConfiguration _apiConfiguration;
|
||||
private readonly IDirectFileService _directFileService;
|
||||
|
||||
public HtmlToPdfService(ApiConfiguration apiConfiguration, IDirectFileService directFileService)
|
||||
{
|
||||
_apiConfiguration = apiConfiguration;
|
||||
_directFileService = directFileService;
|
||||
}
|
||||
|
||||
public byte[] ConvertHtmlToPdf(HtmlToPdfRequest htmlToPdfRequest)
|
||||
{
|
||||
try
|
||||
{
|
||||
var output = CreateHtmlOutput(htmlToPdfRequest);
|
||||
var finalOutput = CleanUpUnusedTagsFromOutput(output);
|
||||
|
||||
File.WriteAllText($"{htmlToPdfRequest.FinalHtmlSaveFilePath}{htmlToPdfRequest.PdfName}.html", finalOutput);
|
||||
var fileCreated = CreatePdfFromHtml(htmlToPdfRequest);
|
||||
|
||||
if (!fileCreated)
|
||||
{
|
||||
throw new ServiceException($"Pdf file was not created. HtmlToPdfRequest: {JsonConvert.SerializeObject(htmlToPdfRequest)}");
|
||||
}
|
||||
|
||||
var pdfBytes = _directFileService.GetFileBytes(htmlToPdfRequest.FinalPdfSaveFilePathWithFile);
|
||||
|
||||
CleanUpHtmlAndPdfFiles(htmlToPdfRequest);
|
||||
|
||||
return pdfBytes;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
var message = "Failure to Convert Html To Pdf.";
|
||||
Log.Error(exception, $"{message} HtmlToPdfRequest:{JsonConvert.SerializeObject(htmlToPdfRequest)}");
|
||||
throw new ServiceException($"{message}. ExceptionMessage:{exception.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public string CreateHtmlOutput(HtmlToPdfRequest htmlToPdfRequest)
|
||||
{
|
||||
var templateOutput = AddBodyValuesToTemplateOutput(htmlToPdfRequest);
|
||||
templateOutput = BuildOutputFromCollectionValues(htmlToPdfRequest.BodyValueCollections, templateOutput);
|
||||
return templateOutput;
|
||||
}
|
||||
|
||||
public string AddBodyValuesToTemplateOutput(HtmlToPdfRequest htmlToPdfRequest)
|
||||
{
|
||||
var templateOutput = File.ReadAllText(htmlToPdfRequest.HtmlTemplateFilePath);
|
||||
templateOutput = AddTagValuesToOutput(templateOutput, htmlToPdfRequest.BodyTagValues);
|
||||
return templateOutput;
|
||||
}
|
||||
|
||||
public string BuildOutputFromCollectionValues(List<ValueCollection> valueCollections, string output)
|
||||
{
|
||||
if (valueCollections == null || !valueCollections.Any())
|
||||
{
|
||||
return output;
|
||||
}
|
||||
|
||||
foreach (var bodyValueCollection in valueCollections)
|
||||
{
|
||||
var rowTemplateOutput = File.ReadAllText(bodyValueCollection.CollectionHtmlTemplateFilePath);
|
||||
var rowsBuilder = new StringBuilder();
|
||||
rowsBuilder = AddCollectionItemsToStringBuilder(bodyValueCollection.ValueCollectionItems, rowTemplateOutput, rowsBuilder);
|
||||
output = output.Replace($"{{{bodyValueCollection.CollectionTag}}}", rowsBuilder.ToString());
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
public string AddTagValuesToOutput(string output, List<TagValue> tagValues)
|
||||
{
|
||||
foreach (var tagValue in tagValues)
|
||||
{
|
||||
output = output.Replace($"{{{tagValue.Tag}}}", tagValue.Value);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
public StringBuilder AddCollectionItemsToStringBuilder(List<ValueCollectionItem> valueCollectionItems, string output, StringBuilder builder)
|
||||
{
|
||||
foreach (var valueCollectionItem in valueCollectionItems)
|
||||
{
|
||||
var tempOutput = output;
|
||||
tempOutput = AddTagValuesToOutput(tempOutput, valueCollectionItem.TagValues);
|
||||
|
||||
if (valueCollectionItem.ValueCollections != null && valueCollectionItem.ValueCollections.Any())
|
||||
{
|
||||
tempOutput = BuildItemValueCollectionsOutput(valueCollectionItem.ValueCollections, tempOutput);
|
||||
}
|
||||
|
||||
builder.Append(tempOutput);
|
||||
}
|
||||
return builder;
|
||||
}
|
||||
|
||||
public string BuildItemValueCollectionsOutput(List<ValueCollection> valueCollections, string templateOutput)
|
||||
{
|
||||
if (valueCollections == null)
|
||||
{
|
||||
return templateOutput;
|
||||
}
|
||||
|
||||
templateOutput = BuildOutputFromCollectionValues(valueCollections, templateOutput);
|
||||
return templateOutput;
|
||||
}
|
||||
|
||||
public string CleanUpUnusedTagsFromOutput(string output)
|
||||
{
|
||||
var pattern = @"{[A-Za-z0-9]*}";
|
||||
var finalOutput = Regex.Replace(output, pattern, string.Empty);
|
||||
return finalOutput;
|
||||
}
|
||||
|
||||
public void SaveHtmlToFilePath(HtmlToPdfRequest htmlToPdfRequest, string htmlOutput)
|
||||
{
|
||||
|
||||
using (StreamWriter outputFile = new StreamWriter($"{htmlToPdfRequest.FinalHtmlSaveFilePath}{htmlToPdfRequest.PdfName}.html"))
|
||||
{
|
||||
outputFile.Write(htmlOutput);
|
||||
}
|
||||
}
|
||||
|
||||
public bool CreatePdfFromHtml(HtmlToPdfRequest htmlToPdfRequest)
|
||||
{
|
||||
try
|
||||
{
|
||||
var url = $"{htmlToPdfRequest.FinalHtmlSaveFilePath}{htmlToPdfRequest.PdfName}.html";
|
||||
var output = Path.Combine(htmlToPdfRequest.FinalPdfSaveFilePath, $"{htmlToPdfRequest.PdfName}.pdf");
|
||||
using (var process = new Process())
|
||||
{
|
||||
process.StartInfo.FileName = _apiConfiguration.ChromePath;
|
||||
process.StartInfo.Arguments = $"--user-data-dir={_apiConfiguration.FileOperationsChromeProfileFolderPath} " +
|
||||
$"--headless --disable-gpu --print-to-pdf={output} {url} --no-pdf-header-footer";
|
||||
process.Start();
|
||||
process.WaitForExit();
|
||||
}
|
||||
|
||||
var attempts = 0;
|
||||
var fileCreated = false;
|
||||
|
||||
while (!fileCreated)
|
||||
{
|
||||
fileCreated = File.Exists(htmlToPdfRequest.FinalPdfSaveFilePathWithFile);
|
||||
attempts++;
|
||||
|
||||
if (attempts > _apiConfiguration.MaxFileCheckAttempts)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
Thread.Sleep(1000);
|
||||
}
|
||||
|
||||
return fileCreated;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
var message = $"Failure to Create Pdf From Html.";
|
||||
Log.Error(exception, message);
|
||||
throw new Exception($"{message}. Exception Message: {exception.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public void CleanUpHtmlAndPdfFiles(HtmlToPdfRequest htmlToPdfRequest)
|
||||
{
|
||||
if (!htmlToPdfRequest.KeepHtmlFile)
|
||||
{
|
||||
File.Delete(htmlToPdfRequest.FinalHtmlSaveFilePathWithFile);
|
||||
}
|
||||
|
||||
if (!htmlToPdfRequest.KeepPdfFile)
|
||||
{
|
||||
File.Delete(htmlToPdfRequest.FinalPdfSaveFilePathWithFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
namespace LCU.Api.Internal.FileOperations.Services;
|
||||
|
||||
public interface IBatchPdfParserService
|
||||
{
|
||||
RequestResponse ExecuteAction(QueueRequest queueRequest);
|
||||
RequestResponse IsRequestValid(BatchParsePdfRequest request, RequestResponse requestResponse);
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
using LCU.Library.Internal.FileOperationsModels.Models.HtmlToPdfModels;
|
||||
namespace LCU.Api.Internal.FileOperations.Services.PdfServices;
|
||||
|
||||
public interface IHtmlToPdfService
|
||||
{
|
||||
byte[] ConvertHtmlToPdf(HtmlToPdfRequest htmlToPdfRequest);
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
|
||||
namespace LCU.Api.Internal.FileOperations.Services
|
||||
{
|
||||
public interface IPdfParserService
|
||||
{
|
||||
string ParsePdf(ParsePdfRequest parsePdfRequest);
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using PdfSharp.Pdf;
|
||||
using PdfSharp.Pdf.IO;
|
||||
using Serilog;
|
||||
using Newtonsoft.Json;
|
||||
using LCU.Library.Internal.Exceptions;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
namespace LCU.Api.Internal.FileOperations.Services.PdfServices;
|
||||
|
||||
public class PdfParserService : IPdfParserService
|
||||
{
|
||||
public string ParsePdf(ParsePdfRequest parsePdfRequest)
|
||||
{
|
||||
try
|
||||
{
|
||||
var fullPath = Path.Combine(parsePdfRequest.RootPath, parsePdfRequest.PdfName);
|
||||
var inputDocument = PdfReader.Open(fullPath, PdfDocumentOpenMode.Import);
|
||||
|
||||
|
||||
var startIndex = parsePdfRequest.PageStart - 1;
|
||||
var stopIndex = parsePdfRequest.PageStop - 1;
|
||||
var outputDocument = new PdfDocument();
|
||||
for (int page = startIndex; page <= stopIndex; page++)
|
||||
{
|
||||
outputDocument.Version = inputDocument.Version;
|
||||
outputDocument.Info.Title = inputDocument.Info.Title;
|
||||
outputDocument.Info.Creator = inputDocument.Info.Creator;
|
||||
outputDocument.AddPage(inputDocument.Pages[page]);
|
||||
}
|
||||
|
||||
string name = Path.GetFileNameWithoutExtension(parsePdfRequest.PdfName);
|
||||
var newPdfName = string.IsNullOrWhiteSpace(parsePdfRequest.ParsedPdfname) ? $"{name}.{parsePdfRequest.PageStart}.pdf" : parsePdfRequest.ParsedPdfname;
|
||||
var newPath = Path.Combine(parsePdfRequest.RootPath, newPdfName);
|
||||
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
||||
outputDocument.Save(newPath);
|
||||
|
||||
return newPdfName;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Log.Error(exception, $"Failure to Parse Pdf. ParsePdfRequest:{JsonConvert.SerializeObject(parsePdfRequest)}");
|
||||
throw new ServiceException($"Failure to Parse Pdf. ExceptionMessage:{exception.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
using LCU.Library.Internal.ApiResponse.Models;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using System.Threading.Tasks;
|
||||
namespace LCU.Api.Internal.FileOperations.Services.QueueServices;
|
||||
|
||||
public interface IQueueFileOperationsService
|
||||
{
|
||||
Task QueueFileOperations<TRequest>(TRequest originalRequest, FileOperationType operationType) where TRequest : ReturnResponse;
|
||||
Task QueueFileOperationsRequest(QueueRequest queueRequest);
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading;
|
||||
using Newtonsoft.Json;
|
||||
using Serilog;
|
||||
using MassTransit;
|
||||
using LCU.Library.Internal.LcuMassTransitModels;
|
||||
using LCU.Library.Internal.Exceptions;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.ApiResponse.Models;
|
||||
namespace LCU.Api.Internal.FileOperations.Services.QueueServices;
|
||||
|
||||
public class QueueFileOperationsService : IQueueFileOperationsService
|
||||
{
|
||||
private readonly ISendEndpoint _sendEndpoint;
|
||||
private readonly ReceiveEndpointConfiguration _receiveEndpointConfiguration;
|
||||
|
||||
public QueueFileOperationsService(IBusControl busControl,
|
||||
ReceiveEndpointConfiguration receiveEndpointConfiguration)
|
||||
{
|
||||
_receiveEndpointConfiguration = receiveEndpointConfiguration;
|
||||
_sendEndpoint = busControl.GetSendEndpoint(_receiveEndpointConfiguration.ReceiveEndpointUri).Result;
|
||||
}
|
||||
|
||||
public async Task QueueFileOperations<TRequest>(TRequest originalRequest, FileOperationType operationType) where TRequest : ReturnResponse
|
||||
{
|
||||
try
|
||||
{
|
||||
QueueRequest queueRequest = new QueueRequest()
|
||||
{
|
||||
ReturnResponse = originalRequest,
|
||||
FileOperationType = operationType,
|
||||
Request = JsonConvert.SerializeObject(originalRequest),
|
||||
Attempts = 0,
|
||||
QueueTimeStamp = DateTime.Now
|
||||
};
|
||||
|
||||
await QueueFileOperationsRequest(queueRequest);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, $"Failed to Queue File Operations. FileOperationType: {operationType.ToString()}");
|
||||
throw new ServiceException($"Failed to Queue File Operation Request. FileOperationType: {operationType.ToString()}");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task QueueFileOperationsRequest(QueueRequest queueRequest)
|
||||
{
|
||||
try
|
||||
{
|
||||
var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(30));
|
||||
await _sendEndpoint.Send(queueRequest, cancellationTokenSource.Token);
|
||||
}
|
||||
catch (TaskCanceledException exception)
|
||||
{
|
||||
Log.Error(exception, $"Failed to Queue File Operation Request. FileOperationType: {queueRequest.FileOperationType}");
|
||||
throw new ServiceException(exception.Message);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Log.Error(exception, $"Failed to Queue File Operation Request. FileOperationType: {queueRequest.FileOperationType}");
|
||||
throw new ServiceException($"Failed to Queue File Operation Request. FileOperationType: {queueRequest.FileOperationType}");
|
||||
}
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
namespace LCU.Api.Internal.FileOperations.Services;
|
||||
|
||||
public static class RequestResponseHelper
|
||||
{
|
||||
public static RequestResponse ExceptionUpdateRequestResponse(RequestResponse requestResponse,
|
||||
FileOperationType fileOperationType,
|
||||
string exception)
|
||||
{
|
||||
requestResponse.IsSuccessful = false;
|
||||
requestResponse.MessageStack.Add($"Failure to execute file action. " +
|
||||
$"FileOperationType: {fileOperationType}, " +
|
||||
$"Exception: {exception} ");
|
||||
return requestResponse;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user