Skip to content

Commit

Permalink
add upload default api
Browse files Browse the repository at this point in the history
  • Loading branch information
sdcb committed Dec 27, 2024
1 parent 1edb477 commit e7ed7e8
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions src/BE/Controllers/Chats/Files/FileController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,45 @@ namespace Chats.BE.Controllers.Chats.Files;
[Route("api"), Authorize]
public class FileController(ChatsDB db, FileServiceFactory fileServiceFactory, IUrlEncryptionService urlEncryption, ILogger<FileController> logger) : ControllerBase
{
[Route("file-service/upload"), HttpPut]
public async Task<ActionResult<FileDto>> 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<ActionResult<FileDto>> 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<ActionResult<FileDto>> UploadPrivate(ChatsDB db, FileServiceFactory fileServiceFactory, ILogger<FileController> 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.");
}
Expand All @@ -38,15 +65,10 @@ public async Task<ActionResult<FileDto>> 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);
Expand All @@ -62,7 +84,7 @@ public async Task<ActionResult<FileDto>> 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),
Expand Down

0 comments on commit e7ed7e8

Please sign in to comment.