1
- using System ;
2
1
using System . Collections . Generic ;
3
- using System . IO ;
4
2
using System . Runtime . CompilerServices ;
5
3
using System . Threading ;
6
4
using System . Threading . Tasks ;
5
+ using ManagedCode . Communication ;
7
6
using ManagedCode . MimeTypes ;
8
- using ManagedCode . Storage . AspNetExtensions . Options ;
9
7
using ManagedCode . Storage . Core ;
10
8
using ManagedCode . Storage . Core . Models ;
11
9
using Microsoft . AspNetCore . Http ;
@@ -17,42 +15,25 @@ public static class StorageExtensions
17
15
{
18
16
private const int MinLengthForLargeFile = 256 * 1024 ;
19
17
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 ,
21
19
CancellationToken cancellationToken = default )
22
20
{
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 ) ;
32
22
33
23
if ( formFile . Length > MinLengthForLargeFile )
34
24
{
35
25
var localFile = await formFile . ToLocalFileAsync ( cancellationToken ) ;
36
- await storage . UploadAsync ( localFile . FileInfo , cancellationToken ) ;
26
+ return await storage . UploadAsync ( localFile . FileInfo , options , cancellationToken ) ;
37
27
}
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 ;
46
28
47
- } , cancellationToken ) ;
48
- }
29
+ using ( var stream = formFile . OpenReadStream ( ) )
30
+ {
31
+ return await storage . UploadAsync ( stream , options , cancellationToken ) ;
49
32
}
50
-
51
- return blobMetadata ;
52
33
}
53
34
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 ,
56
37
[ EnumeratorCancellation ] CancellationToken cancellationToken = default )
57
38
{
58
39
foreach ( var formFile in formFiles )
@@ -61,34 +42,39 @@ public static async IAsyncEnumerable<BlobMetadata> UploadToStorageAsync(this ISt
61
42
}
62
43
}
63
44
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 )
65
47
{
66
- var localFile = await storage . DownloadAsync ( blobName , cancellationToken ) ;
48
+ var result = await storage . DownloadAsync ( blobName , cancellationToken ) ;
67
49
68
- if ( localFile is null )
50
+ if ( result . IsError )
69
51
{
70
- return null ;
52
+ return Result < FileResult > . Fail ( result . Error ) ;
71
53
}
72
54
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 ) )
74
56
{
75
- FileDownloadName = localFile . Value . FileName
57
+ FileDownloadName = result . Value . FileName
76
58
} ;
59
+
60
+ return Result < FileResult > . Succeed ( fileStream ) ;
77
61
}
78
62
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 ,
80
64
CancellationToken cancellationToken = default )
81
65
{
82
- var localFile = await storage . DownloadAsync ( blobMetadata . Name , cancellationToken ) ;
66
+ var result = await storage . DownloadAsync ( blobMetadata . Name , cancellationToken ) ;
83
67
84
- if ( localFile is null )
68
+ if ( result . IsError )
85
69
{
86
- return null ;
70
+ return Result . Fail < FileResult > ( result . Error ) ;
87
71
}
88
72
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 ) )
90
74
{
91
- FileDownloadName = localFile . Value . FileName
75
+ FileDownloadName = result . Value . FileName
92
76
} ;
77
+
78
+ return Result < FileResult > . Succeed ( fileStream ) ;
93
79
}
94
80
}
0 commit comments