1+ using FacwareBase . API . Core . File . Management ;
2+ using FacwareBase . API . Helpers . FileManagement ;
3+ using FacwareBase . API . Services . Amazon . S3 . Core . File ;
4+ using FacwareBase . API . Services . Amazon . S3 . Core . Interfaces ;
5+ using Microsoft . AspNetCore . Http ;
6+ using Microsoft . AspNetCore . Mvc ;
7+ using Microsoft . Extensions . Options ;
8+ using System . Collections . Generic ;
9+ using System . Threading . Tasks ;
10+
11+ namespace FacwareBase . API . Controllers . Document
12+ {
13+ [ Route ( "api/file" ) ]
14+ [ ApiController ]
15+ public class FileController : ControllerBase
16+ {
17+ private readonly IFilesRepository _filesRepository ;
18+ private readonly IFileManagement _fileManagement ;
19+ private readonly FileStorageOptions _fileStorageOptions ;
20+
21+ public FileController ( IFilesRepository filesRepository ,
22+ IFileManagement fileManagement ,
23+ IOptions < FileStorageOptions > fileStorageOptions )
24+ {
25+ _filesRepository = filesRepository ;
26+ _fileManagement = fileManagement ;
27+ _fileStorageOptions = fileStorageOptions . Value ;
28+ }
29+
30+ /// <summary>
31+ /// Add new file
32+ /// </summary>
33+ /// <param name="bucketName">bucket name</param>
34+ /// <param name="key">s3 key</param>
35+ /// <param name="formFiles">file</param>
36+ /// <returns></returns>
37+ [ HttpPost ]
38+ [ Route ( "{bucketName}/add/{key}" ) ]
39+ public async Task < ActionResult < AddFileResponse > > AddFiles ( string bucketName , string key , IList < IFormFile > formFiles )
40+ {
41+ if ( formFiles == null )
42+ {
43+ return BadRequest ( "The request doesn't contain any files to be uploaded." ) ;
44+ }
45+
46+ var response = await _filesRepository . UploadFiles ( bucketName , formFiles , key ) ;
47+
48+ if ( response == null )
49+ {
50+ return BadRequest ( ) ;
51+ }
52+
53+ return Ok ( response ) ;
54+ }
55+
56+ /// <summary>
57+ /// List keys in a s3 bucket
58+ /// </summary>
59+ /// <param name="bucketName"></param>
60+ /// <returns></returns>
61+ [ HttpGet ]
62+ [ Route ( "{bucketName}/list" ) ]
63+ public async Task < ActionResult < IEnumerable < ListFilesResponse > > > ListFiles ( string bucketName )
64+ {
65+ var response = await _filesRepository . ListFiles ( bucketName ) ;
66+
67+ return Ok ( response ) ;
68+ }
69+
70+ /// <summary>
71+ /// Download file from s3 bucket
72+ /// </summary>
73+ /// <param name="bucketName"></param>
74+ /// <param name="fileName"></param>
75+ /// <returns></returns>
76+ [ HttpGet ]
77+ [ Route ( "{bucketName}/download/{fileName}" ) ]
78+ public async Task < IActionResult > DownloadFile ( string bucketName , string fileName )
79+ {
80+ var temporalPath = _fileStorageOptions . TemporalStorage ;
81+
82+ await _filesRepository . DownloadFile ( bucketName , fileName , temporalPath ) ;
83+
84+ var memoryFile = _fileManagement . ReadFileAsync ( temporalPath , fileName ) . Result ;
85+
86+ _fileManagement . RemoveFile ( temporalPath , fileName ) ;
87+
88+ var mimeType = _fileManagement . GetMimeType ( fileName ) ;
89+
90+ return File ( memoryFile , mimeType , fileName ) ;
91+
92+ //return Ok();
93+ }
94+
95+ /// <summary>
96+ /// Delete file from s3 bucket
97+ /// </summary>
98+ /// <param name="bucketName">Bucket name</param>
99+ /// <param name="fileName">file name</param>
100+ /// <returns></returns>
101+ [ HttpDelete ]
102+ [ Route ( "{bucketName}/delete/{fileName}" ) ]
103+ public async Task < ActionResult < DeleteFileResponse > > DeleteFile ( string bucketName , string fileName )
104+ {
105+ var response = await _filesRepository . DeleteFile ( bucketName , fileName ) ;
106+
107+ return Ok ( response ) ;
108+ }
109+
110+ /// <summary>
111+ /// Add json object to s3 bucket
112+ /// </summary>
113+ /// <param name="bucketName">bucket</param>
114+ /// <param name="request">json object</param>
115+ /// <returns></returns>
116+ [ HttpPost ]
117+ [ Route ( "{bucketName}/addjsonobject" ) ]
118+ public async Task < IActionResult > AddJsonObject ( string bucketName , AddJsonObjectRequest request )
119+ {
120+ await _filesRepository . AddJsonObject ( bucketName , request ) ;
121+
122+ return Ok ( ) ;
123+ }
124+
125+ /// <summary>
126+ /// Get json object
127+ /// </summary>
128+ /// <param name="bucketName"></param>
129+ /// <param name="fileName"></param>
130+ /// <returns></returns>
131+ [ HttpGet ]
132+ [ Route ( "{bucketName}/getjsonobject" ) ]
133+ public async Task < ActionResult < GetJsonObjectResponse > > GetJsonObject ( string bucketName , string fileName )
134+ {
135+ var response = await _filesRepository . GetJsonObject ( bucketName , fileName ) ;
136+
137+ return Ok ( response ) ;
138+ }
139+ }
140+ }
0 commit comments