Skip to content

Commit 1a5fb6f

Browse files
committed
add delegate
1 parent fc4cd8e commit 1a5fb6f

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

ManagedCode.Storage.Client/IStorageClient.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.IO;
1+
using System;
2+
using System.IO;
23
using System.Threading;
34
using System.Threading.Tasks;
45
using ManagedCode.Communication;
@@ -12,6 +13,6 @@ public interface IStorageClient
1213
Task<Result<BlobMetadata>> UploadFile(FileInfo fileInfo, string apiUrl, string contentName, CancellationToken cancellationToken = default);
1314
Task<Result<BlobMetadata>> UploadFile(byte[] bytes, string apiUrl, string contentName, CancellationToken cancellationToken = default);
1415
Task<Result<BlobMetadata>> UploadFile(string base64, string apiUrl, string contentName, CancellationToken cancellationToken = default);
15-
Task<Result> UploadLargeFile(Stream file, string сreateApiUrl, string uploadApiUrl, string completeApiUrl, CancellationToken cancellationToken = default);
16+
Task<Result> UploadLargeFile(Stream file, string сreateApiUrl, string uploadApiUrl, string completeApiUrl, Action<double>? onProgressChanged, CancellationToken cancellationToken = default);
1617
Task<Result<LocalFile>> DownloadFile(string fileName, string apiUrl, string? path = null, CancellationToken cancellationToken = default);
1718
}

ManagedCode.Storage.Client/StorageClient.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,15 @@ public async Task<Result> UploadLargeFile(Stream file,
128128
string сreateApiUrl,
129129
string uploadApiUrl,
130130
string completeApiUrl,
131+
Action<double>? onProgressChanged,
131132
CancellationToken cancellationToken)
132133
{
133134
var bufferSize = 4096000; //TODO: chunk size get from config
134135
var buffer = new byte[bufferSize];
135136
int bytesRead;
136137
int chunkIndex = 1;
137138
var fileCRC = Crc32Helper.Calculate(file);
139+
var partOfProgress = file.Length / bufferSize;
138140

139141
var createdFileResponse = await _httpClient.PostAsync(сreateApiUrl, JsonContent.Create(file.Length), cancellationToken);
140142
var createdFile = await createdFileResponse.Content.ReadFromJsonAsync<Result<BlobMetadata>>(cancellationToken: cancellationToken);
@@ -162,6 +164,8 @@ public async Task<Result> UploadLargeFile(Stream file,
162164

163165
_httpClient.PostAsync(uploadApiUrl, chunk, cancellationToken);
164166
}
167+
168+
onProgressChanged?.Invoke(partOfProgress * chunkIndex);
165169
}
166170
}
167171
finally

ManagedCode.Storage.IntegrationTests/Tests/BaseUploadControllerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public async Task UploadFileInChunks_WhenFileValid_ReturnSuccess()
128128

129129
// Act
130130
var result = await storageClient.UploadLargeFile(localFile.FileStream, _uploadChunksEndpoint + "/create",
131-
_uploadChunksEndpoint + "/upload", _uploadChunksEndpoint + "/complete", new CancellationToken());
131+
_uploadChunksEndpoint + "/upload", _uploadChunksEndpoint + "/complete", null, new CancellationToken());
132132

133133
// Assert
134134
result.IsSuccess.Should().BeTrue();

0 commit comments

Comments
 (0)