From 696114c9442158505886f47e048eab87618e8ca5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A3=BC=EC=88=98=ED=98=84?= Date: Mon, 23 Jun 2025 20:26:06 +0900 Subject: [PATCH] =?UTF-8?q?=ED=8C=8C=EC=9D=BC=20=EB=8B=A4=EC=9A=B4?= =?UTF-8?q?=EB=A1=9C=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../adapters/DownloadFileController.java | 60 +++++++++++++++++++ .../ports/application/domain/GetFile.java | 2 - 2 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 core/src/main/java/app/module/core/usecase/file/downloadFile/adapters/DownloadFileController.java diff --git a/core/src/main/java/app/module/core/usecase/file/downloadFile/adapters/DownloadFileController.java b/core/src/main/java/app/module/core/usecase/file/downloadFile/adapters/DownloadFileController.java new file mode 100644 index 00000000..68cb3270 --- /dev/null +++ b/core/src/main/java/app/module/core/usecase/file/downloadFile/adapters/DownloadFileController.java @@ -0,0 +1,60 @@ +package app.module.core.usecase.file.downloadFile.adapters; + +import app.module.core.config.application.util.SftpFileDownloader; +import app.module.core.usecase.file.getFile.ports.GetFilePortIn; +import app.module.core.usecase.file.getFile.ports.application.domain.GetFile; +import app.module.core.usecase.file.getFile.ports.application.domain.GetFileCmd; +import com.jcraft.jsch.JSchException; +import com.jcraft.jsch.SftpException; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.core.io.FileSystemResource; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +import java.io.File; +import java.io.IOException; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; + +@Slf4j +@RestController +@RequiredArgsConstructor +class DownloadFileController { + private final GetFilePortIn getFilePortIn; + private final SftpFileDownloader sftpFileDownloader; + + @GetMapping(path = "/api/file/download/{fileId}") + ResponseEntity downloadFile(@PathVariable Long fileId) throws JSchException, SftpException, IOException { + GetFile file = getFilePortIn.operate(new GetFileCmd(fileId)).orElseThrow(() -> new IllegalArgumentException("File not found: " + fileId)); + + // SFTP에서 파일 다운로드 + sftpFileDownloader.downloadFile(file); + + // 로컬 파일 경로 + File downloadedFile = new File(file.getLocalPath()); + if (!downloadedFile.exists()) { + throw new IllegalStateException("Downloaded file not found: " + file.getLocalPath()); + } + + + String encodedFileName = URLEncoder.encode(file.getOriginName(), StandardCharsets.UTF_8) + .replace("+", "%20"); + // HTTP 헤더 설정 + HttpHeaders headers = new HttpHeaders(); + headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename*=UTF-8''" + encodedFileName); + headers.add(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, must-revalidate"); + headers.add(HttpHeaders.PRAGMA, "no-cache"); + headers.add(HttpHeaders.EXPIRES, "0"); + + return ResponseEntity.ok() + .headers(headers) + .contentLength(downloadedFile.length()) + .contentType(MediaType.APPLICATION_OCTET_STREAM) + .body(new FileSystemResource(downloadedFile)); + } +} diff --git a/core/src/main/java/app/module/core/usecase/file/getFile/ports/application/domain/GetFile.java b/core/src/main/java/app/module/core/usecase/file/getFile/ports/application/domain/GetFile.java index 767d7cfd..09b019ab 100644 --- a/core/src/main/java/app/module/core/usecase/file/getFile/ports/application/domain/GetFile.java +++ b/core/src/main/java/app/module/core/usecase/file/getFile/ports/application/domain/GetFile.java @@ -9,8 +9,6 @@ import lombok.Setter; @Getter @Setter -@AllArgsConstructor -@EqualsAndHashCode public class GetFile { private Long id; private FileUploadType type;