From 2598509df0fb5cffac1c797c9381e535036496c4 Mon Sep 17 00:00:00 2001 From: OluwatobiAwe <114475132+OluwatobiAwe@users.noreply.github.com> Date: Tue, 11 Feb 2025 10:27:52 +0000 Subject: [PATCH 1/2] Revert "Infastructure test for TD-5348" --- .../Controllers/Api/ResourceController.cs | 42 +------------------ LearningHub.Nhs.WebUI/Services/FileService.cs | 35 ++++------------ 2 files changed, 11 insertions(+), 66 deletions(-) diff --git a/LearningHub.Nhs.WebUI/Controllers/Api/ResourceController.cs b/LearningHub.Nhs.WebUI/Controllers/Api/ResourceController.cs index 9eddcc484..0616474ff 100644 --- a/LearningHub.Nhs.WebUI/Controllers/Api/ResourceController.cs +++ b/LearningHub.Nhs.WebUI/Controllers/Api/ResourceController.cs @@ -2,10 +2,6 @@ namespace LearningHub.Nhs.WebUI.Controllers.Api { using System; using System.Collections.Generic; - using System.IO; - using System.Linq; - using System.Net.Http.Headers; - using System.Threading; using System.Threading.Tasks; using LearningHub.Nhs.Models.Enums; using LearningHub.Nhs.Models.Resource; @@ -13,7 +9,6 @@ namespace LearningHub.Nhs.WebUI.Controllers.Api using LearningHub.Nhs.Models.Resource.Contribute; using LearningHub.Nhs.WebUI.Interfaces; using Microsoft.AspNetCore.Authorization; - using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; @@ -65,7 +60,6 @@ public async Task AcceptSensitiveContentAsync(int resourceVersionI /// File name. /// A representing the result of the asynchronous operation. [HttpGet("DownloadResource")] - [AllowAnonymous] public async Task DownloadResource(string filePath, string fileName) { if (string.IsNullOrEmpty(fileName)) @@ -76,15 +70,7 @@ public async Task DownloadResource(string filePath, string fileNa var file = await this.fileService.DownloadFileAsync(filePath, fileName); if (file != null) { - // Set response headers. - this.Response.ContentType = file.ContentType; - this.Response.ContentLength = file.ContentLength; - var contentDisposition = new ContentDispositionHeaderValue("attachment") { FileNameStar = fileName }; - this.Response.Headers["Content-Disposition"] = contentDisposition.ToString(); - - // Stream the file in chunks with periodic flushes to keep the connection active. - await this.StreamFileWithKeepAliveAsync(file.Content, this.Response.Body, this.HttpContext.RequestAborted); - return this.Ok(); + return this.File(file.Content, file.ContentType, fileName); } else { @@ -119,16 +105,7 @@ public async Task DownloadResourceAndRecordActivity(int resourceV ActivityStatus = ActivityStatusEnum.Completed, }; await this.activityService.CreateResourceActivityAsync(activity); - - // Set response headers. - this.Response.ContentType = file.ContentType; - this.Response.ContentLength = file.ContentLength; - var contentDisposition = new ContentDispositionHeaderValue("attachment") { FileNameStar = fileName }; - this.Response.Headers["Content-Disposition"] = contentDisposition.ToString(); - - // Stream the file in chunks with periodic flushes to keep the connection active. - await this.StreamFileWithKeepAliveAsync(file.Content, this.Response.Body, this.HttpContext.RequestAborted); - return this.Ok(); + return this.File(file.Content, file.ContentType, fileName); } else { @@ -607,20 +584,5 @@ public async Task> GetObsoleteResourceFile(int resourceVersionId, b var result = await this.resourceService.GetObsoleteResourceFile(resourceVersionId, deletedResource); return result; } - - /// - /// Reads from the source stream in chunks and writes to the destination stream, - /// flushing after each chunk to help keep the connection active. - /// - private async Task StreamFileWithKeepAliveAsync(Stream source, Stream destination, CancellationToken cancellationToken) - { - byte[] buffer = new byte[8192]; - int bytesRead; - while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken)) > 0) - { - await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken); - await destination.FlushAsync(cancellationToken); - } - } } } diff --git a/LearningHub.Nhs.WebUI/Services/FileService.cs b/LearningHub.Nhs.WebUI/Services/FileService.cs index fefd834a7..4e02db124 100644 --- a/LearningHub.Nhs.WebUI/Services/FileService.cs +++ b/LearningHub.Nhs.WebUI/Services/FileService.cs @@ -135,36 +135,19 @@ public async Task DownloadFileAsync(string filePath, stri { var file = directory.GetFileClient(fileName); - var properties = await file.GetPropertiesAsync(); - long fileSize = properties.Value.ContentLength; - - try - { - if (fileSize <= 900 * 1024 * 1024) - { - // For smaller files, download the entire file as a stream. - var response = await file.DownloadAsync(); - return new FileDownloadResponse - { - Content = response.Value.Content, - ContentType = properties.Value.ContentType, - ContentLength = fileSize, - }; - } - else + if (await file.ExistsAsync()) { - // For large files, open a read stream - return new FileDownloadResponse - { - Content = await file.OpenReadAsync(), - ContentType = properties.Value.ContentType, - ContentLength = fileSize, - }; + return await file.DownloadAsync(); } } - catch (Exception ex) + else if (await sourceDirectory.ExistsAsync()) { - throw new Exception($"Error downloading file: {ex.Message}"); + var file = sourceDirectory.GetFileClient(fileName); + + if (await file.ExistsAsync()) + { + return await file.DownloadAsync(); + } } return null; From aba7c1073d57cb7837d433e6ca49075b94e3f41c Mon Sep 17 00:00:00 2001 From: Oluwatobi Awe Date: Tue, 11 Feb 2025 10:34:36 +0000 Subject: [PATCH 2/2] revert changes and allow unauthorized request --- LearningHub.Nhs.WebUI/Controllers/Api/ResourceController.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/LearningHub.Nhs.WebUI/Controllers/Api/ResourceController.cs b/LearningHub.Nhs.WebUI/Controllers/Api/ResourceController.cs index 0616474ff..be8895b46 100644 --- a/LearningHub.Nhs.WebUI/Controllers/Api/ResourceController.cs +++ b/LearningHub.Nhs.WebUI/Controllers/Api/ResourceController.cs @@ -60,6 +60,7 @@ public async Task AcceptSensitiveContentAsync(int resourceVersionI /// File name. /// A representing the result of the asynchronous operation. [HttpGet("DownloadResource")] + [AllowAnonymous] public async Task DownloadResource(string filePath, string fileName) { if (string.IsNullOrEmpty(fileName))