파일 다운로드 추가
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,6 @@ import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode
|
||||
public class GetFile {
|
||||
private Long id;
|
||||
private FileUploadType type;
|
||||
|
||||
Reference in New Issue
Block a user