|
|
|
|
@@ -1,27 +1,42 @@
|
|
|
|
|
package app.module.core.usecase.getAttachment.adapters.web;
|
|
|
|
|
|
|
|
|
|
import app.module.core.config.application.enums.FileStorageType;
|
|
|
|
|
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 java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
|
|
|
|
|
|
import com.jcraft.jsch.JSchException;
|
|
|
|
|
import com.jcraft.jsch.SftpException;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.core.io.FileSystemResource;
|
|
|
|
|
import org.springframework.core.io.UrlResource;
|
|
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
|
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 org.springframework.web.util.UriUtils;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
@RestController
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
class GetAttachmentController {
|
|
|
|
|
|
|
|
|
|
private final GetFilePortIn getFilePortIn;
|
|
|
|
|
private final SftpFileDownloader sftpFileDownloader;
|
|
|
|
|
|
|
|
|
|
@Value("${path.upload.public}")
|
|
|
|
|
private String publicPath;
|
|
|
|
|
@Value("${app.aws.s3.domain:null}")
|
|
|
|
|
@@ -34,11 +49,14 @@ class GetAttachmentController {
|
|
|
|
|
@GetMapping(path = "/content-disposition/attachment/{fileId}")
|
|
|
|
|
ResponseEntity<?> operate(@PathVariable Long fileId) throws MalformedURLException {
|
|
|
|
|
|
|
|
|
|
GetFile getFile =
|
|
|
|
|
getFilePortIn
|
|
|
|
|
.operate(new GetFileCmd(fileId))
|
|
|
|
|
.orElseThrow(
|
|
|
|
|
() -> new RuntimeException(String.format("파일을 찾을 수 없습니다. ID: %s", fileId)));
|
|
|
|
|
GetFile getFile;
|
|
|
|
|
try {
|
|
|
|
|
getFile = getFilePortIn.operate(new GetFileCmd(fileId))
|
|
|
|
|
.orElseThrow(() -> new RuntimeException(String.format("파일을 찾을 수 없습니다. ID: %s", fileId)));
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
return ResponseEntity.status(HttpStatus.NOT_FOUND)
|
|
|
|
|
.body("파일을 찾을 수 없습니다: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO - private 업로드 기능을 일단 mute
|
|
|
|
|
|
|
|
|
|
@@ -56,35 +74,74 @@ class GetAttachmentController {
|
|
|
|
|
// () -> new AccessDeniedException("[AccessDenied] Private 파일 리소스 접근 권한이 없습니다."));
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// HTTP 헤더 설정
|
|
|
|
|
String encodedFileName = URLEncoder.encode(getFile.getOriginName(), StandardCharsets.UTF_8)
|
|
|
|
|
.replace("+", "%20");
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
|
|
UrlResource resource = null;
|
|
|
|
|
|
|
|
|
|
switch (FileStorageType.valueOf(uploadStorageType)) {
|
|
|
|
|
case LOCAL:
|
|
|
|
|
String filePath =
|
|
|
|
|
Paths.get(publicPath, getFile.getPath(), getFile.getName())
|
|
|
|
|
.toString();
|
|
|
|
|
String filePath = Paths.get(publicPath, getFile.getPath(), getFile.getName()).toString();
|
|
|
|
|
File localFile = new File(filePath);
|
|
|
|
|
if (!localFile.exists()) {
|
|
|
|
|
return ResponseEntity.status(HttpStatus.NOT_FOUND)
|
|
|
|
|
.body("파일을 찾을 수 없습니다: ");
|
|
|
|
|
}
|
|
|
|
|
return ResponseEntity.ok()
|
|
|
|
|
.headers(headers)
|
|
|
|
|
.contentLength(localFile.length())
|
|
|
|
|
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
|
|
|
|
.body(new FileSystemResource(localFile));
|
|
|
|
|
|
|
|
|
|
resource = new UrlResource("file:" + filePath);
|
|
|
|
|
break;
|
|
|
|
|
case S3:
|
|
|
|
|
resource =
|
|
|
|
|
new UrlResource(
|
|
|
|
|
"https://"
|
|
|
|
|
+ cloudFrontDomain
|
|
|
|
|
+ bucketFolder
|
|
|
|
|
+ getFile.getPath()
|
|
|
|
|
+ "/"
|
|
|
|
|
+ getFile.getName());
|
|
|
|
|
break;
|
|
|
|
|
String s3Url = "https://" + cloudFrontDomain + bucketFolder + getFile.getPath() + "/" + getFile.getName();
|
|
|
|
|
UrlResource s3Resource = new UrlResource(s3Url);
|
|
|
|
|
if (!s3Resource.exists()) {
|
|
|
|
|
return ResponseEntity.status(HttpStatus.NOT_FOUND)
|
|
|
|
|
.body("S3 파일을 찾을 수 없습니다: " + s3Url);
|
|
|
|
|
}
|
|
|
|
|
return ResponseEntity.ok()
|
|
|
|
|
.headers(headers)
|
|
|
|
|
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
|
|
|
|
.body(s3Resource);
|
|
|
|
|
|
|
|
|
|
case SFTP:
|
|
|
|
|
try {
|
|
|
|
|
sftpFileDownloader.downloadFile(getFile);
|
|
|
|
|
} catch (JSchException | SftpException | IOException e) {
|
|
|
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
|
|
.body("SFTP 파일 다운로드에 실패했습니다: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
File downloadedFile = new File(getFile.getLocalPath());
|
|
|
|
|
if (!downloadedFile.exists()) {
|
|
|
|
|
return ResponseEntity.status(HttpStatus.NOT_FOUND)
|
|
|
|
|
.body("다운로드된 파일을 찾을 수 없습니다: " + getFile.getLocalPath());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok()
|
|
|
|
|
.headers(headers)
|
|
|
|
|
.contentLength(downloadedFile.length())
|
|
|
|
|
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
|
|
|
|
.body(new FileSystemResource(downloadedFile));
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
|
|
|
|
|
.body("지원되지 않는 저장소 타입입니다: " + uploadStorageType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String encodedUploadFileName = UriUtils.encode(getFile.getOriginName(),
|
|
|
|
|
/* String encodedUploadFileName = UriUtils.encode(getFile.getOriginName(),
|
|
|
|
|
StandardCharsets.UTF_8);
|
|
|
|
|
|
|
|
|
|
String contentDisposition = "attachment; filename=\"" + encodedUploadFileName + "\"";
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok()
|
|
|
|
|
.header(HttpHeaders.CONTENT_DISPOSITION, contentDisposition)
|
|
|
|
|
.body(resource);
|
|
|
|
|
.body(resource);*/
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|