Skip to content

Commit 77dde0d

Browse files
committed
Bugfix: Correct File Chunk Stream Reading
This change fixes an issue where the file upload would not read and upload the file chunks successfully. The original code incorrectly assumed that Stream.Read() would always read a full chunk of data from the file stream, when in reality it may return read fewer bytes. To solve this, the new UploadChunkAsync helper method ensures the entire chunk is read from the stream before proceeding with the upload.
1 parent 09c9177 commit 77dde0d

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

Bynder/Sdk/Service/Upload/FileUploader.cs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Bynder. All rights reserved.
22
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.IO;
67
using System.Net.Http;
@@ -110,16 +111,20 @@ private async Task<SaveMediaResponse> UploadFileAsync(Stream fileStream, UploadQ
110111
{
111112
uint chunkNumber = 0;
112113
var uploadRequest = await GetUploadRequest(filename);
113-
using (fileStream)
114+
115+
await using (fileStream)
114116
{
115-
int bytesRead = 0;
116-
var buffer = new byte[CHUNK_SIZE];
117-
long numberOfChunks = (fileStream.Length + CHUNK_SIZE - 1) / CHUNK_SIZE;
117+
byte[] buffer = new byte[CHUNK_SIZE];
118+
long numberOfChunks = (fileStream.Length + buffer.Length - 1) / buffer.Length;
119+
long totalBytesRead = 0;
118120

119-
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0)
121+
while (totalBytesRead < fileStream.Length)
120122
{
121-
++chunkNumber;
122-
await UploadPartAsync(Path.GetFileName(query.Filepath), buffer, bytesRead, chunkNumber, uploadRequest, (uint)numberOfChunks).ConfigureAwait(false);
123+
int bytesToRead = (int)Math.Min(buffer.Length, fileStream.Length - totalBytesRead);
124+
125+
await UploadChunkAsync(uploadRequest, filename, fileStream, buffer, bytesToRead, numberOfChunks, ++chunkNumber);
126+
127+
totalBytesRead += bytesToRead;
123128
}
124129
}
125130

@@ -147,6 +152,25 @@ private async Task<SaveMediaResponse> UploadFileAsync(Stream fileStream, UploadQ
147152
}
148153
}
149154

155+
private async Task UploadChunkAsync(UploadRequest uploadRequest, string fileName, Stream fileStream, byte[] buffer, int bytesToRead, long numberOfChunks, uint chunkNumber)
156+
{
157+
int bytesReadForChunk = 0;
158+
159+
while (bytesReadForChunk < bytesToRead)
160+
{
161+
int bytesRead = await fileStream.ReadAsync(buffer, bytesReadForChunk, bytesToRead - bytesReadForChunk);
162+
163+
if (bytesRead == 0)
164+
{
165+
throw new EndOfStreamException();
166+
}
167+
168+
bytesReadForChunk += bytesRead;
169+
}
170+
171+
await UploadPartAsync(fileName, buffer, bytesReadForChunk, chunkNumber, uploadRequest, (uint)numberOfChunks).ConfigureAwait(false);
172+
}
173+
150174
/// <summary>
151175
/// Gets the closest s3 endpoint. This is needed to know to which bucket Url it uploads chunks
152176
/// </summary>

0 commit comments

Comments
 (0)