diff --git a/core/src/main/java/app/module/core/config/application/util/SftpFileDeleteUtil.java b/core/src/main/java/app/module/core/config/application/util/SftpFileDeleteUtil.java new file mode 100644 index 00000000..202541cb --- /dev/null +++ b/core/src/main/java/app/module/core/config/application/util/SftpFileDeleteUtil.java @@ -0,0 +1,84 @@ +package app.module.core.config.application.util; + +import app.module.core.config.adapters.persistence.entity.UploadFile; +import app.module.core.usecase.file.saveToStorage.ports.application.strategy.SftpConnector; +import com.jcraft.jsch.ChannelSftp; +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.stereotype.Component; + +import java.nio.file.Paths; + +@Slf4j +@Component +@RequiredArgsConstructor +public class SftpFileDeleteUtil { + + private final SftpConnector sftpConnector; + + @Value("${path.upload.sftp.public}") + private String publicPath; + + /** + * SFTP 서버에서 파일을 삭제하는 공통 메서드 + * @param deleteFile 삭제할 파일 정보 + * @throws JSchException SFTP 연결 오류 + * @throws SftpException SFTP 작업 오류 + */ + public void deleteRealFile(UploadFile deleteFile) throws JSchException, SftpException { + if (deleteFile == null || deleteFile.getPath() == null || deleteFile.getName() == null) { + log.error("Invalid file information: {}", deleteFile); + throw new IllegalArgumentException("Invalid file information"); + } + + // 경로 검증 및 생성 + String filePath = constructSafeFilePath(deleteFile.getPath(), deleteFile.getName()); + log.info("Attempting to delete file from SFTP: {}", filePath); + + // SFTP 연결 확인 및 재연결 + if (!sftpConnector.isConnected()) { + log.warn("SFTP connection lost, attempting to reconnect"); + sftpConnector.connect(); + } + ChannelSftp channelSftp = sftpConnector.getChannelSftp(); + if (channelSftp == null) { + log.error("SFTP channel is not initialized"); + throw new SftpException(ChannelSftp.SSH_FX_NO_CONNECTION, "SFTP channel is not initialized"); + } + + // 파일 삭제 + try { + channelSftp.stat(filePath); // 파일 존재 여부 확인 + channelSftp.rm(filePath); + log.info("File deleted successfully from SFTP: {}", filePath); + } catch (SftpException e) { + if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) { + log.warn("File does not exist on SFTP: {}", filePath); + } else { + log.error("Failed to delete file from SFTP: {}, error: {}", filePath, e.getMessage(), e); + throw e; + } + } + } + + /** + * 경로 조작 방지를 위해 안전한 파일 경로 생성 + */ + private String constructSafeFilePath(String subPath, String fileName) { + // 경로 및 파일 이름 정규화 + String normalizedSubPath = Paths.get(subPath).normalize().toString().replace("\\", "/"); + String normalizedFileName = Paths.get(fileName).getFileName().toString(); + String safePath = CommonUtil.removeLastChar(publicPath, "/") + "/" + normalizedSubPath + "/" + normalizedFileName; + + // 경로가 publicPath로 시작하는지 검증 + if (!safePath.startsWith(publicPath)) { + log.error("Invalid file path detected: {}", safePath); + throw new IllegalArgumentException("Invalid file path: " + safePath); + } + + return safePath; + } +} \ No newline at end of file