1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Runtime . CompilerServices ;
4+ using System . Threading ;
5+ using System . Threading . Tasks ;
6+ using ManagedCode . Communication ;
7+ using ManagedCode . Storage . Core ;
8+ using ManagedCode . Storage . Core . Models ;
9+ using Microsoft . AspNetCore . Components . Forms ;
10+ using Microsoft . AspNetCore . Http ;
11+ using Microsoft . AspNetCore . Mvc ;
12+
13+ namespace ManagedCode . Storage . AspNetExtensions ;
14+
15+ public class BaseController : Controller
16+ {
17+ private readonly IStorage _storage ;
18+
19+ public BaseController ( IStorage storage )
20+ {
21+ _storage = storage ;
22+ }
23+
24+ protected async Task < Result < BlobMetadata > > UploadToStorageAsync ( IBrowserFile formFile ,
25+ UploadOptions ? options = null )
26+ {
27+ return await _storage . UploadToStorageAsync ( formFile , options ) ;
28+ }
29+
30+ protected async Task < Result < BlobMetadata > > UploadToStorageAsync ( IBrowserFile formFile ,
31+ Action < UploadOptions > options )
32+ {
33+ return await _storage . UploadToStorageAsync ( formFile , options ) ;
34+ }
35+
36+ protected async Task < Result < FileResult > > DownloadAsFileResult ( string blobName , CancellationToken cancellationToken = default )
37+ {
38+ return await _storage . DownloadAsFileResult ( blobName , cancellationToken ) ;
39+ }
40+ protected async Task < Result < FileResult > > DownloadAsFileResult ( BlobMetadata blobMetadata , CancellationToken cancellationToken = default )
41+ {
42+ return await _storage . DownloadAsFileResult ( blobMetadata , cancellationToken ) ;
43+ }
44+
45+ protected async Task < Result < BlobMetadata > > UploadToStorageAsync ( IFormFile formFile ,
46+ UploadOptions ? options = null ,
47+ CancellationToken cancellationToken = default )
48+ {
49+ return await _storage . UploadToStorageAsync ( formFile , options , cancellationToken ) ;
50+ }
51+
52+ protected async Task < Result < BlobMetadata > > UploadToStorageAsync ( IFormFile formFile ,
53+ Action < UploadOptions > options ,
54+ CancellationToken cancellationToken = default )
55+ {
56+ return await _storage . UploadToStorageAsync ( formFile , options , cancellationToken ) ;
57+ }
58+ protected async IAsyncEnumerable < Result < BlobMetadata > > UploadToStorageAsync ( IFormFileCollection formFiles ,
59+ UploadOptions ? options = null ,
60+ [ EnumeratorCancellation ] CancellationToken cancellationToken = default )
61+ {
62+ foreach ( var formFile in formFiles )
63+ {
64+ yield return await _storage . UploadToStorageAsync ( formFile , options , cancellationToken ) ;
65+ }
66+ }
67+ protected async IAsyncEnumerable < Result < BlobMetadata > > UploadToStorageAsync ( IFormFileCollection formFiles ,
68+ Action < UploadOptions > options ,
69+ [ EnumeratorCancellation ] CancellationToken cancellationToken = default )
70+ {
71+ foreach ( var formFile in formFiles )
72+ {
73+ yield return await _storage . UploadToStorageAsync ( formFile , options , cancellationToken ) ;
74+ }
75+ }
76+
77+ }
0 commit comments