1+ using System . Globalization ;
2+ using System . Text ;
3+ using System . Text . RegularExpressions ;
14using Microsoft . AspNetCore . Mvc ;
25using PaymentCoreServiceApi . Services ;
36using PaymentCoreServiceApi . Common ;
@@ -25,7 +28,7 @@ public FileController(IMinIOService minIOService, ILogger<FileController> logger
2528 /// <returns>Tên file đã được lưu</returns>
2629 [ HttpPost ( "upload" ) ]
2730 public async Task < ActionResult < ApiResponse < FileUploadResponse > > > UploadFile (
28- IFormFile file ,
31+ IFormFile file ,
2932 [ FromForm ] string ? customFileName = null )
3033 {
3134 try
@@ -83,12 +86,15 @@ public async Task<ActionResult<ApiResponse<List<FileUploadResponse>>>> UploadMul
8386 {
8487 if ( file . Length > 0 )
8588 {
86- var fileName = await _minIOService . UploadFileAsync ( file ) ;
87- var fileUrl = await _minIOService . GetFileUrlAsync ( fileName ) ;
8889
90+ var safeFileName = SanitizeFileName ( file . FileName ) ;
91+ var extension = Path . GetExtension ( safeFileName ) ;
92+ var uniqueFileName = $ "{ Guid . NewGuid ( ) } { extension } ";
93+ var uploadedFileName = await _minIOService . UploadFileAsync ( file , uniqueFileName ) ;
94+ var fileUrl = await _minIOService . GetFileUrlAsync ( uploadedFileName ) ;
8995 responses . Add ( new FileUploadResponse
9096 {
91- FileName = fileName ,
97+ FileName = uploadedFileName ,
9298 OriginalFileName = file . FileName ,
9399 FileSize = file . Length ,
94100 ContentType = file . ContentType ,
@@ -108,8 +114,8 @@ public async Task<ActionResult<ApiResponse<List<FileUploadResponse>>>> UploadMul
108114 _logger . LogWarning ( $ "Một số file upload thất bại: { string . Join ( ", " , errors ) } ") ;
109115 }
110116
111- var message = errors . Any ( )
112- ? $ "Upload hoàn thành với { errors . Count } lỗi"
117+ var message = errors . Any ( )
118+ ? $ "Upload hoàn thành với { errors . Count } lỗi"
113119 : "Upload tất cả file thành công" ;
114120
115121 return Ok ( ApiResponse < List < FileUploadResponse > > . Success ( responses , message ) ) ;
@@ -121,6 +127,26 @@ public async Task<ActionResult<ApiResponse<List<FileUploadResponse>>>> UploadMul
121127 }
122128 }
123129
130+ private string SanitizeFileName ( string fileName )
131+ {
132+ // Lấy phần tên gốc
133+ fileName = Path . GetFileName ( fileName ) ;
134+
135+ // Loại bỏ dấu tiếng Việt
136+ fileName = fileName . Normalize ( NormalizationForm . FormD ) ;
137+ var chars = fileName
138+ . Where ( c => CharUnicodeInfo . GetUnicodeCategory ( c ) != UnicodeCategory . NonSpacingMark )
139+ . ToArray ( ) ;
140+ fileName = new string ( chars ) ;
141+
142+ // Thay thế ký tự không hợp lệ thành "_"
143+ fileName = Regex . Replace ( fileName , @"[^a-zA-Z0-9_.-]" , "_" ) ;
144+
145+ // Giữ nguyên đuôi file
146+ return fileName ;
147+
148+ }
149+
124150 /// <summary>
125151 /// Download file từ MinIO
126152 /// </summary>
@@ -133,7 +159,7 @@ public async Task<IActionResult> DownloadFile(string fileName)
133159 {
134160 var fileStream = await _minIOService . DownloadFileAsync ( fileName ) ;
135161 var contentType = GetContentType ( fileName ) ;
136-
162+
137163 _logger . LogInformation ( $ "File '{ fileName } ' đã được download thành công") ;
138164 return File ( fileStream , contentType , fileName ) ;
139165 }
@@ -152,13 +178,13 @@ public async Task<IActionResult> DownloadFile(string fileName)
152178 /// <returns>URL tạm thời</returns>
153179 [ HttpGet ( "url/{fileName}" ) ]
154180 public async Task < ActionResult < ApiResponse < FileUrlResponse > > > GetFileUrl (
155- string fileName ,
181+ string fileName ,
156182 [ FromQuery ] int expiryInSeconds = 3600 )
157183 {
158184 try
159185 {
160186 var url = await _minIOService . GetFileUrlAsync ( fileName , expiryInSeconds ) ;
161-
187+
162188 var response = new FileUrlResponse
163189 {
164190 FileName = fileName ,
@@ -187,7 +213,7 @@ public async Task<ActionResult<ApiResponse<object>>> DeleteFile(string fileName)
187213 try
188214 {
189215 var result = await _minIOService . DeleteFileAsync ( fileName ) ;
190-
216+
191217 if ( result )
192218 {
193219 _logger . LogInformation ( $ "File '{ fileName } ' đã được xóa thành công") ;
@@ -241,4 +267,4 @@ public class FileUrlResponse
241267 public int ExpiryInSeconds { get ; set ; }
242268 public DateTime ExpiresAt { get ; set ; }
243269 }
244- }
270+ }
0 commit comments