11namespace 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