Skip to content

Commit a4f1eca

Browse files
authored
Merge pull request #991 from TechnologyEnhancedLearning/Develop/Fixes/TD-5348-dahila
TD-5348 LH resource download files error
2 parents e45cd2b + 242d65e commit a4f1eca

File tree

3 files changed

+38
-24
lines changed

3 files changed

+38
-24
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ public async Task<IActionResult> DownloadResource(string filePath, string fileNa
6969
}
7070

7171
var file = await this.fileService.DownloadFileAsync(filePath, fileName);
72+
7273
if (file != null)
7374
{
74-
return this.File(file.Content, file.ContentType, fileName);
75+
return !string.IsNullOrEmpty(file.DownloadUrl) ? this.Redirect(file.DownloadUrl) : this.File(file.Content, file.ContentType, fileName);
7576
}
7677
else
7778
{
@@ -106,7 +107,8 @@ public async Task<IActionResult> DownloadResourceAndRecordActivity(int resourceV
106107
ActivityStatus = ActivityStatusEnum.Completed,
107108
};
108109
await this.activityService.CreateResourceActivityAsync(activity);
109-
return this.File(file.Content, file.ContentType, fileName);
110+
111+
return !string.IsNullOrEmpty(file.DownloadUrl) ? this.Redirect(file.DownloadUrl) : this.File(file.Content, file.ContentType, fileName);
110112
}
111113
else
112114
{

LearningHub.Nhs.WebUI/Models/FileDownloadResponse.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,10 @@ public class FileDownloadResponse
2121
/// Gets or sets the ContentType.
2222
/// </summary>
2323
public long ContentLength { get; set; }
24+
25+
/// <summary>
26+
/// Gets or sets when downloading large files, a SAS URL is returned so the client can download directly from Azure Files.
27+
/// </summary>
28+
public string DownloadUrl { get; set; }
2429
}
2530
}

LearningHub.Nhs.WebUI/Services/FileService.cs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
namespace LearningHub.Nhs.WebUI.Services
22
{
33
using System;
4-
using System.Buffers;
54
using System.Collections.Generic;
65
using System.IO;
76
using System.Linq;
8-
using System.Threading;
97
using System.Threading.Tasks;
10-
using System.Threading.Tasks.Dataflow;
11-
using Azure;
12-
using Azure.Storage;
138
using Azure.Storage.Files.Shares;
149
using Azure.Storage.Files.Shares.Models;
10+
using Azure.Storage.Sas;
1511
using LearningHub.Nhs.Models.Resource;
1612
using LearningHub.Nhs.WebUI.Configuration;
1713
using LearningHub.Nhs.WebUI.Interfaces;
@@ -145,26 +141,19 @@ public async Task<FileDownloadResponse> DownloadFileAsync(string filePath, strin
145141

146142
try
147143
{
148-
if (fileSize <= 900 * 1024 * 1024)
144+
var response = new FileDownloadResponse
149145
{
150-
// Directly download the entire file as a stream
151-
var response = await file.DownloadAsync();
152-
return new FileDownloadResponse
153-
{
154-
Content = response.Value.Content,
155-
ContentType = properties.Value.ContentType,
156-
ContentLength = fileSize,
157-
};
158-
}
159-
else
146+
ContentType = properties.Value.ContentType,
147+
ContentLength = fileSize,
148+
Content = await file.OpenReadAsync(),
149+
};
150+
151+
if (fileSize >= 999 * 1024 * 1024)
160152
{
161-
return new FileDownloadResponse
162-
{
163-
Content = await file.OpenReadAsync(),
164-
ContentType = properties.Value.ContentType,
165-
ContentLength = fileSize,
166-
};
153+
response.DownloadUrl = this.GenerateSasUriForFile(file);
167154
}
155+
156+
return response;
168157
}
169158
catch (Exception ex)
170159
{
@@ -460,5 +449,23 @@ private async Task<ShareFileClient> FindFileAsync(string filePath, string fileNa
460449

461450
return null;
462451
}
452+
453+
private string GenerateSasUriForFile(ShareFileClient fileClient)
454+
{
455+
if (fileClient.CanGenerateSasUri)
456+
{
457+
ShareSasBuilder sasBuilder = new ShareSasBuilder(ShareFileSasPermissions.Read, DateTimeOffset.UtcNow.AddMinutes(20))
458+
{
459+
Protocol = SasProtocol.Https,
460+
};
461+
462+
Uri sasUri = fileClient.GenerateSasUri(sasBuilder);
463+
return sasUri.ToString();
464+
}
465+
else
466+
{
467+
throw new InvalidOperationException("Unable to generate SAS URI for the file.");
468+
}
469+
}
463470
}
464471
}

0 commit comments

Comments
 (0)