From e7ed7e8966395c9b097ed0bf661e772d474a9d34 Mon Sep 17 00:00:00 2001 From: sdcb Date: Fri, 27 Dec 2024 12:54:17 +0800 Subject: [PATCH] add upload default api --- .../Controllers/Chats/Files/FileController.cs | 38 +++++++++++++++---- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/BE/Controllers/Chats/Files/FileController.cs b/src/BE/Controllers/Chats/Files/FileController.cs index 16cdc0ec..c121212d 100644 --- a/src/BE/Controllers/Chats/Files/FileController.cs +++ b/src/BE/Controllers/Chats/Files/FileController.cs @@ -18,18 +18,45 @@ namespace Chats.BE.Controllers.Chats.Files; [Route("api"), Authorize] public class FileController(ChatsDB db, FileServiceFactory fileServiceFactory, IUrlEncryptionService urlEncryption, ILogger logger) : ControllerBase { + [Route("file-service/upload"), HttpPut] + public async Task> DefaultUpload(IFormFile file, + [FromServices] ClientInfoManager clientInfoManager, + [FromServices] FileUrlProvider fdup, + [FromServices] CurrentUser currentUser, + CancellationToken cancellationToken) + { + FileService? fileService = await FileService.GetDefault(db, cancellationToken); + if (fileService == null) + { + return NotFound("File service config not found."); + } + + return await UploadPrivate(db, fileServiceFactory, logger, file, clientInfoManager, fdup, currentUser, fileService, cancellationToken); + } + [Route("file-service/{fileServiceId:int}/upload"), HttpPut] public async Task> Upload(int fileServiceId, IFormFile file, [FromServices] ClientInfoManager clientInfoManager, [FromServices] FileUrlProvider fdup, [FromServices] CurrentUser currentUser, CancellationToken cancellationToken) + { + FileService? fileService = await db.FileServices.FindAsync([fileServiceId], cancellationToken); + if (fileService == null) + { + return NotFound("File server config not found."); + } + + return await UploadPrivate(db, fileServiceFactory, logger, file, clientInfoManager, fdup, currentUser, fileService, cancellationToken); + } + + private async Task> UploadPrivate(ChatsDB db, FileServiceFactory fileServiceFactory, ILogger logger, IFormFile file, ClientInfoManager clientInfoManager, FileUrlProvider fdup, CurrentUser currentUser, FileService fileService, CancellationToken cancellationToken) { if (file.Length == 0) { return BadRequest("File is empty."); } - if (file.Length > 10 * 1024 * 1024) + if (file.Length > 15 * 1024 * 1024) { return BadRequest("File is too large."); } @@ -38,15 +65,10 @@ public async Task> Upload(int fileServiceId, IFormFile fil return BadRequest("Invalid file name."); } - FileService? fileService = await db.FileServices.FindAsync([fileServiceId], cancellationToken); - if (fileService == null) - { - return NotFound("File server config not found."); - } if (!fileService.IsDefault && !currentUser.IsAdmin) { // only admin can upload to non-default file service - return NotFound("File server config not found."); + return NotFound("File service config not found."); } IFileService fs = fileServiceFactory.Create((DBFileServiceType)fileService.FileServiceTypeId, fileService.Configs); @@ -62,7 +84,7 @@ public async Task> Upload(int fileServiceId, IFormFile fil { FileName = file.FileName, FileContentType = await GetOrCreateDBContentType(file.ContentType, cancellationToken), - FileServiceId = fileServiceId, + FileServiceId = fileService.Id, StorageKey = storageKey, Size = (int)file.Length, ClientInfo = await clientInfoManager.GetClientInfo(cancellationToken),