Skip to content

Commit 8fb3447

Browse files
authored
Merge pull request #794 from TechnologyEnhancedLearning/Develop/Fixes/TD-5176
TD-5176 file download refactor
2 parents 2a98d45 + 5caadb0 commit 8fb3447

File tree

1 file changed

+5
-50
lines changed

1 file changed

+5
-50
lines changed

LearningHub.Nhs.WebUI/Services/FileService.cs

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Threading.Tasks;
1010
using System.Threading.Tasks.Dataflow;
1111
using Azure;
12+
using Azure.Storage;
1213
using Azure.Storage.Files.Shares;
1314
using Azure.Storage.Files.Shares.Models;
1415
using LearningHub.Nhs.Models.Resource;
@@ -133,10 +134,6 @@ public async Task DeleteChunkDirectory(string directoryRef, int chunks)
133134
/// <returns>The <see cref="Task{FileDownloadResponse}"/>.</returns>
134135
public async Task<FileDownloadResponse> DownloadFileAsync(string filePath, string fileName)
135136
{
136-
const int ChunkSizeInMB = 100;
137-
const int MaxParallelDownloads = 8;
138-
int chunkSize = ChunkSizeInMB * 1024 * 1024;
139-
140137
var file = await this.FindFileAsync(filePath, fileName);
141138
if (file == null)
142139
{
@@ -146,9 +143,9 @@ public async Task<FileDownloadResponse> DownloadFileAsync(string filePath, strin
146143
var properties = await file.GetPropertiesAsync();
147144
long fileSize = properties.Value.ContentLength;
148145

149-
// If the file size is less than 500 MB, download it directly
150-
if (fileSize <= 500 * 1024 * 1024)
146+
try
151147
{
148+
// Directly download the entire file as a stream
152149
var response = await file.DownloadAsync();
153150
return new FileDownloadResponse
154151
{
@@ -157,52 +154,10 @@ public async Task<FileDownloadResponse> DownloadFileAsync(string filePath, strin
157154
ContentLength = fileSize,
158155
};
159156
}
160-
161-
var pipe = new System.IO.Pipelines.Pipe();
162-
var semaphore = new SemaphoreSlim(1, 1);
163-
164-
var downloadBlock = new ActionBlock<long>(
165-
async offset =>
166-
{
167-
long rangeSize = Math.Min(chunkSize, fileSize - offset);
168-
try
169-
{
170-
var response = await file.DownloadAsync(new HttpRange(offset, rangeSize));
171-
var buffer = new byte[rangeSize];
172-
await response.Value.Content.ReadAsync(buffer, 0, (int)rangeSize);
173-
174-
await semaphore.WaitAsync();
175-
try
176-
{
177-
pipe.Writer.Write(buffer);
178-
}
179-
finally
180-
{
181-
semaphore.Release();
182-
}
183-
}
184-
catch (Exception ex)
185-
{
186-
throw new Exception($"Error downloading chunk at offset {offset}: {ex.Message}");
187-
}
188-
},
189-
new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = MaxParallelDownloads, EnsureOrdered = false });
190-
191-
for (long offset = 0; offset < fileSize; offset += chunkSize)
157+
catch (Exception ex)
192158
{
193-
downloadBlock.Post(offset);
159+
throw new Exception($"Error downloading file: {ex.Message}");
194160
}
195-
196-
downloadBlock.Complete();
197-
await downloadBlock.Completion;
198-
await pipe.Writer.CompleteAsync();
199-
200-
return new FileDownloadResponse
201-
{
202-
Content = pipe.Reader.AsStream(),
203-
ContentType = properties.Value.ContentType,
204-
ContentLength = fileSize,
205-
};
206161
}
207162

208163
/// <summary>

0 commit comments

Comments
 (0)