diff --git a/LearningHub.Nhs.WebUI/Services/FileService.cs b/LearningHub.Nhs.WebUI/Services/FileService.cs index de74a44e3..a55433c9f 100644 --- a/LearningHub.Nhs.WebUI/Services/FileService.cs +++ b/LearningHub.Nhs.WebUI/Services/FileService.cs @@ -9,6 +9,7 @@ using System.Threading.Tasks; using System.Threading.Tasks.Dataflow; using Azure; + using Azure.Storage; using Azure.Storage.Files.Shares; using Azure.Storage.Files.Shares.Models; using LearningHub.Nhs.Models.Resource; @@ -133,10 +134,6 @@ public async Task DeleteChunkDirectory(string directoryRef, int chunks) /// The . public async Task DownloadFileAsync(string filePath, string fileName) { - const int ChunkSizeInMB = 100; - const int MaxParallelDownloads = 8; - int chunkSize = ChunkSizeInMB * 1024 * 1024; - var file = await this.FindFileAsync(filePath, fileName); if (file == null) { @@ -146,9 +143,9 @@ public async Task DownloadFileAsync(string filePath, strin var properties = await file.GetPropertiesAsync(); long fileSize = properties.Value.ContentLength; - // If the file size is less than 500 MB, download it directly - if (fileSize <= 500 * 1024 * 1024) + try { + // Directly download the entire file as a stream var response = await file.DownloadAsync(); return new FileDownloadResponse { @@ -157,52 +154,10 @@ public async Task DownloadFileAsync(string filePath, strin ContentLength = fileSize, }; } - - var pipe = new System.IO.Pipelines.Pipe(); - var semaphore = new SemaphoreSlim(1, 1); - - var downloadBlock = new ActionBlock( - async offset => - { - long rangeSize = Math.Min(chunkSize, fileSize - offset); - try - { - var response = await file.DownloadAsync(new HttpRange(offset, rangeSize)); - var buffer = new byte[rangeSize]; - await response.Value.Content.ReadAsync(buffer, 0, (int)rangeSize); - - await semaphore.WaitAsync(); - try - { - pipe.Writer.Write(buffer); - } - finally - { - semaphore.Release(); - } - } - catch (Exception ex) - { - throw new Exception($"Error downloading chunk at offset {offset}: {ex.Message}"); - } - }, - new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = MaxParallelDownloads, EnsureOrdered = false }); - - for (long offset = 0; offset < fileSize; offset += chunkSize) + catch (Exception ex) { - downloadBlock.Post(offset); + throw new Exception($"Error downloading file: {ex.Message}"); } - - downloadBlock.Complete(); - await downloadBlock.Completion; - await pipe.Writer.CompleteAsync(); - - return new FileDownloadResponse - { - Content = pipe.Reader.AsStream(), - ContentType = properties.Value.ContentType, - ContentLength = fileSize, - }; } ///