Skip to content

Commit f7948d0

Browse files
authored
Merge pull request #977 from TechnologyEnhancedLearning/Develop/Fixes/TD-5348-dev
Revert code changes
2 parents 34dff26 + aba7c10 commit f7948d0

File tree

2 files changed

+11
-65
lines changed

2 files changed

+11
-65
lines changed

LearningHub.Nhs.WebUI/Controllers/Api/ResourceController.cs

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,13 @@ namespace LearningHub.Nhs.WebUI.Controllers.Api
22
{
33
using System;
44
using System.Collections.Generic;
5-
using System.IO;
6-
using System.Linq;
7-
using System.Net.Http.Headers;
8-
using System.Threading;
95
using System.Threading.Tasks;
106
using LearningHub.Nhs.Models.Enums;
117
using LearningHub.Nhs.Models.Resource;
128
using LearningHub.Nhs.Models.Resource.Activity;
139
using LearningHub.Nhs.Models.Resource.Contribute;
1410
using LearningHub.Nhs.WebUI.Interfaces;
1511
using Microsoft.AspNetCore.Authorization;
16-
using Microsoft.AspNetCore.Http;
1712
using Microsoft.AspNetCore.Mvc;
1813
using Microsoft.Extensions.Configuration;
1914

@@ -76,15 +71,7 @@ public async Task<IActionResult> DownloadResource(string filePath, string fileNa
7671
var file = await this.fileService.DownloadFileAsync(filePath, fileName);
7772
if (file != null)
7873
{
79-
// Set response headers.
80-
this.Response.ContentType = file.ContentType;
81-
this.Response.ContentLength = file.ContentLength;
82-
var contentDisposition = new ContentDispositionHeaderValue("attachment") { FileNameStar = fileName };
83-
this.Response.Headers["Content-Disposition"] = contentDisposition.ToString();
84-
85-
// Stream the file in chunks with periodic flushes to keep the connection active.
86-
await this.StreamFileWithKeepAliveAsync(file.Content, this.Response.Body, this.HttpContext.RequestAborted);
87-
return this.Ok();
74+
return this.File(file.Content, file.ContentType, fileName);
8875
}
8976
else
9077
{
@@ -119,16 +106,7 @@ public async Task<IActionResult> DownloadResourceAndRecordActivity(int resourceV
119106
ActivityStatus = ActivityStatusEnum.Completed,
120107
};
121108
await this.activityService.CreateResourceActivityAsync(activity);
122-
123-
// Set response headers.
124-
this.Response.ContentType = file.ContentType;
125-
this.Response.ContentLength = file.ContentLength;
126-
var contentDisposition = new ContentDispositionHeaderValue("attachment") { FileNameStar = fileName };
127-
this.Response.Headers["Content-Disposition"] = contentDisposition.ToString();
128-
129-
// Stream the file in chunks with periodic flushes to keep the connection active.
130-
await this.StreamFileWithKeepAliveAsync(file.Content, this.Response.Body, this.HttpContext.RequestAborted);
131-
return this.Ok();
109+
return this.File(file.Content, file.ContentType, fileName);
132110
}
133111
else
134112
{
@@ -607,20 +585,5 @@ public async Task<List<string>> GetObsoleteResourceFile(int resourceVersionId, b
607585
var result = await this.resourceService.GetObsoleteResourceFile(resourceVersionId, deletedResource);
608586
return result;
609587
}
610-
611-
/// <summary>
612-
/// Reads from the source stream in chunks and writes to the destination stream,
613-
/// flushing after each chunk to help keep the connection active.
614-
/// </summary>
615-
private async Task StreamFileWithKeepAliveAsync(Stream source, Stream destination, CancellationToken cancellationToken)
616-
{
617-
byte[] buffer = new byte[8192];
618-
int bytesRead;
619-
while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken)) > 0)
620-
{
621-
await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken);
622-
await destination.FlushAsync(cancellationToken);
623-
}
624-
}
625588
}
626589
}

LearningHub.Nhs.WebUI/Services/FileService.cs

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -135,36 +135,19 @@ public async Task<ShareFileDownloadInfo> DownloadFileAsync(string filePath, stri
135135
{
136136
var file = directory.GetFileClient(fileName);
137137

138-
var properties = await file.GetPropertiesAsync();
139-
long fileSize = properties.Value.ContentLength;
140-
141-
try
142-
{
143-
if (fileSize <= 900 * 1024 * 1024)
144-
{
145-
// For smaller files, download the entire file as a stream.
146-
var response = await file.DownloadAsync();
147-
return new FileDownloadResponse
148-
{
149-
Content = response.Value.Content,
150-
ContentType = properties.Value.ContentType,
151-
ContentLength = fileSize,
152-
};
153-
}
154-
else
138+
if (await file.ExistsAsync())
155139
{
156-
// For large files, open a read stream
157-
return new FileDownloadResponse
158-
{
159-
Content = await file.OpenReadAsync(),
160-
ContentType = properties.Value.ContentType,
161-
ContentLength = fileSize,
162-
};
140+
return await file.DownloadAsync();
163141
}
164142
}
165-
catch (Exception ex)
143+
else if (await sourceDirectory.ExistsAsync())
166144
{
167-
throw new Exception($"Error downloading file: {ex.Message}");
145+
var file = sourceDirectory.GetFileClient(fileName);
146+
147+
if (await file.ExistsAsync())
148+
{
149+
return await file.DownloadAsync();
150+
}
168151
}
169152

170153
return null;

0 commit comments

Comments
 (0)