Skip to content

Commit 40db074

Browse files
author
TRybina132
committed
Merge branch 'develop' into rybina/test_azure_stream
2 parents afccd53 + cb4f311 commit 40db074

File tree

5 files changed

+43
-66
lines changed

5 files changed

+43
-66
lines changed

ManagedCode.Storage.IntegrationTests/Helpers/Crc32Helper.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,31 @@ public static uint Calculate(byte[] bytes)
3434
}
3535
return ~crcValue;
3636
}
37+
38+
public static uint CalculateFileCRC(string filePath)
39+
{
40+
uint crcValue = 0xffffffff;
41+
42+
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
43+
{
44+
byte[] buffer = new byte[4096]; // 4KB buffer
45+
int bytesRead;
46+
while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)
47+
{
48+
crcValue = Calculate(buffer, crcValue);
49+
}
50+
}
51+
52+
return ~crcValue; // Return the final CRC value
53+
}
54+
55+
public static uint Calculate(byte[] bytes, uint crcValue = 0xffffffff)
56+
{
57+
foreach (byte by in bytes)
58+
{
59+
byte tableIndex = (byte)(((crcValue) & 0xff) ^ by);
60+
crcValue = Crc32Table[tableIndex] ^ (crcValue >> 8);
61+
}
62+
return crcValue;
63+
}
3764
}

ManagedCode.Storage.IntegrationTests/TestApp/Controllers/Base/BaseFileController.cs

Lines changed: 0 additions & 50 deletions
This file was deleted.

ManagedCode.Storage.IntegrationTests/TestApp/Controllers/Base/BaseTestController.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Amazon.Runtime.Internal;
1+
using Amazon.Runtime.Internal;
22
using ManagedCode.Communication;
33
using ManagedCode.Storage.Core;
44
using ManagedCode.Storage.Core.Models;
@@ -9,18 +9,18 @@
99
namespace ManagedCode.Storage.IntegrationTests.TestApp.Controllers.Base;
1010

1111
[ApiController]
12-
public abstract class BaseTestController<TStorage> : BaseController
12+
public abstract class BaseTestController<TStorage> : ControllerBase
1313
where TStorage : IStorage
1414
{
15-
private readonly ResponseContext _responseData;
16-
private readonly int chunkSize;
17-
private readonly string tempFolder;
15+
protected readonly IStorage Storage;
16+
protected readonly ResponseContext ResponseData;
17+
protected readonly int ChunkSize;
1818

19-
protected BaseTestController(TStorage storage) : base(storage)
19+
protected BaseTestController(TStorage storage)
2020
{
21-
_responseData = new ResponseContext();
22-
chunkSize = 100000000;
23-
tempFolder = "C:\\Users\\sasha";
21+
Storage = storage;
22+
ResponseData = new ResponseContext();
23+
ChunkSize = 100000000;
2424
}
2525

2626
[HttpPost("upload")]
@@ -55,11 +55,11 @@ public async Task<IActionResult> UploadChunks(CancellationToken cancellationToke
5555
try
5656
{
5757
var chunkNumber = Guid.NewGuid().ToString();
58-
string newpath = Path.Combine(tempFolder + "/TEMP", "file" + chunkNumber);
58+
string newpath = Path.Combine(Path.GetTempPath(), "file" + chunkNumber);
5959

6060
await using (FileStream fs = System.IO.File.Create(newpath))
6161
{
62-
byte[] bytes = new byte[chunkSize];
62+
byte[] bytes = new byte[ChunkSize];
6363
int bytesRead = 0;
6464
while ((bytesRead = await Request.Body.ReadAsync(bytes, 0, bytes.Length, cancellationToken)) > 0)
6565
{
@@ -73,15 +73,15 @@ public async Task<IActionResult> UploadChunks(CancellationToken cancellationToke
7373
// _responseData.IsSuccess = false;
7474
}
7575

76-
return Ok(_responseData);
76+
return Ok(ResponseData);
7777
}
7878

7979
[HttpPost("upload-chunks/complete")]
8080
public async Task<Result> UploadComplete([FromBody] string fileName)
8181
{
8282
try
8383
{
84-
string tempPath = tempFolder + "/TEMP";
84+
string tempPath = Path.GetTempPath();
8585
string newPath = Path.Combine(tempPath, fileName);
8686
// string[] filePaths = Directory.GetFiles(tempPath).Where(p => p.Contains(fileName))
8787
// .OrderBy(p => Int32.Parse(p.Replace(fileName, "$").Split('$')[1])).ToArray();
@@ -91,7 +91,7 @@ public async Task<Result> UploadComplete([FromBody] string fileName)
9191
MergeChunks(newPath, filePath);
9292
}
9393

94-
System.IO.File.Move(Path.Combine(tempPath, fileName), Path.Combine(tempFolder, fileName));
94+
System.IO.File.Move(Path.Combine(tempPath, fileName), Path.Combine(tempPath, fileName));
9595
}
9696
catch (Exception ex)
9797
{

ManagedCode.Storage.IntegrationTests/Tests/BaseDownloadControllerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public async Task DownloadFile_WhenFileExists_SaveToTempStorage_ReturnSuccess()
3636
// Assert
3737
downloadedFileResult.IsSuccess.Should().BeTrue();
3838
downloadedFileResult.Value.Should().NotBeNull();
39-
var downloadedFileCRC = Crc32Helper.Calculate(await downloadedFileResult.Value.ReadAllBytesAsync());
39+
var downloadedFileCRC = Crc32Helper.CalculateFileCRC(downloadedFileResult.Value.FilePath);
4040
downloadedFileCRC.Should().Be(fileCRC);
4141
}
4242

ManagedCode.Storage.IntegrationTests/Tests/BaseUploadControllerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public async Task UploadFileInChunks_WhenFileValid_ReturnSuccess()
124124
var contentName = "file";
125125

126126
await using var localFile = LocalFile.FromRandomNameWithExtension(".txt");
127-
FileHelper.GenerateLocalFile(localFile, 500);
127+
FileHelper.GenerateLocalFile(localFile, 20);
128128

129129
// Act
130130
var result = await storageClient.UploadFileInChunks(localFile.FileStream, _uploadChunksEndpoint, 100000000);

0 commit comments

Comments
 (0)