updating
This commit is contained in:
+79
@@ -0,0 +1,79 @@
|
|||||||
|
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 WriteBinaryFileService : FileActionService, IFileActionService, IActionService<WriteFileRequest>
|
||||||
|
{
|
||||||
|
public WriteBinaryFileService(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, WriteBinaryFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void WriteBinaryFile(WriteFileRequest request)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var fileBytes = Convert.FromBase64String(request.FileData);
|
||||||
|
File.WriteAllBytes(request.FullFilePath, fileBytes);
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+49
@@ -0,0 +1,49 @@
|
|||||||
|
using LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
||||||
|
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||||
|
using LCU.Library.Internal.LCUServiceFactory.Configurations;
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace LCU.Api.Internal.FileOperationsTests.Services
|
||||||
|
{
|
||||||
|
public class WriteBinaryFileServiceTest : IDisposable
|
||||||
|
{
|
||||||
|
private readonly string _tempDirectory;
|
||||||
|
private readonly WriteBinaryFileService _writeBinaryFileService;
|
||||||
|
|
||||||
|
public WriteBinaryFileServiceTest()
|
||||||
|
{
|
||||||
|
_tempDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
|
||||||
|
Directory.CreateDirectory(_tempDirectory);
|
||||||
|
_writeBinaryFileService = new WriteBinaryFileService(new ServicesFactoryConfiguration());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void PerformRequest_WritesBinaryBytes()
|
||||||
|
{
|
||||||
|
var expectedBytes = new byte[] { 0x52, 0x49, 0x46, 0x46, 0x24, 0x00, 0x00, 0x00 };
|
||||||
|
var request = new WriteFileRequest
|
||||||
|
{
|
||||||
|
DestinationPath = _tempDirectory,
|
||||||
|
FileName = "sample",
|
||||||
|
Extension = ".wav",
|
||||||
|
FileData = Convert.ToBase64String(expectedBytes),
|
||||||
|
Overwrite = true
|
||||||
|
};
|
||||||
|
|
||||||
|
var result = _writeBinaryFileService.PerformRequest(request);
|
||||||
|
|
||||||
|
Assert.True(result.IsSuccessful);
|
||||||
|
Assert.Equal(expectedBytes, File.ReadAllBytes(request.FullFilePath));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
if (Directory.Exists(_tempDirectory))
|
||||||
|
{
|
||||||
|
Directory.Delete(_tempDirectory, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
- op: replace
|
||||||
|
path: /spec/replicas
|
||||||
|
value: 3
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
- op: replace
|
||||||
|
path: /spec/replicas
|
||||||
|
value: 3
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
- op: replace
|
||||||
|
path: /spec/replicas
|
||||||
|
value: 3
|
||||||
Reference in New Issue
Block a user