diff --git a/ManagedCode.Storage.IntegrationTests/Helpers/Crc32Helper.cs b/ManagedCode.Storage.IntegrationTests/Helpers/Crc32Helper.cs index 2f72705..b5ef820 100644 --- a/ManagedCode.Storage.IntegrationTests/Helpers/Crc32Helper.cs +++ b/ManagedCode.Storage.IntegrationTests/Helpers/Crc32Helper.cs @@ -34,4 +34,31 @@ public static uint Calculate(byte[] bytes) } return ~crcValue; } + + public static uint CalculateFileCRC(string filePath) + { + uint crcValue = 0xffffffff; + + using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) + { + byte[] buffer = new byte[4096]; // 4KB buffer + int bytesRead; + while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0) + { + crcValue = Calculate(buffer, crcValue); + } + } + + return ~crcValue; // Return the final CRC value + } + + public static uint Calculate(byte[] bytes, uint crcValue = 0xffffffff) + { + foreach (byte by in bytes) + { + byte tableIndex = (byte)(((crcValue) & 0xff) ^ by); + crcValue = Crc32Table[tableIndex] ^ (crcValue >> 8); + } + return crcValue; + } } \ No newline at end of file diff --git a/ManagedCode.Storage.IntegrationTests/TestApp/Controllers/Base/BaseFileController.cs b/ManagedCode.Storage.IntegrationTests/TestApp/Controllers/Base/BaseFileController.cs deleted file mode 100644 index 1245bdf..0000000 --- a/ManagedCode.Storage.IntegrationTests/TestApp/Controllers/Base/BaseFileController.cs +++ /dev/null @@ -1,50 +0,0 @@ -using Amazon.Runtime.Internal; -using ManagedCode.Communication; -using ManagedCode.Storage.Core; -using ManagedCode.Storage.Core.Models; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.ModelBinding; - -namespace ManagedCode.Storage.IntegrationTests.TestApp.Controllers.Base; - -[ApiController] -public class BaseFileController : ControllerBase -{ - private readonly IStorage _storage; - - public BaseFileController(IStorage storage) - { - _storage = storage; - - } - - [HttpPost("upload")] - public async Task> UploadFile([FromBody] MultipartFormDataContent content, CancellationToken cancellationToken) - { - try - { - var result = await _storage.UploadAsync(await content.ReadAsStreamAsync(cancellationToken), cancellationToken); - return result; - } - catch (Exception ex) - { - return Result.Fail(); - } - } - - [HttpGet("download/{fileName}")] - public async Task> DownloadFile([FromQuery] string fileName, CancellationToken cancellationToken) - { - var result = await _storage.DownloadAsync(fileName, cancellationToken); - - if (result.Value is null) - { - return Result.Fail(); - } - else - { - return Result.Succeed(result.Value.FileStream); - } - } -} \ No newline at end of file diff --git a/ManagedCode.Storage.IntegrationTests/TestApp/Controllers/Base/BaseTestController.cs b/ManagedCode.Storage.IntegrationTests/TestApp/Controllers/Base/BaseTestController.cs index f7f6cf5..f83eb7a 100644 --- a/ManagedCode.Storage.IntegrationTests/TestApp/Controllers/Base/BaseTestController.cs +++ b/ManagedCode.Storage.IntegrationTests/TestApp/Controllers/Base/BaseTestController.cs @@ -1,4 +1,4 @@ -using Amazon.Runtime.Internal; +using Amazon.Runtime.Internal; using ManagedCode.Communication; using ManagedCode.Storage.Core; using ManagedCode.Storage.Core.Models; @@ -9,18 +9,18 @@ namespace ManagedCode.Storage.IntegrationTests.TestApp.Controllers.Base; [ApiController] -public abstract class BaseTestController : BaseController +public abstract class BaseTestController : ControllerBase where TStorage : IStorage { - private readonly ResponseContext _responseData; - private readonly int chunkSize; - private readonly string tempFolder; + protected readonly IStorage Storage; + protected readonly ResponseContext ResponseData; + protected readonly int ChunkSize; - protected BaseTestController(TStorage storage) : base(storage) + protected BaseTestController(TStorage storage) { - _responseData = new ResponseContext(); - chunkSize = 100000000; - tempFolder = "C:\\Users\\sasha"; + Storage = storage; + ResponseData = new ResponseContext(); + ChunkSize = 100000000; } [HttpPost("upload")] @@ -55,11 +55,11 @@ public async Task UploadChunks(CancellationToken cancellationToke try { var chunkNumber = Guid.NewGuid().ToString(); - string newpath = Path.Combine(tempFolder + "/TEMP", "file" + chunkNumber); + string newpath = Path.Combine(Path.GetTempPath(), "file" + chunkNumber); await using (FileStream fs = System.IO.File.Create(newpath)) { - byte[] bytes = new byte[chunkSize]; + byte[] bytes = new byte[ChunkSize]; int bytesRead = 0; while ((bytesRead = await Request.Body.ReadAsync(bytes, 0, bytes.Length, cancellationToken)) > 0) { @@ -73,7 +73,7 @@ public async Task UploadChunks(CancellationToken cancellationToke // _responseData.IsSuccess = false; } - return Ok(_responseData); + return Ok(ResponseData); } [HttpPost("upload-chunks/complete")] @@ -81,7 +81,7 @@ public async Task UploadComplete([FromBody] string fileName) { try { - string tempPath = tempFolder + "/TEMP"; + string tempPath = Path.GetTempPath(); string newPath = Path.Combine(tempPath, fileName); // string[] filePaths = Directory.GetFiles(tempPath).Where(p => p.Contains(fileName)) // .OrderBy(p => Int32.Parse(p.Replace(fileName, "$").Split('$')[1])).ToArray(); @@ -91,7 +91,7 @@ public async Task UploadComplete([FromBody] string fileName) MergeChunks(newPath, filePath); } - System.IO.File.Move(Path.Combine(tempPath, fileName), Path.Combine(tempFolder, fileName)); + System.IO.File.Move(Path.Combine(tempPath, fileName), Path.Combine(tempPath, fileName)); } catch (Exception ex) { diff --git a/ManagedCode.Storage.IntegrationTests/Tests/BaseDownloadControllerTests.cs b/ManagedCode.Storage.IntegrationTests/Tests/BaseDownloadControllerTests.cs index 626838d..d07860b 100644 --- a/ManagedCode.Storage.IntegrationTests/Tests/BaseDownloadControllerTests.cs +++ b/ManagedCode.Storage.IntegrationTests/Tests/BaseDownloadControllerTests.cs @@ -36,7 +36,7 @@ public async Task DownloadFile_WhenFileExists_SaveToTempStorage_ReturnSuccess() // Assert downloadedFileResult.IsSuccess.Should().BeTrue(); downloadedFileResult.Value.Should().NotBeNull(); - var downloadedFileCRC = Crc32Helper.Calculate(await downloadedFileResult.Value.ReadAllBytesAsync()); + var downloadedFileCRC = Crc32Helper.CalculateFileCRC(downloadedFileResult.Value.FilePath); downloadedFileCRC.Should().Be(fileCRC); } diff --git a/ManagedCode.Storage.IntegrationTests/Tests/BaseUploadControllerTests.cs b/ManagedCode.Storage.IntegrationTests/Tests/BaseUploadControllerTests.cs index ffc8fec..c6ed9bd 100644 --- a/ManagedCode.Storage.IntegrationTests/Tests/BaseUploadControllerTests.cs +++ b/ManagedCode.Storage.IntegrationTests/Tests/BaseUploadControllerTests.cs @@ -124,7 +124,7 @@ public async Task UploadFileInChunks_WhenFileValid_ReturnSuccess() var contentName = "file"; await using var localFile = LocalFile.FromRandomNameWithExtension(".txt"); - FileHelper.GenerateLocalFile(localFile, 500); + FileHelper.GenerateLocalFile(localFile, 20); // Act var result = await storageClient.UploadFileInChunks(localFile.FileStream, _uploadChunksEndpoint, 100000000);