using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Swashbuckle.AspNetCore.Annotations; using SPMS.Application.DTOs.File; using SPMS.Application.Interfaces; using SPMS.Domain.Common; using SPMS.Domain.Exceptions; namespace SPMS.API.Controllers; [ApiController] [Route("v1/in/file")] [Authorize] [ApiExplorerSettings(GroupName = "file")] public class FileController : ControllerBase { private readonly IFileService _fileService; public FileController(IFileService fileService) { _fileService = fileService; } [HttpPost("upload")] [SwaggerOperation(Summary = "파일 업로드", Description = "이미지 또는 CSV 파일을 업로드합니다.")] [RequestSizeLimit(52_428_800)] // 50MB public async Task UploadAsync(IFormFile file, [FromForm] string file_type) { var serviceId = GetServiceId(); var adminId = GetAdminId(); using var stream = file.OpenReadStream(); var result = await _fileService.UploadAsync( serviceId, adminId, stream, file.FileName, file.Length, file_type); return Ok(ApiResponse.Success(result)); } [HttpPost("info")] [SwaggerOperation(Summary = "파일 조회", Description = "파일 메타데이터를 조회합니다.")] public async Task GetInfoAsync([FromBody] FileInfoRequestDto request) { var serviceId = GetServiceId(); var result = await _fileService.GetInfoAsync(serviceId, request.FileId); return Ok(ApiResponse.Success(result)); } [HttpPost("list")] [SwaggerOperation(Summary = "파일 목록 조회", Description = "서비스의 파일 목록을 페이징 조회합니다.")] public async Task GetListAsync([FromBody] FileListRequestDto request) { var serviceId = GetServiceId(); var result = await _fileService.GetListAsync(serviceId, request); return Ok(ApiResponse.Success(result)); } [HttpPost("delete")] [SwaggerOperation(Summary = "파일 삭제", Description = "파일을 삭제합니다. (Soft Delete)")] public async Task DeleteAsync([FromBody] FileDeleteRequestDto request) { var serviceId = GetServiceId(); await _fileService.DeleteAsync(serviceId, request.FileId); return Ok(ApiResponse.Success()); } private long GetServiceId() { if (HttpContext.Items.TryGetValue("ServiceId", out var serviceIdObj) && serviceIdObj is long serviceId) return serviceId; throw new SpmsException(ErrorCodes.BadRequest, "서비스 식별 정보가 없습니다.", 400); } private long GetAdminId() { var adminIdClaim = User.FindFirst("adminId")?.Value; if (string.IsNullOrEmpty(adminIdClaim) || !long.TryParse(adminIdClaim, out var adminId)) throw new SpmsException(ErrorCodes.Unauthorized, "인증 정보가 올바르지 않습니다.", 401); return adminId; } }