1- using System ;
21using System . Collections . Generic ;
3- using System . IO ;
42using System . Runtime . CompilerServices ;
53using System . Threading ;
64using System . Threading . Tasks ;
5+ using ManagedCode . Communication ;
76using ManagedCode . MimeTypes ;
8- using ManagedCode . Storage . AspNetExtensions . Options ;
97using ManagedCode . Storage . Core ;
108using ManagedCode . Storage . Core . Models ;
119using Microsoft . AspNetCore . Http ;
@@ -17,42 +15,25 @@ public static class StorageExtensions
1715{
1816 private const int MinLengthForLargeFile = 256 * 1024 ;
1917
20- public static async Task < BlobMetadata > UploadToStorageAsync ( this IStorage storage , IFormFile formFile , UploadToStorageOptions ? options = null ,
18+ public static async Task < Result < BlobMetadata > > UploadToStorageAsync ( this IStorage storage , IFormFile formFile , UploadOptions ? options = null ,
2119 CancellationToken cancellationToken = default )
2220 {
23- options ??= new UploadToStorageOptions ( ) ;
24-
25- var extension = Path . GetExtension ( formFile . FileName ) ;
26-
27- BlobMetadata blobMetadata = new ( )
28- {
29- Name = options . UseRandomName ? "" : formFile . FileName ,
30- MimeType = formFile . ContentType ,
31- } ;
21+ options ??= new UploadOptions ( fileName : formFile . FileName , mimeType : formFile . ContentType ) ;
3222
3323 if ( formFile . Length > MinLengthForLargeFile )
3424 {
3525 var localFile = await formFile . ToLocalFileAsync ( cancellationToken ) ;
36- await storage . UploadAsync ( localFile . FileInfo , cancellationToken ) ;
26+ return await storage . UploadAsync ( localFile . FileInfo , options , cancellationToken ) ;
3727 }
38- else
39- {
40- using ( var stream = formFile . OpenReadStream ( ) )
41- {
42- await storage . UploadAsync ( stream , uploadOptions =>
43- {
44- uploadOptions . FileName = options . UseRandomName ? "" : formFile . FileName ;
45- uploadOptions . MimeType = formFile . ContentType ;
4628
47- } , cancellationToken ) ;
48- }
29+ using ( var stream = formFile . OpenReadStream ( ) )
30+ {
31+ return await storage . UploadAsync ( stream , options , cancellationToken ) ;
4932 }
50-
51- return blobMetadata ;
5233 }
5334
54- public static async IAsyncEnumerable < BlobMetadata > UploadToStorageAsync ( this IStorage storage , IFormFileCollection formFiles ,
55- UploadToStorageOptions ? options = null ,
35+ public static async IAsyncEnumerable < Result < BlobMetadata > > UploadToStorageAsync ( this IStorage storage , IFormFileCollection formFiles ,
36+ UploadOptions ? options = null ,
5637 [ EnumeratorCancellation ] CancellationToken cancellationToken = default )
5738 {
5839 foreach ( var formFile in formFiles )
@@ -61,34 +42,39 @@ public static async IAsyncEnumerable<BlobMetadata> UploadToStorageAsync(this ISt
6142 }
6243 }
6344
64- public static async Task < FileResult ? > DownloadAsFileResult ( this IStorage storage , string blobName , CancellationToken cancellationToken = default )
45+ public static async Task < Result < FileResult > > DownloadAsFileResult ( this IStorage storage , string blobName ,
46+ CancellationToken cancellationToken = default )
6547 {
66- var localFile = await storage . DownloadAsync ( blobName , cancellationToken ) ;
48+ var result = await storage . DownloadAsync ( blobName , cancellationToken ) ;
6749
68- if ( localFile is null )
50+ if ( result . IsError )
6951 {
70- return null ;
52+ return Result < FileResult > . Fail ( result . Error ) ;
7153 }
7254
73- return new FileStreamResult ( localFile . Value . FileStream , MimeHelper . GetMimeType ( localFile . Value . FileInfo . Extension ) )
55+ var fileStream = new FileStreamResult ( result . Value ! . FileStream , MimeHelper . GetMimeType ( result . Value . FileInfo . Extension ) )
7456 {
75- FileDownloadName = localFile . Value . FileName
57+ FileDownloadName = result . Value . FileName
7658 } ;
59+
60+ return Result < FileResult > . Succeed ( fileStream ) ;
7761 }
7862
79- public static async Task < FileResult ? > DownloadAsFileResult ( this IStorage storage , BlobMetadata blobMetadata ,
63+ public static async Task < Result < FileResult > > DownloadAsFileResult ( this IStorage storage , BlobMetadata blobMetadata ,
8064 CancellationToken cancellationToken = default )
8165 {
82- var localFile = await storage . DownloadAsync ( blobMetadata . Name , cancellationToken ) ;
66+ var result = await storage . DownloadAsync ( blobMetadata . Name , cancellationToken ) ;
8367
84- if ( localFile is null )
68+ if ( result . IsError )
8569 {
86- return null ;
70+ return Result . Fail < FileResult > ( result . Error ) ;
8771 }
8872
89- return new FileStreamResult ( localFile . Value . FileStream , MimeHelper . GetMimeType ( localFile . Value . FileInfo . Extension ) )
73+ var fileStream = new FileStreamResult ( result . Value ! . FileStream , MimeHelper . GetMimeType ( result . Value . FileInfo . Extension ) )
9074 {
91- FileDownloadName = localFile . Value . FileName
75+ FileDownloadName = result . Value . FileName
9276 } ;
77+
78+ return Result < FileResult > . Succeed ( fileStream ) ;
9379 }
9480}
0 commit comments