1414
1515namespace ManagedCode . Storage . Server . Extensions ;
1616
17- public static class ControllerBaseExtensions
17+ public static class ControllerUploadExtensions
1818{
1919 private const int DefaultMultipartBoundaryLengthLimit = 70 ;
20+ private const int MinLengthForLargeFile = 256 * 1024 ;
2021
21- public static async Task < Result < BlobMetadata > > UploadFromFormFileAsync (
22+ public static async Task < Result < BlobMetadata > > UploadFormFileAsync (
2223 this ControllerBase controller ,
2324 IStorage storage ,
2425 IFormFile file ,
2526 UploadOptions ? options = null ,
2627 CancellationToken cancellationToken = default )
2728 {
28- if ( options == null )
29+ options ??= new UploadOptions ( file . FileName , mimeType : file . ContentType ) ;
30+
31+ if ( file . Length > MinLengthForLargeFile )
2932 {
30- options = new UploadOptions
31- {
32- FileName = file . FileName ,
33- MimeType = file . ContentType
34- } ;
33+ var localFile = await file . ToLocalFileAsync ( cancellationToken ) ;
34+ return await storage . UploadAsync ( localFile . FileInfo , options , cancellationToken ) ;
3535 }
36-
37- return await storage . UploadToStorageAsync ( file , options , cancellationToken ) ;
36+
37+ await using var stream = file . OpenReadStream ( ) ;
38+ return await storage . UploadAsync ( stream , options , cancellationToken ) ;
3839 }
3940
4041 public static async Task < Result < BlobMetadata > > UploadFromBrowserFileAsync (
@@ -44,16 +45,16 @@ public static async Task<Result<BlobMetadata>> UploadFromBrowserFileAsync(
4445 UploadOptions ? options = null ,
4546 CancellationToken cancellationToken = default )
4647 {
47- if ( options == null )
48+ options ??= new UploadOptions ( file . Name , mimeType : file . ContentType ) ;
49+
50+ if ( file . Size > MinLengthForLargeFile )
4851 {
49- options = new UploadOptions
50- {
51- FileName = file . Name ,
52- MimeType = file . ContentType
53- } ;
52+ var localFile = await file . ToLocalFileAsync ( cancellationToken ) ;
53+ return await storage . UploadAsync ( localFile . FileInfo , options , cancellationToken ) ;
5454 }
55-
56- return await storage . UploadToStorageAsync ( file , options ) ;
55+
56+ await using var stream = file . OpenReadStream ( ) ;
57+ return await storage . UploadAsync ( stream , options , cancellationToken ) ;
5758 }
5859
5960 public static async Task < Result < BlobMetadata > > UploadFromStreamAsync (
@@ -83,14 +84,7 @@ public static async Task<Result<BlobMetadata>> UploadFromStreamAsync(
8384 var fileName = contentDisposition . FileName . Value ;
8485 var contentType = section . ContentType ;
8586
86- if ( options == null )
87- {
88- options = new UploadOptions
89- {
90- FileName = fileName ,
91- MimeType = contentType
92- } ;
93- }
87+ options ??= new UploadOptions ( fileName , mimeType : contentType ) ;
9488
9589 using var memoryStream = new MemoryStream ( ) ;
9690 await section . Body . CopyToAsync ( memoryStream , cancellationToken ) ;
@@ -104,13 +98,4 @@ public static async Task<Result<BlobMetadata>> UploadFromStreamAsync(
10498
10599 return Result < BlobMetadata > . Fail ( HttpStatusCode . BadRequest , "No file found in request" ) ;
106100 }
107-
108- public static async Task < Result < FileResult > > DownloadAsFileResultAsync (
109- this ControllerBase controller ,
110- IStorage storage ,
111- string blobName ,
112- CancellationToken cancellationToken = default )
113- {
114- return await storage . DownloadAsFileResult ( blobName , cancellationToken ) ;
115- }
116101}
0 commit comments